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.log4j.Logger;
19 import org.kuali.rice.kew.api.exception.WorkflowException;
20 import org.kuali.rice.kew.engine.RouteContext;
21
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31
32
33 public class AccumulatingBSFRuleExpression extends BSFRuleExpression {
34 private static final Logger LOG = Logger.getLogger(AccumulatingBSFRuleExpression.class);
35
36 @Override
37 protected void declareBeans(ScriptEngine engine, Rule rule, RouteContext context) throws ScriptException {
38
39 super.declareBeans(engine, rule, context);
40
41 RuleHelper rh = new RuleHelper(rule, context);
42 engine.put("metarule", rh);
43 engine.put("rulehelper", rh);
44 }
45
46
47
48
49 private static final class RuleHelper {
50 private Rule rule;
51 private WorkflowRuleAPI workflow;
52
53
54
55 private List<org.kuali.rice.kew.api.rule.RuleResponsibility> responsibilities = new ArrayList<org.kuali.rice.kew.api.rule.RuleResponsibility>();
56 private int responsibilityPriority = 0;
57
58 private RuleHelper(Rule rule, RouteContext context) {
59 this.workflow = new WorkflowRuleAPI(context);
60 this.rule = rule;
61 }
62
63
64
65
66 public List<org.kuali.rice.kew.api.rule.RuleResponsibility> getResponsibilities() {
67 return responsibilities;
68 }
69
70
71
72
73
74 public RuleExpressionResult getResult(boolean success) {
75 return new RuleExpressionResult(rule, success, responsibilities);
76 }
77
78
79
80
81
82 public void accumulate(RuleExpressionResult result) {
83 if (result.getResponsibilities() == null || result.getResponsibilities().size() == 0) return;
84
85 Integer curPriority = Integer.valueOf(responsibilityPriority);
86 for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility: result.getResponsibilities()) {
87 org.kuali.rice.kew.api.rule.RuleResponsibility.Builder builder =
88 org.kuali.rice.kew.api.rule.RuleResponsibility.Builder.create(responsibility);
89 builder.setPriority(curPriority);
90 responsibilities.add(builder.build());
91 }
92
93 responsibilityPriority++;
94 }
95
96
97
98
99
100
101 public boolean evalRule(String ruleName) throws WorkflowException {
102 RuleExpressionResult result = workflow.invokeRule(ruleName);
103 accumulate(result);
104 return result.isSuccess();
105 }
106
107
108
109
110
111
112
113 public boolean evalRule(String ruleName, boolean accumOnSuccess) throws WorkflowException {
114 RuleExpressionResult result = workflow.invokeRule(ruleName);
115 if (accumOnSuccess == result.isSuccess()) {
116 accumulate(result);
117 }
118 return result.isSuccess();
119 }
120
121 }
122 }