0

I have an original Cayenne Expression

(effectiveDate >= 01/01/2015) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))

There is a util method in my codebase which converts above expression to a HashMap. I traverse the map and convert to JSON format and feed that JSON to jquery QueryBuilder. I change the JSON in UI layer and using Jackson get the JSON into a HashMap The HashMap sysout is as below

{condition=AND, rules=[{id=effectiveDate, field=effectiveDate, type=date, input=text, operator=greater_or_equal, value=04/05/2016}, {condition=AND, rules=[{id=specialFeaturesString, field=specialFeaturesString, type=string, input=text, operator=contains, value="*808*"}, {condition=OR, rules=[{id=amortizationType, field=amortizationType, type=string, input=select, operator=equal, value=05}, {id=amortizationType, field=amortizationType, type=string, input=select, operator=equal, value=06}]}, {id=loanType, field=loanType, type=string, input=select, operator=equal, value=2}]}]}

I need to traverse the HashMap and convert it to Cayenne Expression.

The final result should be

(effectiveDate >= 04/05/2016) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))

Please provide the code

1 Answers1

1

Here is a skeleton of a recursive parser that should get you started:

public class ExpressionParser {

    public SimpleNode parse(Map<String, Object> map) {

        SimpleNode e = expForAggregateCondition(map);

        if (e == null) {
            e = expForRule(map);
        } else {

            Collection<Map<String, Object>> rules = 
              (Collection<Map<String, Object>>) map.get("rules");
            if (rules != null) {
                for (Map<String, Object> submap : rules) {

                    SimpleNode subExp = parse(submap);
                    e.jjtAddChild(subExp, e.jjtGetNumChildren());
                }
            }
        }

        return e;
    }

    private SimpleNode expForAggregateCondition(Map<String, Object> map) {
        String condition = (String) map.get("condition");
        if (condition == null) {
            return null;
        }

        switch (condition) {
        case "AND":
            return new ASTAnd();
        case "OR":
            return new ASTOr();
        default:
            throw new IllegalArgumentException("Bad condition: " + condition);
        }
    }

    private SimpleNode expForRule(Map<String, Object> map) {
        // TODO...
    }

}

Updated the expForRule method as

  private SimpleNode expForRule(Map<String, Object> map) {
    return (SimpleNode) ExpressionFactory.matchExp((String) map.get("id"), map.get("value"));
}

This is resulting in

effectiveDate = "04/05/2016" and specialFeaturesString = "\"*808*\"" and amortizationType = "05" or amortizationType = "06" and loanType = "2"

Not appearing with brackets.

andrus_a
  • 2,528
  • 1
  • 16
  • 10