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