1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.rule; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
21 | |
import org.kuali.rice.kew.api.rule.RuleExpressionContract; |
22 | |
import org.kuali.rice.kew.engine.RouteContext; |
23 | |
import org.kuali.rice.kew.api.exception.WorkflowException; |
24 | |
|
25 | |
import java.text.ParseException; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class MetaRuleExpression extends AccumulatingBSFRuleExpression { |
46 | 0 | private static final Logger LOG = Logger.getLogger(MetaRuleExpression.class); |
47 | |
|
48 | |
@Override |
49 | |
public RuleExpressionResult evaluate(Rule rule, RouteContext context) { |
50 | 0 | org.kuali.rice.kew.api.rule.RuleContract ruleDefinition = rule.getDefinition(); |
51 | 0 | RuleExpressionContract exprDef = ruleDefinition.getRuleExpressionDef(); |
52 | 0 | if (exprDef == null) { |
53 | 0 | throw new RiceIllegalStateException("No expression defined in rule definition: " + ruleDefinition); |
54 | |
} |
55 | 0 | String expression = exprDef.getExpression(); |
56 | 0 | if (StringUtils.isEmpty(expression)) { |
57 | 0 | throw new RiceIllegalStateException("Empty expression in rule definition: " + ruleDefinition); |
58 | |
} |
59 | |
|
60 | 0 | String lang = parseLang(ruleDefinition.getRuleExpressionDef().getType(), null); |
61 | 0 | if (lang == null) { |
62 | |
|
63 | 0 | return evaluateBuiltinExpression(expression, rule, context); |
64 | |
} else { |
65 | 0 | return super.evaluate(rule, context); |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private RuleExpressionResult evaluateBuiltinExpression(String expression, Rule rule, RouteContext context) { |
78 | |
try { |
79 | 0 | KRAMetaRuleEngine engine = new KRAMetaRuleEngine(expression); |
80 | |
|
81 | 0 | int responsibilityPriority = 0; |
82 | 0 | RuleExpressionResult result = null; |
83 | 0 | boolean success = false; |
84 | 0 | List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>(); |
85 | 0 | while (!engine.isDone()) { |
86 | 0 | result = engine.processSingleStatement(context); |
87 | 0 | if (result.isSuccess() && result.getResponsibilities() != null) { |
88 | |
|
89 | |
|
90 | 0 | for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility: result.getResponsibilities()) { |
91 | 0 | org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder = |
92 | |
org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility); |
93 | 0 | builder.setPriority(Integer.valueOf(responsibilityPriority)); |
94 | 0 | responsibilities.add(builder.build()); |
95 | 0 | } |
96 | |
|
97 | 0 | responsibilityPriority++; |
98 | 0 | success = true; |
99 | |
} |
100 | |
} |
101 | 0 | result = new RuleExpressionResult(rule, success, responsibilities); |
102 | 0 | LOG.debug("MetaRuleExpression returning result: " + result); |
103 | 0 | return result; |
104 | 0 | } catch (ParseException pe) { |
105 | 0 | throw new RiceIllegalStateException("Error parsing expression", pe); |
106 | |
} |
107 | |
} |
108 | |
} |