Search in sources :

Example 6 with DataDTOToMVELTranslator

use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.

the class DataDTOToMVELTranslatorTest method testCustomerQualificationMVEL.

/**
 * Tests the creation of a Customer Qualification MVEL expression from a DataDTO
 * @throws MVELTranslationException
 *
 * [{"pk":null,
 *  "quantity":null,
 *  "condition":"AND",
 *  "rules":[
 *      {"pk":null,
 *      "quantity":null,
 *      "condition":null,
 *      "rules":null,
 *      "id":"emailAddress",
 *      "operator":"NOT_EQUAL_FIELD",
 *      "value":"username"}]
 *  }]
 */
public void testCustomerQualificationMVEL() throws MVELTranslationException {
    DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
    DataDTO dataDTO = new DataDTO();
    dataDTO.setCondition(BLCOperator.AND.name());
// not currently supported
// ExpressionDTO e1 = new ExpressionDTO();
// e1.setName("emailAddress");
// e1.setOperator(BLCOperator.NOT_EQUAL_FIELD.name());
// e1.setValue("username");
// Not supported
// ExpressionDTO e2 = new ExpressionDTO();
// e2.setName("deactivated");
// e2.setOperator(BLCOperator.EQUALS.name());
// e2.setValue("true");
// dataDTO.getGroups().add(e1);
// dataDTO.getGroups().add(e2);
// Not supported
// String translated = translator.createMVEL("customer", dataDTO, customerFieldService);
// String mvel = "customer.?deactivated==true";
// assert (mvel.equals(translated));
}
Also used : DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) DataDTOToMVELTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator)

Example 7 with DataDTOToMVELTranslator

use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.

the class DataDTOToMVELTranslatorTest method testItemQualificationCollectionMVEL.

public void testItemQualificationCollectionMVEL() 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.COLLECTION_IN.name());
    d1e1.setValue("[\"test category\", \"test category 2\"]");
    d1.getRules().add(d1e1);
    String d1Translated = translator.createMVEL("discreteOrderItem", d1, orderItemFieldService);
    String d1Mvel = "CollectionUtils.intersection(discreteOrderItem.?category.?name,[\"test category\", \"test category 2\"]).size()>0";
    assert (d1Mvel.equals(d1Translated));
}
Also used : DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) DataDTOToMVELTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator) ExpressionDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)

Example 8 with DataDTOToMVELTranslator

use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldPersistenceProvider method populateQuantityRule.

protected boolean populateQuantityRule(PopulateValueRequest populateValueRequest, Serializable instance) throws FieldNotAvailableException, IllegalAccessException {
    String prop = populateValueRequest.getProperty().getName();
    Field field = populateValueRequest.getFieldManager().getField(instance.getClass(), prop);
    OneToMany oneToMany = field.getAnnotation(OneToMany.class);
    if (oneToMany == null) {
        throw new UnsupportedOperationException("RuleFieldPersistenceProvider is currently only compatible with collection fields when modelled using @OneToMany");
    }
    // currently, this only works with Collection fields
    boolean dirty;
    Class<?> valueType = getListFieldType(instance, populateValueRequest.getFieldManager(), populateValueRequest.getProperty(), populateValueRequest.getPersistenceManager());
    if (valueType == null) {
        throw new IllegalAccessException("Unable to determine the valueType for the rule field (" + populateValueRequest.getProperty().getName() + ")");
    }
    DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
    Collection<QuantityBasedRule> rules;
    rules = (Collection<QuantityBasedRule>) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
    Object parent = extractParent(populateValueRequest, instance);
    // AntiSamy HTML encodes the rule JSON - pass the unHTMLEncoded version
    EntityManager entityManager = populateValueRequest.getPersistenceManager().getDynamicEntityDao().getStandardEntityManager();
    String fieldService = populateValueRequest.getMetadata().getRuleIdentifier();
    String entityKey = RuleIdentifier.ENTITY_KEY_MAP.get(fieldService);
    Property ruleProperty = populateValueRequest.getProperty();
    String jsonPropertyValue = ruleProperty.getUnHtmlEncodedValue();
    String mappedByEntity = oneToMany.mappedBy();
    dirty = updateQuantityRule(entityManager, translator, entityKey, fieldService, jsonPropertyValue, rules, valueType, parent, mappedByEntity, ruleProperty);
    return dirty;
}
Also used : OneToMany(javax.persistence.OneToMany) DataDTOToMVELTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator) Field(java.lang.reflect.Field) EntityManager(javax.persistence.EntityManager) QuantityBasedRule(org.broadleafcommerce.common.rule.QuantityBasedRule) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 9 with DataDTOToMVELTranslator

use of org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldValidator method validate.

@Override
public PropertyValidationResult validate(PopulateValueRequest populateValueRequest, Serializable instance) {
    if (canHandleValidation(populateValueRequest)) {
        DataDTOToMVELTranslator translator = new DataDTOToMVELTranslator();
        EntityManager em = populateValueRequest.getPersistenceManager().getDynamicEntityDao().getStandardEntityManager();
        if (SupportedFieldType.RULE_SIMPLE.equals(populateValueRequest.getMetadata().getFieldType()) || SupportedFieldType.RULE_SIMPLE_TIME.equals(populateValueRequest.getMetadata().getFieldType())) {
            // AntiSamy HTML encodes the rule JSON - pass the unHTMLEncoded version
            DataWrapper dw = ruleFieldExtractionUtility.convertJsonToDataWrapper(populateValueRequest.getProperty().getUnHtmlEncodedValue());
            if (dw != null && StringUtils.isNotEmpty(dw.getError())) {
                return new PropertyValidationResult(false, "Could not serialize JSON from rule builder: " + dw.getError());
            }
            if (dw == null || StringUtils.isEmpty(dw.getError())) {
                try {
                    String mvel = ruleFieldExtractionUtility.convertSimpleMatchRuleJsonToMvel(translator, RuleIdentifier.ENTITY_KEY_MAP.get(populateValueRequest.getMetadata().getRuleIdentifier()), populateValueRequest.getMetadata().getRuleIdentifier(), dw);
                } catch (MVELTranslationException e) {
                    return new PropertyValidationResult(false, getMvelParsingErrorMesage(dw, e));
                }
            }
        }
        if (SupportedFieldType.RULE_WITH_QUANTITY.equals(populateValueRequest.getMetadata().getFieldType())) {
            Collection<QuantityBasedRule> existingRules;
            try {
                existingRules = (Collection<QuantityBasedRule>) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
            } catch (FieldNotAvailableException e) {
                return new PropertyValidationResult(false, "Could not access rule field on Java object to set values");
            } catch (IllegalAccessException e) {
                return new PropertyValidationResult(false, "Could not access rule field on Java object to set values");
            }
            String entityKey = RuleIdentifier.ENTITY_KEY_MAP.get(populateValueRequest.getMetadata().getRuleIdentifier());
            String jsonPropertyValue = populateValueRequest.getProperty().getUnHtmlEncodedValue();
            String fieldService = populateValueRequest.getMetadata().getRuleIdentifier();
            if (!StringUtils.isEmpty(jsonPropertyValue)) {
                DataWrapper dw = ruleFieldExtractionUtility.convertJsonToDataWrapper(jsonPropertyValue);
                if (dw != null && StringUtils.isNotEmpty(dw.getError())) {
                    return new PropertyValidationResult(false, "Could not serialize JSON from rule builder: " + dw.getError());
                }
                if (dw != null && StringUtils.isEmpty(dw.getError())) {
                    for (DataDTO dto : dw.getData()) {
                        if (dto.getPk() != null) {
                            boolean foundIdToUpdate = false;
                            for (QuantityBasedRule quantityBasedRule : existingRules) {
                                Long sandBoxVersionId = sandBoxHelper.getSandBoxVersionId(quantityBasedRule.getClass(), dto.getPk());
                                if (sandBoxVersionId == null) {
                                    sandBoxVersionId = dto.getPk();
                                }
                                if (sandBoxVersionId.equals(quantityBasedRule.getId()) || sandBoxHelper.isRelatedToParentCatalogIds(quantityBasedRule, dto.getPk())) {
                                    foundIdToUpdate = true;
                                    try {
                                        String mvel = ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService);
                                    } catch (MVELTranslationException e) {
                                        return new PropertyValidationResult(false, getMvelParsingErrorMesage(dw, e));
                                    }
                                }
                            }
                            if (!foundIdToUpdate) {
                                return new PropertyValidationResult(false, "Tried to update QuantityBasedRule with ID " + dto.getPk() + " but that rule does not exist");
                            }
                        } else {
                            // This is a new rule, just validate that it parses successfully
                            try {
                                ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService);
                            } catch (MVELTranslationException e) {
                                return new PropertyValidationResult(false, getMvelParsingErrorMesage(dw, e));
                            }
                        }
                    }
                }
            }
        }
    }
    return new PropertyValidationResult(true);
}
Also used : MVELTranslationException(org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) DataDTOToMVELTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator) DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) EntityManager(javax.persistence.EntityManager) QuantityBasedRule(org.broadleafcommerce.common.rule.QuantityBasedRule) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)

Aggregations

DataDTOToMVELTranslator (org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator)9 DataDTO (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO)7 ExpressionDTO (org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)5 EntityManager (javax.persistence.EntityManager)3 Field (java.lang.reflect.Field)2 OneToMany (javax.persistence.OneToMany)2 QuantityBasedRule (org.broadleafcommerce.common.rule.QuantityBasedRule)2 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)2 MVELTranslationException (org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException)2 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 SimpleRule (org.broadleafcommerce.common.rule.SimpleRule)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 Property (org.broadleafcommerce.openadmin.dto.Property)1 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)1 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)1