use of org.broadleafcommerce.openadmin.web.rulebuilder.BLCOperator in project BroadleafCommerce by BroadleafCommerce.
the class PhraseTranslator method createExpression.
public Expression createExpression(String phrase) throws MVELTranslationException {
String[] components = extractComponents(phrase);
String field = components[0];
String operator = components[1];
String value = components[2];
boolean isNegation = false;
if (field.startsWith("!") || phrase.startsWith("!")) {
isNegation = true;
}
boolean isIgnoreCase = false;
boolean isCollectionCase = false;
if (phrase.contains(DataDTOToMVELTranslator.COLLECTION_OPERATOR)) {
isCollectionCase = true;
}
// remove null check syntax
field = field.replaceAll("\\.\\?", ".");
// keep for backwards compatibility with legacy generated MVEL
String legacyCaseInsensitivityKey = "MVEL.eval(\"toUpperCase()\",";
String newCaseInsensitivityKey = "MvelHelper.toUpperCase(";
String caseInsensitivityKey;
if (field.contains(legacyCaseInsensitivityKey) || value.contains(legacyCaseInsensitivityKey)) {
caseInsensitivityKey = legacyCaseInsensitivityKey;
} else {
caseInsensitivityKey = newCaseInsensitivityKey;
}
if (field.contains(caseInsensitivityKey)) {
isIgnoreCase = true;
field = field.substring(field.indexOf(caseInsensitivityKey) + caseInsensitivityKey.length(), field.length() - 1);
}
while (value.contains(caseInsensitivityKey)) {
int caseIndex = value.indexOf(caseInsensitivityKey);
value = value.substring(0, caseIndex) + value.substring(caseIndex + caseInsensitivityKey.length());
if (value.contains("\")")) {
value = value.substring(0, value.indexOf("\")") + 1) + value.substring(value.indexOf("\")") + 2);
} else {
value = value.substring(0, value.indexOf(")")) + value.substring(value.indexOf(")") + 1);
}
}
if (value.startsWith("[") && value.endsWith("]") && !isCollectionCase) {
value = value.substring(1, value.length() - 1);
String[] temps = value.split(",");
for (int j = 0; j < temps.length; j++) {
if (temps[j].startsWith("\"") && temps[j].endsWith("\"")) {
temps[j] = temps[j].substring(1, temps[j].length() - 1);
}
}
StringBuffer sb = new StringBuffer();
sb.append("[");
for (int j = 0; j < temps.length; j++) {
sb.append(temps[j]);
if (j < temps.length - 1) {
sb.append(",");
}
}
sb.append("]");
value = sb.toString();
}
// keep for backwards compatibility with legacy generated MVEL
String legacyDateFormatKey = "java.text.DateFormat.getDateTimeInstance(3,3).parse(";
String newDateFormatKey = "MvelHelper.convertField(\"DATE\",\"";
String dateFormatKey;
if (value.contains(legacyDateFormatKey)) {
dateFormatKey = legacyDateFormatKey;
} else {
dateFormatKey = newDateFormatKey;
}
if (value.startsWith(dateFormatKey)) {
value = value.substring(dateFormatKey.length(), value.length() - 1);
// convert the date into admin display format
try {
if (value.startsWith("\"")) {
value = value.substring(1, value.length());
}
if (value.endsWith("\"")) {
value = value.substring(0, value.length() - 1);
}
value = RuleBuilderFormatUtil.formatDate(RuleBuilderFormatUtil.parseDate(value));
} catch (ParseException e) {
throw new MVELTranslationException(MVELTranslationException.INCOMPATIBLE_DATE_VALUE, "Unable to convert " + "the persisted date value(" + value + ") to the admin display format.");
}
}
int entityKeyIndex = field.indexOf(".");
if (entityKeyIndex < 0) {
throw new MVELTranslationException(MVELTranslationException.NO_FIELD_FOUND_IN_RULE, "Could not identify a " + "valid property field value in the expression: (" + phrase + ")");
}
if (value.startsWith(caseInsensitivityKey)) {
value = value.substring(caseInsensitivityKey.length(), value.length() - 1);
}
String entityKey = field.substring(0, entityKeyIndex);
boolean isFieldComparison = false;
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
} else if (value.startsWith(entityKey + ".")) {
isFieldComparison = true;
value = value.substring(entityKey.length() + 1, value.length());
}
field = field.substring(entityKeyIndex + 1, field.length());
// If this is a Money field, then DataDTOToMVELTranslator.formatValue() will append .getAmount() onto the end
// of the field name. We need to remove that as it should not be considered part of the field name, but we still
// want to support other method invocations in MVEL expressions (like for getProductAttributes())
String moneyAmountMethod = ".getAmount()";
int amountMethodPos = field.lastIndexOf(moneyAmountMethod);
if (amountMethodPos >= 0) {
field = field.substring(0, amountMethodPos);
}
// Same as above, but for Enumeration types
String typeMethod = ".getType()";
int typeMethodPos = field.lastIndexOf(typeMethod);
if (typeMethodPos >= 0) {
field = field.substring(0, typeMethodPos);
}
Expression expression = new Expression();
expression.setField(field);
BLCOperator operatorId = getOperator(field, operator, value, isNegation, isFieldComparison, isIgnoreCase);
expression.setOperator(operatorId);
expression.setValue(value);
return expression;
}
Aggregations