HashMap 到 cayenne 表达式
HashMap to cayenne Expression
我有一个原版 Cayenne Expression
(effectiveDate >= 01/01/2015) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))
我的代码库中有一个 util 方法可以将上述表达式转换为 HashMap。
我遍历地图并转换为 JSON 格式并将 JSON 提供给 jquery QueryBuilder。我更改 UI 层中的 JSON 并使用 Jackson 将 JSON 放入 HashMap
HashMap 系统输出如下
{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}]}]}
我需要遍历HashMap并将其转换为Cayenne Expression
最后的结果应该是
(effectiveDate >= 04/05/2016) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))
请提供验证码
这里是递归解析器的框架,可以帮助您入门:
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...
}
}
将 expForRule 方法更新为
private SimpleNode expForRule(Map<String, Object> map) {
return (SimpleNode) ExpressionFactory.matchExp((String) map.get("id"), map.get("value"));
}
这导致
effectiveDate = "04/05/2016" and specialFeaturesString = "\"*808*\"" and amortizationType = "05" or amortizationType = "06" and loanType = "2"
没有出现在括号中。
我有一个原版 Cayenne Expression
(effectiveDate >= 01/01/2015) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))
我的代码库中有一个 util 方法可以将上述表达式转换为 HashMap。 我遍历地图并转换为 JSON 格式并将 JSON 提供给 jquery QueryBuilder。我更改 UI 层中的 JSON 并使用 Jackson 将 JSON 放入 HashMap HashMap 系统输出如下
{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}]}]}
我需要遍历HashMap并将其转换为Cayenne Expression
最后的结果应该是
(effectiveDate >= 04/05/2016) and ((specialFeaturesString like "*808*") and ((amortizationType = "05") or (amortizationType = "06")) and (loanType = 2))
请提供验证码
这里是递归解析器的框架,可以帮助您入门:
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...
}
}
将 expForRule 方法更新为
private SimpleNode expForRule(Map<String, Object> map) {
return (SimpleNode) ExpressionFactory.matchExp((String) map.get("id"), map.get("value"));
}
这导致
effectiveDate = "04/05/2016" and specialFeaturesString = "\"*808*\"" and amortizationType = "05" or amortizationType = "06" and loanType = "2"
没有出现在括号中。