Search in sources :

Example 6 with DataDTO

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

the class MVELToDataWrapperTranslator method checkForInvalidSubGroup.

protected Boolean checkForInvalidSubGroup(DataDTO dataDTO) {
    Boolean invalidSubGroupFound = false;
    for (DataDTO rules : dataDTO.getRules()) {
        ArrayList<DataDTO> subRules = rules.getRules();
        if (subRules != null && subRules.size() == 2) {
            ExpressionDTO expression1 = (ExpressionDTO) subRules.get(0);
            ExpressionDTO expression2 = (ExpressionDTO) subRules.get(1);
            Boolean isBetweenDetected = false;
            Boolean isBetweenInclusiveDetected = false;
            if (expression1.getOperator().equals("GREATER_THAN") && expression2.getOperator().equals("LESS_THAN")) {
                isBetweenDetected = true;
            }
            if (expression1.getOperator().equals("GREATER_OR_EQUAL") && expression2.getOperator().equals("LESS_OR_EQUAL")) {
                isBetweenInclusiveDetected = true;
            }
            if (!isBetweenDetected && !isBetweenInclusiveDetected) {
                invalidSubGroupFound = true;
            }
        } else {
            invalidSubGroupFound = true;
        }
    }
    return invalidSubGroupFound;
}
Also used : DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) ExpressionDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)

Example 7 with DataDTO

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

the class DataDTOToMVELTranslator method buildMVEL.

protected void buildMVEL(DataDTO dataDTO, StringBuffer sb, String entityKey, String groupOperator, RuleBuilderFieldService fieldService) throws MVELTranslationException {
    BLCOperator operator = null;
    if (dataDTO instanceof ExpressionDTO) {
        operator = BLCOperator.valueOf(((ExpressionDTO) dataDTO).getOperator());
    } else {
        operator = BLCOperator.valueOf(dataDTO.getCondition());
    }
    ArrayList<DataDTO> groups = dataDTO.getRules();
    if (sb.length() != 0 && sb.charAt(sb.length() - 1) != '(' && groupOperator != null) {
        BLCOperator groupOp = BLCOperator.valueOf(groupOperator);
        switch(groupOp) {
            default:
                sb.append("&&");
                break;
            case OR:
                sb.append("||");
        }
    }
    if (dataDTO instanceof ExpressionDTO) {
        buildExpression((ExpressionDTO) dataDTO, sb, entityKey, operator, fieldService);
    } else {
        boolean includeTopLevelParenthesis = false;
        if (sb.length() != 0 || BLCOperator.NOT.equals(operator) || (sb.length() == 0 && groupOperator != null)) {
            includeTopLevelParenthesis = true;
        }
        if (BLCOperator.NOT.equals(operator)) {
            sb.append("!");
        }
        if (includeTopLevelParenthesis)
            sb.append("(");
        for (DataDTO dto : groups) {
            buildMVEL(dto, sb, entityKey, dataDTO.getCondition(), fieldService);
        }
        if (includeTopLevelParenthesis)
            sb.append(")");
    }
}
Also used : DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) ExpressionDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)

Example 8 with DataDTO

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

the class MVELToDataWrapperTranslator method createRuleData.

public DataWrapper createRuleData(Entity[] entities, String mvelProperty, String quantityProperty, String idProperty, String containedProperty, RuleBuilderFieldService fieldService) {
    if (entities == null || entities.length == 0 || mvelProperty == null) {
        return null;
    }
    DataWrapper dataWrapper = new DataWrapper();
    String mvel = null;
    try {
        for (Entity e : entities) {
            Integer qty = null;
            Long id = null;
            Long containedId = null;
            for (Property p : e.getProperties()) {
                if (mvelProperty.equals(p.getName())) {
                    mvel = p.getValue();
                }
                if (quantityProperty != null && quantityProperty.equals(p.getName())) {
                    qty = Integer.parseInt(p.getValue());
                }
                if (idProperty != null && idProperty.equals(p.getName())) {
                    id = Long.parseLong(p.getValue());
                }
                if (containedProperty != null && containedProperty.equals(p.getName())) {
                    containedId = Long.parseLong(p.getValue());
                }
            }
            if (mvel != null) {
                Group group = groupingTranslator.createGroups(mvel);
                DataDTO dataDTO = createRuleDataDTO(null, group, fieldService);
                if (dataDTO != null) {
                    dataDTO.setPk(id);
                    dataDTO.setContainedPk(containedId);
                    dataDTO.setQuantity(qty);
                    dataWrapper.getData().add(dataDTO);
                    if (group.getSubGroups().size() > 0) {
                        Boolean invalidSubGroupFound = checkForInvalidSubGroup(dataDTO);
                        if (invalidSubGroupFound) {
                            throw new MVELTranslationException(MVELTranslationException.SUB_GROUP_DETECTED, SUB_GROUP_MESSAGE);
                        }
                    }
                }
            }
        }
    } catch (MVELTranslationException e) {
        LOG.error("Unable to translate rule MVEL", e);
        dataWrapper.getData().clear();
        dataWrapper.setError(e.getLocalizedMessage());
        dataWrapper.setRawMvel(mvel);
    }
    return dataWrapper;
}
Also used : DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) Entity(org.broadleafcommerce.openadmin.dto.Entity) Group(org.broadleafcommerce.openadmin.web.rulebuilder.grouping.Group) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 9 with DataDTO

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

the class MVELToDataWrapperTranslator method createRuleDataDTO.

protected DataDTO createRuleDataDTO(DataDTO parentDTO, Group group, RuleBuilderFieldService fieldService) throws MVELTranslationException {
    DataDTO data = new DataDTO();
    if (group.getOperatorType() == null) {
        group.setOperatorType(BLCOperator.AND);
    }
    BLCOperator operator = group.getOperatorType();
    data.setCondition(operator.name());
    List<ExpressionDTO> myCriteriaList = new ArrayList<ExpressionDTO>();
    if (BLCOperator.NOT.equals(group.getOperatorType()) && group.getIsTopGroup()) {
        group = group.getSubGroups().get(0);
        group.setOperatorType(operator);
    }
    for (String phrase : group.getPhrases()) {
        appendExpression(phrase, fieldService, parentDTO, myCriteriaList);
    }
    if (!myCriteriaList.isEmpty()) {
        data.getRules().addAll(myCriteriaList);
    }
    for (Group subgroup : group.getSubGroups()) {
        DataDTO subCriteria = createRuleDataDTO(data, subgroup, fieldService);
        if (subCriteria != null && !subCriteria.getRules().isEmpty()) {
            data.getRules().add(subCriteria);
        }
    }
    if (data.getRules() != null && !data.getRules().isEmpty()) {
        return data;
    } else {
        return null;
    }
}
Also used : Group(org.broadleafcommerce.openadmin.web.rulebuilder.grouping.Group) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) ArrayList(java.util.ArrayList) ExpressionDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)

Example 10 with DataDTO

use of org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO 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));
}
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)

Aggregations

DataDTO (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO)14 ExpressionDTO (org.broadleafcommerce.openadmin.web.rulebuilder.dto.ExpressionDTO)8 DataDTOToMVELTranslator (org.broadleafcommerce.openadmin.web.rulebuilder.DataDTOToMVELTranslator)7 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)3 MVELTranslationException (org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException)3 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 QuantityBasedRule (org.broadleafcommerce.common.rule.QuantityBasedRule)2 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)2 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)2 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)2 Group (org.broadleafcommerce.openadmin.web.rulebuilder.grouping.Group)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 Field (java.lang.reflect.Field)1