From 1bd2caae4a08504a3af3348b7d3ed3ca7c7b0d6e Mon Sep 17 00:00:00 2001 From: Daniel Watford Date: Tue, 25 Feb 2020 13:52:27 +0000 Subject: [PATCH 1/3] Improved: Backport - Use FlexibleStringExpander for field parameter names. (OFBIZ-11415) Allows generation of unique names when repeating rendering of a form on a screen. Backport cherry-picked from commit 52e21a3d2e9123674e151d255fe898f86a5be72e with ModelFormFieldTest removed. --- .../org/apache/ofbiz/widget/model/ModelFormField.java | 8 ++++---- .../apache/ofbiz/widget/model/ModelFormFieldBuilder.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java index 6ab2c7d89c..ff82d24481 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java @@ -122,7 +122,7 @@ public class ModelFormField { protected final String name; private final List onChangeUpdateAreas; private final List onClickUpdateAreas; - protected final String parameterName; + protected final FlexibleStringExpander parameterName; private final Integer position; private final String redWhen; private final Boolean requiredField; @@ -522,7 +522,7 @@ public class ModelFormField { return onClickUpdateAreas; } - public String getParameterName() { + public FlexibleStringExpander getParameterName() { return parameterName; } @@ -535,7 +535,7 @@ public class ModelFormField { public String getParameterName(Map context) { String baseName; if (UtilValidate.isNotEmpty(this.parameterName)) { - baseName = this.parameterName; + baseName = this.parameterName.expandString(context); } else { baseName = this.name; } @@ -1853,7 +1853,7 @@ public class ModelFormField { public String getParameterNameOther(Map context) { String baseName; if (UtilValidate.isNotEmpty(getModelFormField().parameterName)) { - baseName = getModelFormField().parameterName; + baseName = getModelFormField().parameterName.expandString(context); } else { baseName = getModelFormField().name; } diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormFieldBuilder.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormFieldBuilder.java index 656b99d554..ef678c8b9c 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormFieldBuilder.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormFieldBuilder.java @@ -88,7 +88,7 @@ public class ModelFormFieldBuilder { private String name = ""; private List onChangeUpdateAreas = new ArrayList<>(); private List onClickUpdateAreas = new ArrayList<>(); - private String parameterName = ""; + private FlexibleStringExpander parameterName = FlexibleStringExpander.getInstance(""); private Integer position = null; private String redWhen = ""; private Boolean requiredField = null; @@ -133,7 +133,7 @@ public class ModelFormFieldBuilder { this.mapAcsr = FlexibleMapAccessor.getInstance(fieldElement.getAttribute("map-name")); this.modelForm = modelForm; this.name = name; - this.parameterName = UtilXml.checkEmpty(fieldElement.getAttribute("parameter-name"), name); + this.parameterName = FlexibleStringExpander.getInstance(UtilXml.checkEmpty(fieldElement.getAttribute("parameter-name"), name)); String positionAtttr = fieldElement.getAttribute("position"); Integer position = null; if (!positionAtttr.isEmpty()) { @@ -406,7 +406,7 @@ public class ModelFormFieldBuilder { return onClickUpdateAreas; } - public String getParameterName() { + public FlexibleStringExpander getParameterName() { return parameterName; } @@ -905,7 +905,7 @@ public class ModelFormFieldBuilder { } public ModelFormFieldBuilder setParameterName(String parameterName) { - this.parameterName = parameterName; + this.parameterName = FlexibleStringExpander.getInstance(parameterName); return this; } From b518a5df2e806980c0f409ba2dbe919ae1176cb2 Mon Sep 17 00:00:00 2001 From: Daniel Watford Date: Mon, 2 Mar 2020 09:29:51 +0000 Subject: [PATCH 2/3] Improved: Backport - Use FlexibleStringExpander in form widget lookup field target parameters. (OFBIZ-11418) Changed target-parameter attribute of the form widget's Lookup field to use the FlexibleStringExpander type rather than String. This has been done to allow reference to other form fields which may have had their parameter-name attribute set to a FlexibleStringExpander expression. Backport cherry-picked from commit 08a371d0471a506e663c879f30299f424fc70921 with ModelFormFieldTest removed. --- .../ofbiz/widget/model/ModelFormField.java | 46 +++++++++++++------ .../renderer/macro/MacroFormRenderer.java | 2 +- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java index ff82d24481..096e875761 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelFormField.java @@ -24,6 +24,7 @@ import java.sql.Timestamp; import java.text.DateFormat; import java.text.NumberFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -33,8 +34,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.StringTokenizer; +import java.util.Optional; import java.util.TimeZone; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.ofbiz.base.conversion.ConversionException; import org.apache.ofbiz.base.conversion.DateTimeConverters; @@ -3101,7 +3106,7 @@ public class ModelFormField { private final String lookupPresentation; private final String lookupWidth; private final String showDescription; - private final String targetParameter; + private final FlexibleStringExpander targetParameter; public LookupField(Element element, ModelFormField modelFormField) { super(element, modelFormField); @@ -3114,7 +3119,7 @@ public class ModelFormField { this.lookupPresentation = element.getAttribute("presentation"); this.lookupWidth = element.getAttribute("width"); this.showDescription = element.getAttribute("show-description"); - this.targetParameter = element.getAttribute("target-parameter"); + this.targetParameter = FlexibleStringExpander.getInstance(element.getAttribute("target-parameter")); } public LookupField(int fieldSource, ModelFormField modelFormField) { @@ -3128,7 +3133,7 @@ public class ModelFormField { this.lookupPresentation = ""; this.lookupWidth = ""; this.showDescription = ""; - this.targetParameter = ""; + this.targetParameter = FlexibleStringExpander.getInstance(""); } public LookupField(LookupField original, ModelFormField modelFormField) { @@ -3196,19 +3201,32 @@ public class ModelFormField { return UtilValidate.isEmpty(this.showDescription) ? null : "true".equals(this.showDescription); } - public String getTargetParameter() { + public FlexibleStringExpander getTargetParameter() { return targetParameter; } + public String getTargetParameter(final Map context) { + return targetParameter.expandString(context); + } + + /** + * @deprecated Target Parameter is potentially a FlexibleStringExpander expression and should therefore be + * evaluated within a given context. + *

Use {@link #getTargetParameter(Map)} instead.

+ */ + @Deprecated public List getTargetParameterList() { - List paramList = new LinkedList<>(); - if (UtilValidate.isNotEmpty(this.targetParameter)) { - StringTokenizer stk = new StringTokenizer(this.targetParameter, ", "); - while (stk.hasMoreTokens()) { - paramList.add(stk.nextToken()); - } - } - return paramList; + // Evaluate target-parameter with a null context, treating it as a plain string. + return getTargetParameterList(null); + } + + public List getTargetParameterList(final Map context) { + return Optional.ofNullable(getTargetParameter(context)) + .map(x -> x.split(",")) + .map(Arrays::stream) + .orElse(Stream.empty()) + .map(String::trim) + .collect(Collectors.toList()); } @Override @@ -4360,4 +4378,4 @@ public class ModelFormField { formStringRenderer.renderTextFindField(writer, context, this); } } -} \ No newline at end of file +} diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java index a0541074a3..08a92a0e54 100644 --- a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java +++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java @@ -2190,7 +2190,7 @@ public final class MacroFormRenderer implements FormStringRenderer { StringBuilder targetParameterIter = new StringBuilder(); StringBuilder imgSrc = new StringBuilder(); // FIXME: refactor using the StringUtils methods - List targetParameterList = lookupField.getTargetParameterList(); + List targetParameterList = lookupField.getTargetParameterList(context); targetParameterIter.append("["); for (String targetParameter : targetParameterList) { if (targetParameterIter.length() > 1) { From 308982526d75fd8ff70b70879410e706fe01703b Mon Sep 17 00:00:00 2001 From: Daniel Watford Date: Mon, 2 Mar 2020 09:35:31 +0000 Subject: [PATCH 3/3] Documented: Backport - Documented use of field attribute parameter-name and lookup field attribute target-parameter in widget-form.xsd (OFBIZ-11418) Backport cherry-picked from commit 5640de4eba0148e4a95d72528945866b7069d2ed. --- framework/widget/dtd/widget-form.xsd | 29 ++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/framework/widget/dtd/widget-form.xsd b/framework/widget/dtd/widget-form.xsd index 5c6fd7cffe..36ef8099ab 100644 --- a/framework/widget/dtd/widget-form.xsd +++ b/framework/widget/dtd/widget-form.xsd @@ -647,7 +647,13 @@ under the License. - Defaults to the value of the name attribute; is the name of the parameter to use for this field (the request parameter in a web application). + Specifies the parameter name to use for this field - i.e. the name attribute of an + HTML input field in a web application. + + Accepts Unified Expression Language (JSR 245) notation, i.e. ${}, to allow use of expressions. + + Defaults to the value of the name attribute. + @@ -656,7 +662,7 @@ under the License. The name of this field that will be shown to the user. It can use the ${} and foo.bar (dot) syntax to insert values from the context for parameterization or internationalization. - Be sure a "FormFieldTitle_" labels does not already exist, else it will be overriden. + Be sure a "FormFieldTitle_" labels does not already exist, else it will be overridden. @@ -1187,10 +1193,21 @@ under the License. - The name of a field whose value is passed in as a parameter to the lookup target form. - Can be a comma separated list. You need to affect it the value using parameters.parmI - (where I is the position number in the list, I must begin at 0) in the related lookup screen. - See LookupPreferredContactMech as example + Comma separated list of the parameter-names of the fields whose values are passed + as parameters to the lookup target form. + + Accepts Unified Expression Language (JSR 245) notation, i.e. ${}, to allow use of expressions. + Expressions are evaluated before splitting at any commas. After splitting, whitespace is trimmed + from the beginning and end of the resulting elements. + + The position of a field name in the list corresponds to the parameter name that will be passed + to the lookup form. Parameter names follow the naming convention parmN where N is the list + position of the field name, starting with 0. + + Example: A form has a text input field with parameter-name alpha and value 42; and text input + field with parameter-name bravo and value 107. A lookup field has been created for that form + with target-parameter="bravo,alpha". When the lookup is performed the resulting request will + include parm0=107 and parm1=42.