From 26e2c7744a3cbcea19d62091c20de2897b3eb37e Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Thu, 25 Jul 2013 07:52:45 +0000 Subject: [PATCH] A slightly modified patch from Niclas Malin for "New elements auto-parameters-[entity/service] for link/hyperlink" https://issues.apache.org/jira/browse/OFBIZ-4472 When you want edit/remove an entity from link call it's needed to give all pk on parameter element as like : {quote} {quote} To simplify, I introduce auto-parameters-entity like this : {quote} {quote} or {quote}
{quote} This method is inspired from auto-fields-entity and I homogenized on widget renderer screen (link), menu (link) and form (hyperlink, sub-link) jleroux: I simply put returns in case of no dispatcher or delegator git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1506835 13f79535-47bb-0310-9956-ffa450edef68 --- framework/widget/dtd/widget-form.xsd | 60 ++++++++ framework/widget/dtd/widget-menu.xsd | 56 ++++++++ framework/widget/dtd/widget-screen.xsd | 56 ++++++++ .../src/org/ofbiz/widget/WidgetWorker.java | 133 ++++++++++++++++++ .../src/org/ofbiz/widget/form/ModelForm.java | 47 ++++++- .../org/ofbiz/widget/form/ModelFormField.java | 50 +++++-- .../org/ofbiz/widget/menu/ModelMenuItem.java | 17 ++- .../widget/screen/ModelScreenWidget.java | 21 ++- 8 files changed, 427 insertions(+), 13 deletions(-) diff --git a/framework/widget/dtd/widget-form.xsd b/framework/widget/dtd/widget-form.xsd index 95ba7f4839..d9d15da6b4 100644 --- a/framework/widget/dtd/widget-form.xsd +++ b/framework/widget/dtd/widget-form.xsd @@ -426,6 +426,8 @@ under the License. + + @@ -962,6 +964,8 @@ under the License. + + @@ -1055,6 +1059,60 @@ under the License. + + + + + + + + The service name used to resolve parameters. If empty, use form defaultServiceName. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + The entity name used to resolve parameters. If empty use form defaultEntityName attribute. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1699,6 +1757,8 @@ under the License. + + diff --git a/framework/widget/dtd/widget-menu.xsd b/framework/widget/dtd/widget-menu.xsd index fdf47ffa2e..501f57ea66 100644 --- a/framework/widget/dtd/widget-menu.xsd +++ b/framework/widget/dtd/widget-menu.xsd @@ -148,6 +148,8 @@ under the License. + + @@ -231,6 +233,60 @@ under the License. + + + + + + + + The service name used to resolve parameters. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + The entity name used to resolve parameters. If empty try use menu defaultEntityName attribute. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/framework/widget/dtd/widget-screen.xsd b/framework/widget/dtd/widget-screen.xsd index 8921f0ef1c..9d96925758 100644 --- a/framework/widget/dtd/widget-screen.xsd +++ b/framework/widget/dtd/widget-screen.xsd @@ -772,6 +772,8 @@ under the License. + + @@ -841,6 +843,60 @@ under the License. + + + + + + + + The service name used to resolve parameters. If empty, use form defaultServiceName. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + The entity name used to resolve parameters. If empty use form defaultEntityName attribute. Flexible string allowed. + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/framework/widget/src/org/ofbiz/widget/WidgetWorker.java b/framework/widget/src/org/ofbiz/widget/WidgetWorker.java index bacdbae943..e238c93248 100644 --- a/framework/widget/src/org/ofbiz/widget/WidgetWorker.java +++ b/framework/widget/src/org/ofbiz/widget/WidgetWorker.java @@ -25,6 +25,8 @@ import java.math.BigDecimal; import java.net.URLEncoder; import java.nio.charset.Charset; import java.text.DateFormat; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.TimeZone; @@ -32,16 +34,25 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javolution.util.FastList; +import javolution.util.FastMap; + import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.collections.FlexibleMapAccessor; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.model.ModelEntity; +import org.ofbiz.entity.model.ModelField; +import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ModelParam; +import org.ofbiz.service.ModelService; import org.ofbiz.webapp.control.ConfigXMLReader; import org.ofbiz.webapp.control.RequestHandler; import org.ofbiz.webapp.taglib.ContentUrlTag; @@ -396,6 +407,128 @@ public class WidgetWorker { } } + public static class AutoServiceParameters { + private String serviceName; + List excludeList = FastList.newInstance(); + boolean includePk; + boolean includeNonPk; + boolean sendIfEmpty; + public AutoServiceParameters(Element autoElement){ + serviceName = UtilXml.checkEmpty(autoElement.getAttribute("service-name")); + sendIfEmpty = "true".equals(autoElement.getAttribute("send-if-empty")); + List excludes = UtilXml.childElementList(autoElement, "exclude"); + if (excludes != null) { + for (Element exclude: excludes) { + if (UtilValidate.isNotEmpty(exclude.getAttribute("field-name"))) { + excludeList.add(exclude.getAttribute("field-name")); + } + } + } + } + + public Map getParametersMap(Map context, String defaultServiceName) { + Map autServiceParams = FastMap.newInstance(); + LocalDispatcher dispatcher = (LocalDispatcher) context.get("dispatcher"); + if (dispatcher == null) { + Debug.logError("We can not append auto service Parameters since we could not find dispatcher in the current context", module); + return autServiceParams; + } + if (UtilValidate.isEmpty(serviceName)) serviceName = defaultServiceName; + FlexibleStringExpander toExpand = FlexibleStringExpander.getInstance(serviceName); + ModelService service = null; + try { + service = dispatcher.getDispatchContext().getModelService(toExpand.toString()); + } catch (GenericServiceException e) { + Debug.logError("Resolve service throw an error : " + e, module); + } + if (service == null) { + Debug.logError("We can not append auto service Parameters since we could not find service with name [" + serviceName + "]", module); + return autServiceParams; + } + + Iterator paramsIter = service.getInModelParamList().iterator(); + if (paramsIter != null) { + while (paramsIter.hasNext()) { + ModelParam param = paramsIter.next(); + if (param.getInternal()) continue; + String paramName = param.getName(); + FlexibleMapAccessor fma = FlexibleMapAccessor.getInstance(paramName); + if (!excludeList.contains(paramName)) { + Object flexibleValue = fma.get(context); + if (UtilValidate.isEmpty(flexibleValue) && context.containsKey("parameters")) { + flexibleValue = fma.get((Map) context.get("parameters")); + } + if (UtilValidate.isNotEmpty(flexibleValue) || sendIfEmpty) { + autServiceParams.put(paramName, String.valueOf(flexibleValue)); + } + } + } + } + return autServiceParams; + } + } + + public static class AutoEntityParameters { + private String entityName; + private String includeType; + List excludeList = FastList.newInstance(); + boolean includePk; + boolean includeNonPk; + boolean sendIfEmpty; + public AutoEntityParameters(Element autoElement){ + entityName = UtilXml.checkEmpty(autoElement.getAttribute("entity-name")); + sendIfEmpty = "true".equals(autoElement.getAttribute("send-if-empty")); + includeType = UtilXml.checkEmpty(autoElement.getAttribute("include")); + includePk = "pk".equals(includeType) || "all".equals(includeType); + includeNonPk = "nonpk".equals(includeType) || "all".equals(includeType); + List excludes = UtilXml.childElementList(autoElement, "exclude"); + if (excludes != null) { + for (Element exclude: excludes) { + if (UtilValidate.isNotEmpty(exclude.getAttribute("field-name"))) { + excludeList.add(exclude.getAttribute("field-name")); + } + } + } + } + + public Map getParametersMap(Map context, String defaultEntityName) { + Map autEntityParams = FastMap.newInstance(); + Delegator delegator = (Delegator) context.get("delegator"); + if (delegator == null) { + Debug.logError("We can not append auto entity Parameters since we could not find delegator in the current context", module); + return autEntityParams; + } + if (UtilValidate.isEmpty(entityName)) entityName = defaultEntityName; + FlexibleStringExpander toExpand = FlexibleStringExpander.getInstance(entityName); + ModelEntity entity = delegator.getModelEntity(toExpand.expandString(context)); + if (entity == null) { + Debug.logError("We can not append auto entity Parameters since we could not find entity with name [" + entityName + "]", module); + return autEntityParams; + } + + Iterator fieldsIter = entity.getFieldsIterator(); + if (fieldsIter != null) { + while (fieldsIter.hasNext()) { + ModelField field = fieldsIter.next(); + String fieldName = field.getName(); + FlexibleMapAccessor fma = FlexibleMapAccessor.getInstance(fieldName); + boolean shouldExclude = excludeList.contains(fieldName); + if ((!shouldExclude) && (!field.getIsAutoCreatedInternal()) + && ((field.getIsPk() && includePk) || (!field.getIsPk() && includeNonPk))) { + Object flexibleValue = fma.get(context); + if (UtilValidate.isEmpty(flexibleValue) && context.containsKey("parameters")) { + flexibleValue = fma.get((Map) context.get("parameters")); + } + if (UtilValidate.isNotEmpty(flexibleValue) || sendIfEmpty) { + autEntityParams.put(fieldName, String.valueOf(flexibleValue)); + } + } + } + } + return autEntityParams; + } + } + public static String determineAutoLinkType(String linkType, String target, String targetType, HttpServletRequest request) { if ("auto".equals(linkType)) { if ("intra-app".equals(targetType)) { diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java index 6fcf1af2df..c77f5ca0cf 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java @@ -513,6 +513,9 @@ public class ModelForm extends ModelWidget { UpdateArea updateArea = new UpdateArea(updateAreaElement); this.addOnEventUpdateArea(updateArea); } + //propagate defaultEntityName on updateAreas + if (UtilValidate.isNotEmpty(this.defaultEntityName)) this.setDefaultEntityNameOnUpdateAreas(); + if (UtilValidate.isNotEmpty(this.defaultServiceName)) this.setDefaultServiceNameOnUpdateAreas(); // auto-fields-service for (Element autoFieldsServiceElement: UtilXml.childElementList(formElement, "auto-fields-service")) { @@ -2814,6 +2817,28 @@ public class ModelForm extends ModelWidget { return this.onSubmitUpdateAreas; } + public void setDefaultEntityNameOnUpdateAreas() { + List allUpdateAreas = FastList.newInstance(); + if (UtilValidate.isNotEmpty(this.onSubmitUpdateAreas)) allUpdateAreas.addAll(this.onSubmitUpdateAreas); + if (UtilValidate.isNotEmpty(this.onPaginateUpdateAreas)) allUpdateAreas.addAll(this.onPaginateUpdateAreas); + for (UpdateArea updateArea : allUpdateAreas) { + if (UtilValidate.isEmpty(updateArea.defaultEntityName)) { + updateArea.defaultEntityName = this.defaultEntityName; + } + } + } + + public void setDefaultServiceNameOnUpdateAreas() { + List allUpdateAreas = FastList.newInstance(); + if (UtilValidate.isNotEmpty(this.onSubmitUpdateAreas)) allUpdateAreas.addAll(this.onSubmitUpdateAreas); + if (UtilValidate.isNotEmpty(this.onPaginateUpdateAreas)) allUpdateAreas.addAll(this.onPaginateUpdateAreas); + for (UpdateArea updateArea : allUpdateAreas) { + if (UtilValidate.isEmpty(updateArea.defaultServiceName)) { + updateArea.defaultServiceName = this.defaultServiceName; + } + } + } + public static class AltRowStyle { public String useWhen; public String style; @@ -2878,7 +2903,11 @@ public class ModelForm extends ModelWidget { protected String eventType; protected String areaId; protected String areaTarget; - List parameterList =FastList.newInstance(); + protected String defaultServiceName; + protected String defaultEntityName; + protected WidgetWorker.AutoEntityParameters autoEntityParameters; + protected WidgetWorker.AutoEntityParameters autoServiceParameters; + List parameterList = FastList.newInstance(); /** XML constructor. * @param updateAreaElement The <on-xxx-update-area> * XML element. @@ -2891,6 +2920,14 @@ public class ModelForm extends ModelWidget { for (Element parameterElement: parameterElementList) { this.parameterList.add(new WidgetWorker.Parameter(parameterElement)); } + Element autoServiceParamsElement = UtilXml.firstChildElement(updateAreaElement, "auto-parameters-service"); + if (autoServiceParamsElement != null) { + autoServiceParameters = new WidgetWorker.AutoEntityParameters(autoServiceParamsElement); + } + Element autoEntityParamsElement = UtilXml.firstChildElement(updateAreaElement, "auto-parameters-entity"); + if (autoEntityParamsElement != null) { + autoEntityParameters = new WidgetWorker.AutoEntityParameters(autoEntityParamsElement); + } } /** String constructor. * @param areaId The id of the widget element to be updated @@ -2920,10 +2957,16 @@ public class ModelForm extends ModelWidget { } public Map getParameterMap(Map context) { Map fullParameterMap = FastMap.newInstance(); + if (autoServiceParameters != null) { + fullParameterMap.putAll(autoServiceParameters.getParametersMap(context, defaultServiceName)); + } + if (autoEntityParameters != null) { + fullParameterMap.putAll(autoEntityParameters.getParametersMap(context, defaultEntityName)); + } for (WidgetWorker.Parameter parameter: this.parameterList) { fullParameterMap.put(parameter.getName(), parameter.getValue(context)); } - + return fullParameterMap; } } diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java index 5a8ddf4a8b..ed36ff90a6 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java @@ -218,13 +218,13 @@ public class ModelFormField { } protected void addOnChangeUpdateArea(UpdateArea updateArea) { - if (onChangeUpdateAreas == null) onChangeUpdateAreas = FastList.newInstance(); + if (onChangeUpdateAreas == null) onChangeUpdateAreas = FastList.newInstance(); onChangeUpdateAreas.add(updateArea); Debug.logInfo(this.modelForm.getName() + ":" + this.name + " onChangeUpdateAreas size = " + onChangeUpdateAreas.size(), module); } protected void addOnClickUpdateArea(UpdateArea updateArea) { - if (onClickUpdateAreas == null) onClickUpdateAreas = FastList.newInstance(); + if (onClickUpdateAreas == null) onClickUpdateAreas = FastList.newInstance(); onClickUpdateAreas.add(updateArea); } @@ -2226,6 +2226,8 @@ public class ModelFormField { protected FlexibleStringExpander targetWindowExdr; protected FlexibleMapAccessor> parametersMapAcsr; protected List parameterList = FastList.newInstance(); + protected WidgetWorker.AutoServiceParameters autoServiceParameters; + protected WidgetWorker.AutoEntityParameters autoEntityParameters; protected boolean requestConfirmation = false; protected FlexibleStringExpander confirmationMsgExdr; @@ -2261,6 +2263,14 @@ public class ModelFormField { for (Element parameterElement: parameterElementList) { this.parameterList.add(new WidgetWorker.Parameter(parameterElement)); } + Element autoServiceParamsElement = UtilXml.firstChildElement(element, "auto-parameters-service"); + if (autoServiceParamsElement != null) { + autoServiceParameters = new WidgetWorker.AutoServiceParameters(autoServiceParamsElement); + } + Element autoEntityParamsElement = UtilXml.firstChildElement(element, "auto-parameters-entity"); + if (autoEntityParamsElement != null) { + autoEntityParameters = new WidgetWorker.AutoEntityParameters(autoEntityParamsElement); + } } @Override @@ -2278,8 +2288,8 @@ public class ModelFormField { public String getConfirmation(Map context) { String message = getConfirmationMsg(context); - if (UtilValidate.isNotEmpty(message)) return message; - + if (UtilValidate.isNotEmpty(message)) return message; + if (getRequestConfirmation()) { String defaultMessage = UtilProperties.getPropertyValue("general", "default.confirmation.message", "${uiLabelMap.CommonConfirm}"); setConfirmationMsg(defaultMessage); @@ -2333,11 +2343,19 @@ public class ModelFormField { if (addlParamMap != null) { fullParameterMap.putAll(addlParamMap); } - + for (WidgetWorker.Parameter parameter: this.parameterList) { fullParameterMap.put(parameter.getName(), parameter.getValue(context)); } - + + if (autoServiceParameters != null) { + fullParameterMap.putAll(autoServiceParameters.getParametersMap(context, this.getModelFormField().getModelForm().getDefaultServiceName())); + } + + if (autoEntityParameters != null) { + fullParameterMap.putAll(autoEntityParameters.getParametersMap(context, this.getModelFormField().getModelForm().getDefaultEntityName())); + } + return fullParameterMap; } @@ -2419,6 +2437,8 @@ public class ModelFormField { protected boolean requestConfirmation = false; protected FlexibleStringExpander confirmationMsgExdr; protected ModelFormField modelFormField; + protected WidgetWorker.AutoServiceParameters autoServiceParameters; + protected WidgetWorker.AutoEntityParameters autoEntityParameters; public SubHyperlink(Element element, ModelFormField modelFormField) { this.setDescription(element.getAttribute("description")); @@ -2434,7 +2454,14 @@ public class ModelFormField { } setRequestConfirmation("true".equals(element.getAttribute("request-confirmation"))); setConfirmationMsg(element.getAttribute("confirmation-message")); - + Element autoServiceParamsElement = UtilXml.firstChildElement(element, "auto-parameters-service"); + if (autoServiceParamsElement != null) { + autoServiceParameters = new WidgetWorker.AutoServiceParameters(autoServiceParamsElement); + } + Element autoEntityParamsElement = UtilXml.firstChildElement(element, "auto-parameters-entity"); + if (autoEntityParamsElement != null) { + autoEntityParameters = new WidgetWorker.AutoEntityParameters(autoEntityParamsElement); + } this.modelFormField = modelFormField; } @@ -2485,7 +2512,14 @@ public class ModelFormField { for (WidgetWorker.Parameter parameter: this.parameterList) { fullParameterMap.put(parameter.getName(), parameter.getValue(context)); } - + + if (autoServiceParameters != null) { + fullParameterMap.putAll(autoServiceParameters.getParametersMap(context, this.getModelFormField().getModelForm().getDefaultServiceName())); + } + if (autoEntityParameters != null) { + fullParameterMap.putAll(autoEntityParameters.getParametersMap(context, this.getModelFormField().getModelForm().getDefaultEntityName())); + } + return fullParameterMap; } diff --git a/framework/widget/src/org/ofbiz/widget/menu/ModelMenuItem.java b/framework/widget/src/org/ofbiz/widget/menu/ModelMenuItem.java index a2b8e14793..d45d225d9b 100644 --- a/framework/widget/src/org/ofbiz/widget/menu/ModelMenuItem.java +++ b/framework/widget/src/org/ofbiz/widget/menu/ModelMenuItem.java @@ -578,6 +578,8 @@ public class ModelMenuItem { protected boolean secure = false; protected boolean encode = false; protected String linkType; + protected WidgetWorker.AutoServiceParameters autoServiceParameters; + protected WidgetWorker.AutoEntityParameters autoEntityParameters; protected FlexibleMapAccessor> parametersMapAcsr; protected List parameterList = FastList.newInstance(); protected boolean requestConfirmation = false; @@ -609,6 +611,14 @@ public class ModelMenuItem { } setRequestConfirmation("true".equals(linkElement.getAttribute("request-confirmation"))); setConfirmationMsg(linkElement.getAttribute("confirmation-message")); + Element autoServiceParamsElement = UtilXml.firstChildElement(linkElement, "auto-parameters-service"); + if (autoServiceParamsElement != null) { + autoServiceParameters = new WidgetWorker.AutoServiceParameters(autoServiceParamsElement); + } + Element autoEntityParamsElement = UtilXml.firstChildElement(linkElement, "auto-parameters-entity"); + if (autoEntityParamsElement != null) { + autoEntityParameters = new WidgetWorker.AutoEntityParameters(autoEntityParamsElement); + } } public Link(ModelMenuItem parentMenuItem) { @@ -716,7 +726,12 @@ public class ModelMenuItem { for (WidgetWorker.Parameter parameter: this.parameterList) { fullParameterMap.put(parameter.getName(), parameter.getValue(context)); } - + if (autoServiceParameters != null) { + fullParameterMap.putAll(autoServiceParameters.getParametersMap(context, null)); + } + if (autoEntityParameters != null) { + fullParameterMap.putAll(autoEntityParameters.getParametersMap(context, linkMenuItem.getModelMenu().getDefaultEntityName())); + } return fullParameterMap; } diff --git a/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java b/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java index 8acabc7689..cf5bb9e9ef 100644 --- a/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java +++ b/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java @@ -1387,7 +1387,8 @@ public abstract class ModelScreenWidget extends ModelWidget { protected String width; protected String height; protected List parameterList = FastList.newInstance(); - + protected WidgetWorker.AutoServiceParameters autoServiceParameters; + protected WidgetWorker.AutoEntityParameters autoEntityParameters; public Link(ModelScreen modelScreen, Element linkElement) { super(modelScreen, linkElement); @@ -1414,6 +1415,15 @@ public abstract class ModelScreenWidget extends ModelWidget { this.parameterList.add(new WidgetWorker.Parameter(parameterElement)); } + Element autoServiceParamsElement = UtilXml.firstChildElement(linkElement, "auto-parameters-service"); + if (autoServiceParamsElement != null) { + autoServiceParameters = new WidgetWorker.AutoServiceParameters(autoServiceParamsElement); + } + Element autoEntityParamsElement = UtilXml.firstChildElement(linkElement, "auto-parameters-entity"); + if (autoEntityParamsElement != null) { + autoEntityParameters = new WidgetWorker.AutoEntityParameters(autoEntityParamsElement); + } + this.width = linkElement.getAttribute("width"); this.height = linkElement.getAttribute("height"); } @@ -1512,7 +1522,14 @@ public abstract class ModelScreenWidget extends ModelWidget { for (WidgetWorker.Parameter parameter: this.parameterList) { fullParameterMap.put(parameter.getName(), parameter.getValue(context)); } - + + if (autoServiceParameters != null) { + fullParameterMap.putAll(autoServiceParameters.getParametersMap(context, null)); + } + if (autoEntityParameters != null) { + fullParameterMap.putAll(autoEntityParameters.getParametersMap(context, null)); + } + return fullParameterMap; }