Skip to content

Commit f58211f

Browse files
Merge pull request BroadleafCommerce#1512 from BroadleafCommerce/qa-738-field-data-validation
Provide mechanism to skip validation for MVEL strings in FieldData
2 parents 8684c3d + 99a3966 commit f58211f

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

admin/broadleaf-open-admin-platform/src/main/java/org/broadleafcommerce/openadmin/web/rulebuilder/dto/FieldData.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
* #L%
1919
*/
20+
2021
package org.broadleafcommerce.openadmin.web.rulebuilder.dto;
2122

2223
import org.broadleafcommerce.common.presentation.client.SupportedFieldType;
@@ -38,6 +39,7 @@ public class FieldData {
3839
protected String options;
3940
protected SupportedFieldType fieldType;
4041
protected SupportedFieldType secondaryFieldType;
42+
protected boolean skipValidation;
4143

4244
private FieldData(Builder builder) {
4345
this.fieldLabel = builder.fieldLabel;
@@ -46,21 +48,25 @@ private FieldData(Builder builder) {
4648
this.options = builder.options;
4749
this.fieldType = builder.fieldType;
4850
this.secondaryFieldType = builder.secondaryFieldType;
51+
this.skipValidation = builder.skipValidation;
4952
}
5053

5154
public static class Builder {
55+
5256
protected String fieldLabel = null;
5357
protected String fieldName = null;
5458
protected String operators = null;
5559
protected String options = null;
5660
protected SupportedFieldType fieldType = null;
5761
protected SupportedFieldType secondaryFieldType = null;
62+
protected boolean skipValidation;
5863

5964
public FieldData build() {
6065
return new FieldData(this);
6166
}
6267

63-
public Builder() {}
68+
public Builder() {
69+
}
6470

6571
public Builder label(String fieldLabel) {
6672
this.fieldLabel = fieldLabel;
@@ -91,6 +97,11 @@ public Builder secondaryType(SupportedFieldType fieldType) {
9197
this.secondaryFieldType = fieldType;
9298
return this;
9399
}
100+
101+
public Builder skipValidation(boolean skipValidation) {
102+
this.skipValidation = skipValidation;
103+
return this;
104+
}
94105
}
95106

96107
public String getFieldLabel() {
@@ -116,4 +127,13 @@ public SupportedFieldType getFieldType() {
116127
public SupportedFieldType getSecondaryFieldType() {
117128
return secondaryFieldType;
118129
}
130+
131+
public boolean getSkipValidation() {
132+
return skipValidation;
133+
}
134+
135+
public void setSkipValidation(boolean skipValidation) {
136+
this.skipValidation = skipValidation;
137+
}
138+
119139
}

admin/broadleaf-open-admin-platform/src/main/java/org/broadleafcommerce/openadmin/web/rulebuilder/service/AbstractRuleBuilderFieldService.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
* #L%
1919
*/
20+
2021
package org.broadleafcommerce.openadmin.web.rulebuilder.service;
2122

2223
import org.apache.commons.lang.ArrayUtils;
@@ -77,14 +78,14 @@ public FieldWrapper buildFields() {
7778
for (FieldData field : getFields()) {
7879
FieldDTO fieldDTO = new FieldDTO();
7980
fieldDTO.setLabel(field.getFieldLabel());
80-
81+
8182
//translate the label to display
8283
String label = field.getFieldLabel();
8384
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
8485
MessageSource messages = context.getMessageSource();
8586
label = messages.getMessage(label, null, label, context.getJavaLocale());
8687
fieldDTO.setLabel(label);
87-
88+
8889
fieldDTO.setName(field.getFieldName());
8990
fieldDTO.setOperators(field.getOperators());
9091
fieldDTO.setOptions(field.getOptions());
@@ -99,7 +100,7 @@ public SupportedFieldType getSupportedFieldType(String fieldName) {
99100
SupportedFieldType type = null;
100101
if (fieldName != null) {
101102
for (FieldData field : getFields()) {
102-
if (fieldName.equals(field.getFieldName())){
103+
if (fieldName.equals(field.getFieldName())) {
103104
return field.getFieldType();
104105
}
105106
}
@@ -112,7 +113,7 @@ public SupportedFieldType getSecondaryFieldType(String fieldName) {
112113
SupportedFieldType type = null;
113114
if (fieldName != null) {
114115
for (FieldData field : getFields()) {
115-
if (fieldName.equals(field.getFieldName())){
116+
if (fieldName.equals(field.getFieldName())) {
116117
return field.getSecondaryFieldType();
117118
}
118119
}
@@ -155,7 +156,8 @@ public List<FieldData> getFields() {
155156
@Override
156157
@SuppressWarnings("unchecked")
157158
public void setFields(final List<FieldData> fields) {
158-
List<FieldData> proxyFields = (List<FieldData>) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{List.class}, new InvocationHandler() {
159+
List<FieldData> proxyFields = (List<FieldData>) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { List.class }, new InvocationHandler() {
160+
159161
@Override
160162
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
161163
if (method.getName().equals("add")) {
@@ -174,20 +176,22 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
174176
}
175177

176178
private void testFieldName(FieldData fieldData) throws ClassNotFoundException {
177-
if (!StringUtils.isEmpty(fieldData.getFieldName()) && dynamicEntityDao != null) {
178-
Class<?>[] dtos = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(Class.forName(getDtoClassName()));
179-
if (ArrayUtils.isEmpty(dtos)) {
180-
dtos = new Class<?>[]{Class.forName(getDtoClassName())};
181-
}
182-
Field field = null;
183-
for (Class<?> dto : dtos) {
184-
field = dynamicEntityDao.getFieldManager().getField(dto, fieldData.getFieldName());
185-
if (field != null) {
186-
break;
179+
if (!fieldData.getSkipValidation()) {
180+
if (!StringUtils.isEmpty(fieldData.getFieldName()) && dynamicEntityDao != null) {
181+
Class<?>[] dtos = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(Class.forName(getDtoClassName()));
182+
if (ArrayUtils.isEmpty(dtos)) {
183+
dtos = new Class<?>[] { Class.forName(getDtoClassName()) };
184+
}
185+
Field field = null;
186+
for (Class<?> dto : dtos) {
187+
field = dynamicEntityDao.getFieldManager().getField(dto, fieldData.getFieldName());
188+
if (field != null) {
189+
break;
190+
}
191+
}
192+
if (field == null) {
193+
throw new IllegalArgumentException("Unable to find the field declared in FieldData (" + fieldData.getFieldName() + ") on the target class (" + getDtoClassName() + "), or any registered entity class that derives from it.");
187194
}
188-
}
189-
if (field == null) {
190-
throw new IllegalArgumentException("Unable to find the field declared in FieldData (" + fieldData.getFieldName() + ") on the target class (" + getDtoClassName() + "), or any registered entity class that derives from it.");
191195
}
192196
}
193197
}
@@ -221,7 +225,7 @@ public void afterPropertiesSet() throws Exception {
221225
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager(TargetModeType.SANDBOX);
222226
dynamicEntityDao = persistenceManager.getDynamicEntityDao();
223227
setFields(new ArrayList<FieldData>());
224-
228+
225229
// This cannot be null during startup as we do not want to remove the null safety checks in a multi-tenant env.
226230
boolean contextWasNull = false;
227231
if (BroadleafRequestContext.getBroadleafRequestContext() == null) {

common/src/main/java/org/broadleafcommerce/common/RequestDTOImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
* limitations under the License.
1818
* #L%
1919
*/
20+
2021
package org.broadleafcommerce.common;
2122

2223
import org.apache.commons.lang3.StringUtils;
2324
import org.broadleafcommerce.common.presentation.AdminPresentation;
25+
import org.broadleafcommerce.common.web.BroadleafRequestContext;
2426
import org.springframework.web.context.request.WebRequest;
2527

2628
import java.io.Serializable;
29+
import java.util.Map;
2730

2831
import javax.servlet.http.HttpServletRequest;
2932

@@ -37,7 +40,6 @@ public class RequestDTOImpl implements RequestDTO, Serializable {
3740
@AdminPresentation(friendlyName = "RequestDTOImpl_Request_URI")
3841
private String requestURI;
3942

40-
4143
@AdminPresentation(friendlyName = "RequestDTOImpl_Full_Url")
4244
private String fullUrlWithQueryString;
4345

@@ -102,4 +104,12 @@ public void setRequestURI(String requestURI) {
102104
this.requestURI = requestURI;
103105
}
104106

107+
public Map<String, Object> getProperties() {
108+
if (BroadleafRequestContext.getBroadleafRequestContext() != null) {
109+
return BroadleafRequestContext.getBroadleafRequestContext().getAdditionalProperties();
110+
} else {
111+
return null;
112+
}
113+
}
114+
105115
}

0 commit comments

Comments
 (0)