Search in sources :

Example 1 with SimpleRule

use of org.broadleafcommerce.common.rule.SimpleRule in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldPersistenceProvider method findContainedRuleIfApplicable.

public static Object findContainedRuleIfApplicable(Object rule) {
    Object response = null;
    for (Field field : getAllFields(rule.getClass())) {
        field.setAccessible(true);
        Object test = null;
        try {
            test = field.get(rule);
        } catch (IllegalAccessException e) {
            throw ExceptionHelper.refineException(e);
        }
        if (test != null && (test instanceof SimpleRule || test instanceof QuantityBasedRule)) {
            response = test;
            break;
        }
    }
    return response;
}
Also used : Field(java.lang.reflect.Field) SimpleRule(org.broadleafcommerce.common.rule.SimpleRule) QuantityBasedRule(org.broadleafcommerce.common.rule.QuantityBasedRule)

Example 2 with SimpleRule

use of org.broadleafcommerce.common.rule.SimpleRule 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;
}
Also used : BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) OneToMany(javax.persistence.OneToMany) DataDTOToMVELTranslator(org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) MVELTranslationException(org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) Field(java.lang.reflect.Field) SimpleRule(org.broadleafcommerce.common.rule.SimpleRule) EntityManager(javax.persistence.EntityManager) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with SimpleRule

use of org.broadleafcommerce.common.rule.SimpleRule in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldPersistenceProvider method extractSimpleRule.

protected void extractSimpleRule(ExtractValueRequest extractValueRequest, Property property, ObjectMapper mapper, MVELToDataWrapperTranslator translator) {
    Property jsonProperty;
    if (extractValueRequest.getRequestedValue() != null) {
        if (extractValueRequest.getRequestedValue() instanceof String) {
            String val = (String) extractValueRequest.getRequestedValue();
            property.setValue(val);
            property.setDisplayValue(extractValueRequest.getDisplayVal());
            jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, val, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
        } else {
            Object simpleRule = extractValueRequest.getRequestedValue();
            if (simpleRule != null) {
                if (simpleRule instanceof SimpleRule) {
                    String val = ((SimpleRule) simpleRule).getMatchRule();
                    property.setValue(val);
                    property.setDisplayValue(extractValueRequest.getDisplayVal());
                    jsonProperty = convertSimpleRuleToJson(translator, mapper, (SimpleRule) simpleRule, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
                } else {
                    throw new UnsupportedOperationException("RULE_SIMPLE type is currently only supported on " + "fields of type SimpleRule");
                }
            } else {
                jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, null, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
            }
        }
    } else {
        jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, null, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
    }
    extractValueRequest.getProps().add(jsonProperty);
}
Also used : SimpleRule(org.broadleafcommerce.common.rule.SimpleRule) Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

SimpleRule (org.broadleafcommerce.common.rule.SimpleRule)3 Field (java.lang.reflect.Field)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 EntityManager (javax.persistence.EntityManager)1 OneToMany (javax.persistence.OneToMany)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 QuantityBasedRule (org.broadleafcommerce.common.rule.QuantityBasedRule)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 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)1 DataDTOToMVELTranslator (org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator)1 MVELTranslationException (org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException)1 DataDTO (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO)1 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)1