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 public class MetaRuleExpression extends AccumulatingBSFRuleExpression {
46 private static final Logger LOG = Logger.getLogger(MetaRuleExpression.class);
47
48 @Override
49 public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
50 org.kuali.rice.kew.api.rule.RuleContract ruleDefinition = rule.getDefinition();
51 RuleExpressionContract exprDef = ruleDefinition.getRuleExpressionDef();
52 if (exprDef == null) {
53 throw new RiceIllegalStateException("No expression defined in rule definition: " + ruleDefinition);
54 }
55 String expression = exprDef.getExpression();
56 if (StringUtils.isEmpty(expression)) {
57 throw new RiceIllegalStateException("Empty expression in rule definition: " + ruleDefinition);
58 }
59
60 String lang = parseLang(ruleDefinition.getRuleExpressionDef().getType(), null);
61 if (lang == null) {
62
63 return evaluateBuiltinExpression(expression, rule, context);
64 } else {
65 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 KRAMetaRuleEngine engine = new KRAMetaRuleEngine(expression);
80
81 int responsibilityPriority = 0;
82 RuleExpressionResult result = null;
83 boolean success = false;
84 List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>();
85 while (!engine.isDone()) {
86 result = engine.processSingleStatement(context);
87 if (result.isSuccess() && result.getResponsibilities() != null) {
88
89
90 for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility: result.getResponsibilities()) {
91 org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder =
92 org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility);
93 builder.setPriority(Integer.valueOf(responsibilityPriority));
94 responsibilities.add(builder.build());
95 }
96
97 responsibilityPriority++;
98 success = true;
99 }
100 }
101 result = new RuleExpressionResult(rule, success, responsibilities);
102 LOG.debug("MetaRuleExpression returning result: " + result);
103 return result;
104 } catch (ParseException pe) {
105 throw new RiceIllegalStateException("Error parsing expression", pe);
106 }
107 }
108 }