Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved: Use FlexibleStringExpander for field parameter names.
(OFBIZ-11330)

Allows generation of unique names when repeating rendering of a form on a screen.
Added test cases to ensure parameter names can be built using FlexibleStringExpander.
  • Loading branch information
danwatford authored and nmalin committed Jan 29, 2020
1 parent 91fa4a9 commit 52e21a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Expand Up @@ -136,7 +136,7 @@ public static ModelFormField from(ModelFormFieldBuilder builder) {
protected final String name;
private final List<UpdateArea> onChangeUpdateAreas;
private final List<UpdateArea> onClickUpdateAreas;
protected final String parameterName;
protected final FlexibleStringExpander parameterName;
private final Integer position;
private final String redWhen;
private final Boolean requiredField;
Expand Down Expand Up @@ -536,7 +536,7 @@ public List<UpdateArea> getOnClickUpdateAreas() {
return onClickUpdateAreas;
}

public String getParameterName() {
public FlexibleStringExpander getParameterName() {
return parameterName;
}

Expand All @@ -549,7 +549,7 @@ public String getParameterName() {
public String getParameterName(Map<String, ? extends Object> context) {
String baseName;
if (UtilValidate.isNotEmpty(this.parameterName)) {
baseName = this.parameterName;
baseName = this.parameterName.expandString(context);
} else {
baseName = this.name;
}
Expand Down Expand Up @@ -1902,7 +1902,7 @@ public int getOtherFieldSize() {
public String getParameterNameOther(Map<String, Object> context) {
String baseName;
if (UtilValidate.isNotEmpty(getModelFormField().parameterName)) {
baseName = getModelFormField().parameterName;
baseName = getModelFormField().parameterName.expandString(context);
} else {
baseName = getModelFormField().name;
}
Expand Down
Expand Up @@ -90,7 +90,7 @@ public class ModelFormFieldBuilder {
private String name = "";
private List<UpdateArea> onChangeUpdateAreas = new ArrayList<>();
private List<UpdateArea> onClickUpdateAreas = new ArrayList<>();
private String parameterName = "";
private FlexibleStringExpander parameterName = FlexibleStringExpander.getInstance("");
private Integer position = null;
private String redWhen = "";
private Boolean requiredField = null;
Expand Down Expand Up @@ -172,7 +172,7 @@ public ModelFormFieldBuilder(Element fieldElement, ModelForm modelForm, ModelRea
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()) {
Expand Down Expand Up @@ -445,7 +445,7 @@ public List<UpdateArea> getOnClickUpdateAreas() {
return onClickUpdateAreas;
}

public String getParameterName() {
public FlexibleStringExpander getParameterName() {
return parameterName;
}

Expand Down Expand Up @@ -905,7 +905,7 @@ public ModelFormFieldBuilder setName(String name) {
}

public ModelFormFieldBuilder setParameterName(String parameterName) {
this.parameterName = parameterName;
this.parameterName = FlexibleStringExpander.getInstance(parameterName);
return this;
}

Expand Down
Expand Up @@ -20,21 +20,23 @@

import static org.apache.ofbiz.widget.model.ModelFormField.from;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;

public class ModelFormFieldTest {
private HashMap<String, Object> context;

@Before
public void setUp() throws Exception {
public void setUp() {
context = new HashMap<>();
}

Expand Down Expand Up @@ -81,4 +83,19 @@ public void fieldsToRenderDuplicatesUseWhen() {
ModelFormField fA2 = from(b -> b.setName("A").setUseWhen("true"));
assertThat(getUsedField(fA0, fA1, fA2), containsInAnyOrder(fA0, fA1));
}

@Test
public void fieldUsesFlexibleParameterName() {
ModelFormField field = from(b -> b.setParameterName("${prefix}Param"));
assertThat(field.getParameterName(ImmutableMap.of("prefix", "P1")), equalTo("P1Param"));
assertThat(field.getParameterName(ImmutableMap.of("prefix", "P2")), equalTo("P2Param"));
}

@Test
public void dropDownFieldUsesFlexibleParameterNameOther() {
ModelFormField field = from(b -> b.setParameterName("${prefix}Param"));
ModelFormField.DropDownField dropDownField = new ModelFormField.DropDownField(field);
assertThat(dropDownField.getParameterNameOther(ImmutableMap.of("prefix", "P1")), equalTo("P1Param_OTHER"));
assertThat(dropDownField.getParameterNameOther(ImmutableMap.of("prefix", "P2")), equalTo("P2Param_OTHER"));
}
}

0 comments on commit 52e21a3

Please sign in to comment.