use of org.broadleafcommerce.common.rule.QuantityBasedRule 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);
}
Aggregations