View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.engine.node;
17  
18  import org.kuali.rice.kew.api.WorkflowRuntimeException;
19  import org.kuali.rice.kew.engine.RouteContext;
20  import org.kuali.rice.kew.service.KEWServiceLocator;
21  import org.kuali.rice.krad.util.LegacyUtils;
22  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  import java.util.StringTokenizer;
27  
28  
29  /**
30   * A basic implementation of the JoinEngine which handles join setup and makes determinations
31   * as to when a join condition has been satisfied.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class BasicJoinEngine implements JoinEngine {
36  
37      public static final String EXPECTED_JOINERS = "ExpectedJoiners";
38      public static final String ACTUAL_JOINERS = "ActualJoiners";
39      
40      public RouteNodeInstance createExpectedJoinState(RouteContext context, RouteNodeInstance joinInstance, RouteNodeInstance previousNodeInstance) {
41          RouteNodeInstance splitNode = previousNodeInstance.getBranch().getSplitNode();
42          if (splitNode == null) {
43              throw new WorkflowRuntimeException("The split node retrieved from node with name '" + previousNodeInstance.getName() + "' and branch with name '" + previousNodeInstance.getBranch().getName() + "' was null");
44          }
45          for (Iterator iter = splitNode.getNextNodeInstances().iterator(); iter.hasNext();) {
46              RouteNodeInstance splitNodeNextNode = (RouteNodeInstance) iter.next();
47              joinInstance = saveNode(context, joinInstance);
48              splitNodeNextNode.getBranch().setJoinNode(joinInstance);
49              addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
50          }
51          joinInstance.setBranch(splitNode.getBranch());
52          joinInstance.setProcess(splitNode.getProcess());
53          return joinInstance;
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 || org.apache.commons.lang.StringUtils.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 RouteNodeInstance saveNode(RouteContext context, RouteNodeInstance nodeInstance) {
103         if (!context.isSimulation()) {
104             return KEWServiceLocator.getRouteNodeService().save(nodeInstance);
105         } else {
106             // if we are in simulation mode, lets go ahead and assign some id
107             // values to our beans
108             for (RouteNodeInstance routeNodeInstance : nodeInstance.getNextNodeInstances()) {
109                 if (routeNodeInstance.getRouteNodeInstanceId() == null) {
110                     routeNodeInstance.setRouteNodeInstanceId(context.getEngineState().getNextSimulationId());
111                 }
112             }
113             if (nodeInstance.getProcess() != null && nodeInstance.getProcess().getRouteNodeInstanceId() == null) {
114                 nodeInstance.getProcess().setRouteNodeInstanceId(context.getEngineState().getNextSimulationId());
115             }
116             if (nodeInstance.getBranch() != null && nodeInstance.getBranch().getBranchId() == null) {
117                 nodeInstance.getBranch().setBranchId(context.getEngineState().getNextSimulationId());
118             }
119             return nodeInstance;
120         }
121     }
122 
123 }