use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.
the class RuleFieldPersistenceProvider method populateSimpleRule.
protected boolean populateSimpleRule(PopulateValueRequest populateValueRequest, Serializable instance) throws Exception {
boolean dirty = false;
String prop = populateValueRequest.getProperty().getName();
if (prop.contains(FieldManager.MAPFIELDSEPARATOR)) {
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop.substring(0, prop.indexOf(FieldManager.MAPFIELDSEPARATOR)));
if (field.getAnnotation(OneToMany.class) == null) {
throw new UnsupportedOperationException("RuleFieldPersistenceProvider is currently only compatible with map fields when modelled using @OneToMany");
}
}
DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
// AntiSamy HTML encodes the rule JSON - pass the unHTMLEncoded version
DataWrapper dw = ruleFieldExtractionUtility.convertJsonToDataWrapper(populateValueRequest.getProperty().getUnHtmlEncodedValue());
if (dw == null || StringUtils.isEmpty(dw.getError())) {
String mvel = ruleFieldExtractionUtility.convertSimpleMatchRuleJsonToMvel(translator, RuleIdentifier.ENTITY_KEY_MAP.get(populateValueRequest.getMetadata().getRuleIdentifier()), populateValueRequest.getMetadata().getRuleIdentifier(), dw);
Class<?> valueType = getStartingValueType(populateValueRequest);
// This is a simple String field (or String map field)
if (String.class.isAssignableFrom(valueType)) {
// first check if the property is null and the mvel is null
if (instance != null && mvel == null) {
Object value = populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
dirty = value != null;
} else {
dirty = checkDirtyState(populateValueRequest, instance, mvel);
}
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), mvel);
}
if (SimpleRule.class.isAssignableFrom(valueType)) {
boolean persist = false;
SimpleRule rule;
try {
rule = (SimpleRule) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
if (rule == null) {
rule = (SimpleRule) valueType.newInstance();
Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop.substring(0, prop.indexOf(FieldManager.MAPFIELDSEPARATOR)));
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
Object parent = extractParent(populateValueRequest, instance);
populateValueRequest.getFieldManager().setFieldValue(rule, oneToMany.mappedBy(), parent);
populateValueRequest.getFieldManager().setFieldValue(rule, populateValueRequest.getMetadata().getMapKeyValueProperty(), prop.substring(prop.indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), prop.length()));
persist = true;
}
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
if (mvel == null) {
// cause the rule to be deleted
dirty = populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName()) != null;
if (dirty) {
if (!populateValueRequest.getProperty().getName().contains(FieldManager.MAPFIELDSEPARATOR)) {
populateValueRequest.getFieldManager().setFieldValue(instance, populateValueRequest.getProperty().getName(), null);
} else {
// Since this class explicitly removes the simple rule - we must also preserve the id of the element
// as the CacheInvalidationProducer will need this in order to remove the member cache instance as well.
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
context.getAdditionalProperties().put("deletedSimpleRule", rule);
populateValueRequest.getPersistenceManager().getDynamicEntityDao().remove(rule);
}
}
} else if (rule != null) {
dirty = !mvel.equals(rule.getMatchRule());
if (!dirty && extensionManager != null) {
ExtensionResultHolder<Boolean> resultHolder = new ExtensionResultHolder<Boolean>();
ExtensionResultStatusType result = extensionManager.getProxy().establishDirtyState(rule, resultHolder);
if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
dirty = resultHolder.getResult();
}
}
if (dirty) {
updateSimpleRule(populateValueRequest, mvel, persist, rule);
EntityManager em = populateValueRequest.getPersistenceManager().getDynamicEntityDao().getStandardEntityManager();
Long id = getRuleId(rule, em);
Long containedId = getContainedRuleId(rule, em);
DataDTO dto = dw.getData().get(0);
if (persist && cascadeExtensionManager != null) {
ExtensionResultHolder resultHolder = new ExtensionResultHolder();
cascadeExtensionManager.getProxy().postCascadeAdd(rule, dto, resultHolder);
}
dto.setPk(id);
dto.setContainedPk(containedId);
ObjectMapper mapper = new ObjectMapper();
String json;
try {
json = mapper.writeValueAsString(dw);
} catch (Exception e) {
throw new RuntimeException(e);
}
populateValueRequest.getProperty().setValue(json);
}
}
}
}
return dirty;
}
use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.
the class DataDTOToMVELTranslatorTest method testCreateMVEL.
/**
* Tests the creation of an MVEL expression from a DataDTO
* @throws MVELTranslationException
*
* Here's an example of a DataWrapper with a single DataDTO
*
* [{"pk":"100",
* "quantity":"1",
* "condition":"AND",
* "rules":[
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"category.name",
* "operator":"IEQUALS",
* "value":"merchandise"}]
* }]
*/
public void testCreateMVEL() throws MVELTranslationException {
DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
ExpressionDTO expressionDTO = new ExpressionDTO();
expressionDTO.setId("category.name");
expressionDTO.setOperator(BLCOperator.IEQUALS.name());
expressionDTO.setValue("merchandise");
String translated = translator.createMVEL("discreteOrderItem", expressionDTO, orderItemFieldService);
String mvel = "MvelHelper.toUpperCase(discreteOrderItem.?category.?name)==MvelHelper.toUpperCase(\"merchandise\")";
assert (mvel.equals(translated));
}
use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.
the class DataDTOToMVELTranslatorTest method testFulfillmentQualificationMVEL.
/**
* Tests the creation of a Fulfillment Group Qualification MVEL expression from a DataDTO
* @throws MVELTranslationException
*
* [{"pk":null,
* "quantity":null,
* "condition":"AND",
* "rules":[
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"address.state.name",
* "operator":"EQUALS",
* "value":["Texas"]},
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"retailShippingPrice",
* "operator":"BETWEEN_INCLUSIVE",
* "value":"[99,199]"}]
* }]
*/
public void testFulfillmentQualificationMVEL() throws MVELTranslationException {
DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
DataDTO dataDTO = new DataDTO();
dataDTO.setCondition(BLCOperator.AND.name());
ExpressionDTO e1 = new ExpressionDTO();
e1.setId("address.state.name");
e1.setOperator(BLCOperator.EQUALS.name());
e1.setValue("Texas");
ExpressionDTO e2 = new ExpressionDTO();
e2.setId("retailFulfillmentPrice");
e2.setOperator(BLCOperator.BETWEEN_INCLUSIVE.name());
e2.setValue("[99,199]");
dataDTO.getRules().add(e1);
dataDTO.getRules().add(e2);
String translated = translator.createMVEL("fulfillmentGroup", dataDTO, fulfillmentGroupFieldService);
String mvel = "fulfillmentGroup.?address.?state.?name==\"Texas\"&&(fulfillmentGroup.?retailFulfillmentPrice.getAmount()>=99&&fulfillmentGroup.?retailFulfillmentPrice.getAmount()<=199)";
assert (mvel.equals(translated));
}
use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.
the class DataDTOToMVELTranslatorTest method testOrderQualificationMVEL.
/**
* Tests the creation of an Order Qualification MVEL expression from a DataDTO
* @throws MVELTranslationException
*
* [{"pk":null,
* "quantity":null,
* "condition":"AND",
* "rules":[
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"subTotal",
* "operator":"GREATER_OR_EQUAL",
* "value":"100"}]
* }]
*/
public void testOrderQualificationMVEL() throws MVELTranslationException {
DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
DataDTO dataDTO = new DataDTO();
dataDTO.setCondition(BLCOperator.AND.name());
ExpressionDTO expressionDTO = new ExpressionDTO();
expressionDTO.setId("subTotal");
expressionDTO.setOperator(BLCOperator.GREATER_OR_EQUAL.name());
expressionDTO.setValue("100");
dataDTO.getRules().add(expressionDTO);
String translated = translator.createMVEL("order", dataDTO, orderFieldService);
String mvel = "order.?subTotal.getAmount()>=100";
assert (mvel.equals(translated));
}
use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.
the class DataDTOToMVELTranslatorTest method testItemQualificationMVEL.
/**
* Tests the creation of an Item Qualification MVEL expression from a DataDTO
* @throws MVELTranslationException
*
* [{"pk":100,
* "quantity":1,
* "condition":"AND",
* "rules":[
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"category.name",
* "operator":"EQUALS",
* "value":"test category"
* }]
* },
* {"pk":"200",
* "quantity":2,
* "condition":"NOT",
* "rules":[
* {"pk":null,
* "quantity":null,
* "condition":null,
* "rules":null,
* "id":"product.manufacturer",
* "operator":"EQUALS",
* "value":"test manufacturer"}]
* }]
*/
public void testItemQualificationMVEL() throws MVELTranslationException {
DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
DataDTO d1 = new DataDTO();
d1.setQuantity(1);
d1.setCondition(BLCOperator.AND.name());
ExpressionDTO d1e1 = new ExpressionDTO();
d1e1.setId("category.name");
d1e1.setOperator(BLCOperator.EQUALS.name());
d1e1.setValue("test category");
d1.getRules().add(d1e1);
String d1Translated = translator.createMVEL("discreteOrderItem", d1, orderItemFieldService);
String d1Mvel = "discreteOrderItem.?category.?name==\"test category\"";
assert (d1Mvel.equals(d1Translated));
DataDTO d2 = new DataDTO();
d2.setQuantity(1);
d2.setCondition(BLCOperator.NOT.name());
ExpressionDTO d2e1 = new ExpressionDTO();
d2e1.setId("product.manufacturer");
d2e1.setOperator(BLCOperator.EQUALS.name());
d2e1.setValue("test manufacturer");
d2.getRules().add(d2e1);
String d2Translated = translator.createMVEL("discreteOrderItem", d2, orderItemFieldService);
String d2Mvel = "!(discreteOrderItem.?product.?manufacturer==\"test manufacturer\")";
assert (d2Mvel.equals(d2Translated));
}
Aggregations