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 org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
20 import org.kuali.rice.kew.api.WorkflowRuntimeException;
21 import org.kuali.rice.kew.engine.RouteContext;
22 import org.kuali.rice.kew.service.KEWServiceLocator;
23
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Set;
27 import java.util.StringTokenizer;
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
50
51 if (!OrmUtils.isJpaEnabled("rice.kew")) {
52 saveBranch(context, splitNodeNextNode.getBranch());
53 }
54 addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
55 }
56 joinInstance.setBranch(splitNode.getBranch());
57 joinInstance.setProcess(splitNode.getProcess());
58 }
59
60 public void addExpectedJoiner(RouteNodeInstance nodeInstance, Branch branch) {
61 addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
62 }
63
64 public void addActualJoiner(RouteNodeInstance nodeInstance, Branch branch) {
65 addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
66 }
67
68 private void addJoinState(RouteNodeInstance nodeInstance, Branch branch, String key) {
69 NodeState state = nodeInstance.getNodeState(key);
70 if (state == null) {
71 state = new NodeState();
72 state.setKey(key);
73 state.setValue("");
74 state.setNodeInstance(nodeInstance);
75 nodeInstance.addNodeState(state);
76 }
77 state.setValue(state.getValue()+branch.getBranchId()+",");
78 }
79
80 public boolean isJoined(RouteNodeInstance nodeInstance) {
81 NodeState expectedState = nodeInstance.getNodeState(EXPECTED_JOINERS);
82 if (expectedState == null || org.apache.commons.lang.StringUtils.isEmpty(expectedState.getValue())) {
83 return true;
84 }
85 NodeState actualState = nodeInstance.getNodeState(ACTUAL_JOINERS);
86 Set expectedSet = loadIntoSet(expectedState);
87 Set actualSet = loadIntoSet(actualState);
88 for (Iterator iterator = expectedSet.iterator(); iterator.hasNext();) {
89 String value = (String) iterator.next();
90 if (actualSet.contains(value)) {
91 iterator.remove();
92 }
93 }
94 return expectedSet.size() == 0;
95 }
96
97 private Set loadIntoSet(NodeState state) {
98 Set set = new HashSet();
99 StringTokenizer tokenizer = new StringTokenizer(state.getValue(), ",");
100 while (tokenizer.hasMoreTokens()) {
101 set.add(tokenizer.nextToken());
102 }
103 return set;
104 }
105
106 private void saveBranch(RouteContext context, Branch branch) {
107 if (!context.isSimulation()) {
108 KEWServiceLocator.getRouteNodeService().save(branch);
109 }
110 }
111
112 }