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. 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..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; @@ -122,7 +127,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 +527,7 @@ public class ModelFormField { return onClickUpdateAreas; } - public String getParameterName() { + public FlexibleStringExpander getParameterName() { return parameterName; } @@ -535,7 +540,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 +1858,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; } @@ -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/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; } 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) {