2016-04-05 12 views
0

Ich habe eine original Cayenne ExpressionHashMap nach Cayenne Expression

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

Es gibt eine util Methode in meiner Code-Basis, die zu einem HashMap obigen Ausdruck umwandelt. Ich durchquere die Karte und konvertiere in das JSON-Format und führe diesen JSON in jquery QueryBuilder ein. Ich die JSON ändern in UI-Ebene und mit Jackson die JSON in eine HashMap Die HashMap SYSOUT ist wie unten

{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}]}]} 

Ich brauche die HashMap zu durchqueren und wandeln es Expression nach Cayenne.

sollte das Endergebnis

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

Bitte geben Sie den Code

Antwort

0

Hier ist ein Skelett eines rekursiven Parser, der Ihnen den Einstieg sollten:

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... 
    } 

} 

die expForRule Methode Aktualisiert als

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

wird, was in

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

Nicht mit Klammern erscheinen.