PL-8889 Implement Supplier<Map<String,Object>> paramsSupplier for standard actions that can provide parameters for window opening

This commit is contained in:
Yuriy Artamonov 2017-04-03 21:47:49 +04:00
parent bb71b57095
commit 5992c99675
4 changed files with 182 additions and 25 deletions

View File

@ -22,7 +22,10 @@ import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.ListComponent;
import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
@ -34,8 +37,10 @@ import org.springframework.context.annotation.Scope;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
/**
* Standard list action adding an entity instance to list from a lookup screen.
@ -60,7 +65,10 @@ public class AddAction extends BaseAction implements Action.HasOpenType, Action.
protected AfterAddHandler afterAddHandler;
protected String windowId;
protected Map<String, Object> windowParams;
protected Supplier<Map<String, Object>> windowParamsSupplier;
protected Security security = AppBeans.get(Security.NAME);
protected BeforeActionPerformedHandler beforeActionPerformedHandler;
@ -201,9 +209,7 @@ public class AddAction extends BaseAction implements Action.HasOpenType, Action.
return;
}
Map<String, Object> params = getWindowParams();
if (params == null)
params = new HashMap<>();
Map<String, Object> params = prepareWindowParams();
Window.Lookup.Handler handler = getHandler();
Window.Lookup.Handler itemsHandler = handler != null ? handler : new DefaultHandler();
@ -215,6 +221,22 @@ public class AddAction extends BaseAction implements Action.HasOpenType, Action.
});
}
protected Map<String, Object> prepareWindowParams() {
Map<String, Object> windowParams = getWindowParams();
Map<String, Object> supplierParams = null;
if (windowParamsSupplier != null) {
supplierParams = windowParamsSupplier.get();
}
Map<String, Object> params = Collections.emptyMap();
if (supplierParams != null || windowParams != null) {
params = new HashMap<>();
params.putAll(windowParams != null ? windowParams : Collections.emptyMap());
params.putAll(supplierParams != null ? supplierParams : Collections.emptyMap());
}
return params;
}
/**
* @return handler to pass to lookup screen
*/
@ -268,14 +290,28 @@ public class AddAction extends BaseAction implements Action.HasOpenType, Action.
}
/**
* @return lookup screen parameters
* @return lookup screen parameters
*/
public Map<String, Object> getWindowParams() {
return windowParams;
}
/**
* @param windowParams lookup screen parameters
* @return supplier that provides lookup screen parameters
*/
public Supplier<Map<String, Object>> getWindowParamsSupplier() {
return windowParamsSupplier;
}
/**
* @param windowParamsSupplier supplier that provides lookup screen parameters
*/
public void setWindowParamsSupplier(Supplier<Map<String, Object>> windowParamsSupplier) {
this.windowParamsSupplier = windowParamsSupplier;
}
/**
* @param windowParams lookup screen parameters
*/
public void setWindowParams(Map<String, Object> windowParams) {
this.windowParams = windowParams;
@ -285,7 +321,6 @@ public class AddAction extends BaseAction implements Action.HasOpenType, Action.
void handle(Collection items);
}
@Override
public BeforeActionPerformedHandler getBeforeActionPerformedHandler() {
return beforeActionPerformedHandler;

View File

@ -32,7 +32,9 @@ import org.springframework.context.annotation.Scope;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
/**
* Standard list action to create a new entity instance.
@ -56,8 +58,13 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
protected WindowManager.OpenType openType;
protected String windowId;
protected Map<String, Object> windowParams;
protected Supplier<Map<String, Object>> windowParamsSupplier;
protected Map<String, Object> initialValues;
protected Supplier<Map<String, Object>> initialValuesSupplier;
protected boolean addFirst = true;
protected Metadata metadata = AppBeans.get(Metadata.NAME);
@ -243,12 +250,7 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
}
}
Map<String, Object> values = getInitialValues();
if (values != null) {
for (Map.Entry<String, Object> entry : values.entrySet()) {
item.setValue(entry.getKey(), entry.getValue());
}
}
setInitialValuesToItem(item);
Datasource parentDs = null;
if (datasource instanceof PropertyDatasource) {
@ -258,16 +260,48 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
}
}
Map<String, Object> params = getWindowParams();
if (params == null) {
params = Collections.emptyMap();
}
Map<String, Object> params = prepareWindowParams();
internalOpenEditor(datasource, item, parentDs, params);
}
protected Map<String, Object> prepareWindowParams() {
Map<String, Object> windowParams = getWindowParams();
Map<String, Object> supplierParams = null;
if (windowParamsSupplier != null) {
supplierParams = windowParamsSupplier.get();
}
Map<String, Object> params = Collections.emptyMap();
if (supplierParams != null || windowParams != null) {
params = new HashMap<>();
params.putAll(windowParams != null ? windowParams : Collections.emptyMap());
params.putAll(supplierParams != null ? supplierParams : Collections.emptyMap());
}
return params;
}
protected void setInitialValuesToItem(Entity item) {
Map<String, Object> values = getInitialValues();
if (values != null) {
for (Map.Entry<String, Object> entry : values.entrySet()) {
item.setValue(entry.getKey(), entry.getValue());
}
}
if (initialValuesSupplier != null) {
Map<String, Object> supplierValues = initialValuesSupplier.get();
if (supplierValues != null) {
for (Map.Entry<String, Object> entry : supplierValues.entrySet()) {
item.setValue(entry.getKey(), entry.getValue());
}
}
}
}
@SuppressWarnings("unchecked")
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs,
Map<String, Object> params) {
Window.Editor window = target.getFrame().openEditor(getWindowId(), newItem, getOpenType(), params, parentDs);
if (editorCloseListener == null) {
@ -346,12 +380,26 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
}
/**
* @param windowParams editor screen parameters
* @param windowParams editor screen parameters
*/
public void setWindowParams(Map<String, Object> windowParams) {
this.windowParams = windowParams;
}
/**
* @return supplier that provides editor screen parameters
*/
public Supplier<Map<String, Object>> getWindowParamsSupplier() {
return windowParamsSupplier;
}
/**
* @param windowParamsSupplier supplier that provides editor screen parameters
*/
public void setWindowParamsSupplier(Supplier<Map<String, Object>> windowParamsSupplier) {
this.windowParamsSupplier = windowParamsSupplier;
}
/**
* @return map of initial values for attributes of created entity
*/
@ -366,6 +414,20 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
this.initialValues = initialValues;
}
/**
* @return supplier that provides map of initial values for attributes of created entity
*/
public Supplier<Map<String, Object>> getInitialValuesSupplier() {
return initialValuesSupplier;
}
/**
* @param initialValuesSupplier supplier that provides map of initial values for attributes of created entity
*/
public void setInitialValuesSupplier(Supplier<Map<String, Object>> initialValuesSupplier) {
this.initialValuesSupplier = initialValuesSupplier;
}
/**
* @return whether this action will add a new instance to the beginning of the datasource's collection.
* Affects only standalone datasources, for nested datasources new items are always added to the end.
@ -384,7 +446,6 @@ public class CreateAction extends BaseAction implements Action.HasOpenType, Acti
this.addFirst = addFirst;
}
/**
* Hook invoked after the editor was committed and closed
* @param entity new committed entity instance

View File

@ -36,8 +36,10 @@ import com.haulmont.cuba.security.entity.EntityOp;
import org.springframework.context.annotation.Scope;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
/**
* Standard list action to edit an entity instance.
@ -62,6 +64,7 @@ public class EditAction extends ItemTrackingAction implements Action.HasOpenType
protected String windowId;
protected Map<String, Object> windowParams;
protected Supplier<Map<String, Object>> windowParamsSupplier;
// Set default caption only once
protected boolean captionInitialized = false;
@ -217,15 +220,28 @@ public class EditAction extends ItemTrackingAction implements Action.HasOpenType
}
}
Map<String, Object> params = getWindowParams();
if (params == null) {
params = Collections.emptyMap();
}
Map<String, Object> params = prepareWindowParams();
internalOpenEditor(datasource, datasource.getItem(), parentDs, params);
}
}
protected Map<String, Object> prepareWindowParams() {
Map<String, Object> windowParams = getWindowParams();
Map<String, Object> supplierParams = null;
if (windowParamsSupplier != null) {
supplierParams = windowParamsSupplier.get();
}
Map<String, Object> params = Collections.emptyMap();
if (supplierParams != null || windowParams != null) {
params = new HashMap<>();
params.putAll(windowParams != null ? windowParams : Collections.emptyMap());
params.putAll(supplierParams != null ? supplierParams : Collections.emptyMap());
}
return params;
}
protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem,
Datasource parentDs, Map<String, Object> params) {
@ -309,6 +325,20 @@ public class EditAction extends ItemTrackingAction implements Action.HasOpenType
this.windowParams = windowParams;
}
/**
* @return supplier that provides editor screen parameters
*/
public Supplier<Map<String, Object>> getWindowParamsSupplier() {
return windowParamsSupplier;
}
/**
* @param windowParamsSupplier supplier that provides editor screen parameters
*/
public void setWindowParamsSupplier(Supplier<Map<String, Object>> windowParamsSupplier) {
this.windowParamsSupplier = windowParamsSupplier;
}
/**
* Hook invoked after the editor was committed and closed
* @param entity new committed entity instance

View File

@ -23,7 +23,10 @@ import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import org.springframework.context.annotation.Scope;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
/**
* Standard list action to refresh a list of entities.
@ -46,6 +49,7 @@ public class RefreshAction extends BaseAction {
protected ListComponent owner;
protected Map<String, Object> refreshParams;
protected Supplier<Map<String, Object>> refreshParamsSupplier;
protected Runnable beforeRefreshHandler;
protected Runnable afterRefreshHandler;
@ -101,7 +105,20 @@ public class RefreshAction extends BaseAction {
}
CollectionDatasource datasource = owner.getDatasource();
Map<String, Object> params = getRefreshParams();
Map<String, Object> refreshParams = getRefreshParams();
Map<String, Object> supplierParams = null;
if (refreshParamsSupplier != null) {
supplierParams = refreshParamsSupplier.get();
}
Map<String, Object> params = null;
if (supplierParams != null || refreshParams != null) {
params = new HashMap<>();
params.putAll(refreshParams != null ? refreshParams : Collections.emptyMap());
params.putAll(supplierParams != null ? supplierParams : Collections.emptyMap());
}
if (params != null) {
datasource.refresh(params);
} else {
@ -127,6 +144,20 @@ public class RefreshAction extends BaseAction {
this.refreshParams = refreshParams;
}
/**
* @return supplier that provides parameters for {@link CollectionDatasource#refresh(java.util.Map)} method
*/
public Supplier<Map<String, Object>> getRefreshParamsSupplier() {
return refreshParamsSupplier;
}
/**
* @param refreshParamsSupplier supplier that provides parameters for {@link CollectionDatasource#refresh(java.util.Map)} method
*/
public void setRefreshParamsSupplier(Supplier<Map<String, Object>> refreshParamsSupplier) {
this.refreshParamsSupplier = refreshParamsSupplier;
}
public Runnable getBeforeRefreshHandler() {
return beforeRefreshHandler;
}