protected boolean populateQuantityBaseRuleCollection(EntityManager em, DataDTOToMVELTranslator translator, String entityKey,
String fieldService, String jsonPropertyValue,
Collection<QuantityBasedRule> criteriaList, Class<?> memberType) {
boolean dirty = false;
if (!StringUtils.isEmpty(jsonPropertyValue)) {
DataWrapper dw = convertJsonToDataWrapper(jsonPropertyValue);
if (dw != null && StringUtils.isEmpty(dw.getError())) {
List<QuantityBasedRule> updatedRules = new ArrayList<QuantityBasedRule>();
for (DataDTO dto : dw.getData()) {
if (dto.getId() != null && !CollectionUtils.isEmpty(criteriaList)) {
checkId: {
//updates are comprehensive, even data that was not changed
//is submitted here
//Update Existing Criteria
for (QuantityBasedRule quantityBasedRule : criteriaList) {
//make compatible with enterprise module
Long sandBoxVersionId = sandBoxHelper.getSandBoxVersionId(em, quantityBasedRule.getClass(), dto.getId());
if (sandBoxVersionId == null) {
sandBoxVersionId = dto.getId();
}
if (sandBoxVersionId.equals(quantityBasedRule.getId())){
//don't update if the data has not changed
if (!quantityBasedRule.getQuantity().equals(dto.getQuantity())) {
quantityBasedRule.setQuantity(dto.getQuantity());
dirty = true;
}
try {
String mvel = translator.createMVEL(entityKey, dto,
ruleBuilderFieldServiceFactory.createInstance(fieldService));
if (!quantityBasedRule.getMatchRule().equals(mvel)) {
quantityBasedRule.setMatchRule(mvel);
dirty = true;
}
} catch (MVELTranslationException e) {
throw new RuntimeException(e);
}
//make compatible with enterprise module
em.flush();
updatedRules.add(quantityBasedRule);
break checkId;
}
}
throw new IllegalArgumentException("Unable to update the rule of type (" + memberType.getName() +
") because an update was requested for id (" + dto.getId() + "), which does not exist.");
}
} else {
//Create a new Criteria
QuantityBasedRule quantityBasedRule;
try {
quantityBasedRule = (QuantityBasedRule) memberType.newInstance();
quantityBasedRule.setQuantity(dto.getQuantity());
quantityBasedRule.setMatchRule(translator.createMVEL(entityKey, dto,
ruleBuilderFieldServiceFactory.createInstance(fieldService)));
if (StringUtils.isEmpty(quantityBasedRule.getMatchRule()) && !StringUtils.isEmpty(dw.getRawMvel())) {
quantityBasedRule.setMatchRule(dw.getRawMvel());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
sandBoxHelper.setupSandBoxState(quantityBasedRule, em);