1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.engine.node;
18
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Set;
22 import java.util.StringTokenizer;
23
24 import org.kuali.rice.kew.engine.RouteContext;
25 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
26 import org.kuali.rice.kew.service.KEWServiceLocator;
27 import org.kuali.rice.kew.util.Utilities;
28
29
30
31
32
33
34
35
36 public class BasicJoinEngine implements JoinEngine {
37
38 public static final String EXPECTED_JOINERS = "ExpectedJoiners";
39 public static final String ACTUAL_JOINERS = "ActualJoiners";
40
41 public void createExpectedJoinState(RouteContext context, RouteNodeInstance joinInstance, RouteNodeInstance previousNodeInstance) {
42 RouteNodeInstance splitNode = previousNodeInstance.getBranch().getSplitNode();
43 if (splitNode == null) {
44 throw new WorkflowRuntimeException("The split node retrieved from node with name '" + previousNodeInstance.getName() + "' and branch with name '" + previousNodeInstance.getBranch().getName() + "' was null");
45 }
46 for (Iterator iter = splitNode.getNextNodeInstances().iterator(); iter.hasNext();) {
47 RouteNodeInstance splitNodeNextNode = (RouteNodeInstance) iter.next();
48 splitNodeNextNode.getBranch().setJoinNode(joinInstance);
49 saveBranch(context, splitNodeNextNode.getBranch());
50 addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
51 }
52 joinInstance.setBranch(splitNode.getBranch());
53 joinInstance.setProcess(splitNode.getProcess());
54 }
55
56 public void addExpectedJoiner(RouteNodeInstance nodeInstance, Branch branch) {
57 addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
58 }
59
60 public void addActualJoiner(RouteNodeInstance nodeInstance, Branch branch) {
61 addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
62 }
63
64 private void addJoinState(RouteNodeInstance nodeInstance, Branch branch, String key) {
65 NodeState state = nodeInstance.getNodeState(key);
66 if (state == null) {
67 state = new NodeState();
68 state.setKey(key);
69 state.setValue("");
70 state.setNodeInstance(nodeInstance);
71 nodeInstance.addNodeState(state);
72 }
73 state.setValue(state.getValue()+branch.getBranchId()+",");
74 }
75
76 public boolean isJoined(RouteNodeInstance nodeInstance) {
77 NodeState expectedState = nodeInstance.getNodeState(EXPECTED_JOINERS);
78 if (expectedState == null || Utilities.isEmpty(expectedState.getValue())) {
79 return true;
80 }
81 NodeState actualState = nodeInstance.getNodeState(ACTUAL_JOINERS);
82 Set expectedSet = loadIntoSet(expectedState);
83 Set actualSet = loadIntoSet(actualState);
84 for (Iterator iterator = expectedSet.iterator(); iterator.hasNext();) {
85 String value = (String) iterator.next();
86 if (actualSet.contains(value)) {
87 iterator.remove();
88 }
89 }
90 return expectedSet.size() == 0;
91 }
92
93 private Set loadIntoSet(NodeState state) {
94 Set set = new HashSet();
95 StringTokenizer tokenizer = new StringTokenizer(state.getValue(), ",");
96 while (tokenizer.hasMoreTokens()) {
97 set.add(tokenizer.nextToken());
98 }
99 return set;
100 }
101
102 private void saveBranch(RouteContext context, Branch branch) {
103 if (!context.isSimulation()) {
104 KEWServiceLocator.getRouteNodeService().save(branch);
105 }
106 }
107
108 }