View Javadoc

1   /**
2    * Copyright 2005-2013 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.core.framework.persistence.jpa.OrmUtils;
19  import org.kuali.rice.kew.api.WorkflowRuntimeException;
20  import org.kuali.rice.kew.engine.RouteContext;
21  import org.kuali.rice.kew.service.KEWServiceLocator;
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 void 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              // Dilemma: we are given an unsaved join node. For linking to work in absence of joinNode auto-update we must
48              // ensure the join node instance is saved before we save the branch, however this save is done afterwards in
49              // the caller (StandardWorkflowEngine).
50              // If we save here we should probably be sure to take into account simulation context
51              saveNode(context, joinInstance);
52              splitNodeNextNode.getBranch().setJoinNode(joinInstance);
53              // The saveBranch() call below is necessary for parallel routing to work properly with OJB, but it breaks parallel routing with JPA,
54              // so only perform it if KEW is not JPA-enabled.
55              if (!OrmUtils.isJpaEnabled("rice.kew")) {
56              	saveBranch(context, splitNodeNextNode.getBranch());
57              }
58              addExpectedJoiner(joinInstance, splitNodeNextNode.getBranch());
59          }
60          joinInstance.setBranch(splitNode.getBranch());
61          joinInstance.setProcess(splitNode.getProcess());
62      }
63      
64      public void addExpectedJoiner(RouteNodeInstance nodeInstance, Branch branch) {
65          addJoinState(nodeInstance, branch, EXPECTED_JOINERS);
66      }
67  
68      public void addActualJoiner(RouteNodeInstance nodeInstance, Branch branch) {
69          addJoinState(nodeInstance, branch, ACTUAL_JOINERS);
70      }
71      
72      private void addJoinState(RouteNodeInstance nodeInstance, Branch branch, String key) {
73          NodeState state = nodeInstance.getNodeState(key);
74          if (state == null) {
75              state = new NodeState();
76              state.setKey(key);
77              state.setValue("");
78              state.setNodeInstance(nodeInstance);
79              nodeInstance.addNodeState(state);
80          }
81          state.setValue(state.getValue()+branch.getBranchId()+",");
82      }
83  
84      public boolean isJoined(RouteNodeInstance nodeInstance) {
85          NodeState expectedState = nodeInstance.getNodeState(EXPECTED_JOINERS);
86          if (expectedState == null || org.apache.commons.lang.StringUtils.isEmpty(expectedState.getValue())) {
87              return true;
88          }
89          NodeState actualState = nodeInstance.getNodeState(ACTUAL_JOINERS);
90          Set expectedSet = loadIntoSet(expectedState);
91          Set actualSet = loadIntoSet(actualState);
92          for (Iterator iterator = expectedSet.iterator(); iterator.hasNext();) {
93              String value = (String) iterator.next();
94              if (actualSet.contains(value)) {
95                  iterator.remove();
96              }            
97          }
98          return expectedSet.size() == 0;
99      }
100     
101     private Set loadIntoSet(NodeState state) {
102         Set set = new HashSet();
103         StringTokenizer tokenizer = new StringTokenizer(state.getValue(), ",");
104         while (tokenizer.hasMoreTokens()) {
105             set.add(tokenizer.nextToken());
106         }
107         return set;
108     }
109     
110     private void saveBranch(RouteContext context, Branch branch) {
111         if (!context.isSimulation()) {
112             KEWServiceLocator.getRouteNodeService().save(branch);
113         }
114     }
115     
116     // see {@link StandardWorkflowEngine#saveNode}
117     private void saveNode(RouteContext context, RouteNodeInstance nodeInstance) {
118         if (!context.isSimulation()) {
119             KEWServiceLocator.getRouteNodeService().save(nodeInstance);
120         } else {
121             // if we are in simulation mode, lets go ahead and assign some id
122             // values to our beans
123             for (Iterator<RouteNodeInstance> iterator = nodeInstance.getNextNodeInstances().iterator(); iterator.hasNext();) {
124                 RouteNodeInstance routeNodeInstance = (RouteNodeInstance) iterator.next();
125                 if (routeNodeInstance.getRouteNodeInstanceId() == null) {
126                     routeNodeInstance.setRouteNodeInstanceId(context.getEngineState().getNextSimulationId());
127                 }
128             }
129             if (nodeInstance.getProcess() != null && nodeInstance.getProcess().getRouteNodeInstanceId() == null) {
130                 nodeInstance.getProcess().setRouteNodeInstanceId(context.getEngineState().getNextSimulationId());
131             }
132             if (nodeInstance.getBranch() != null && nodeInstance.getBranch().getBranchId() == null) {
133                 nodeInstance.getBranch().setBranchId(context.getEngineState().getNextSimulationId());
134             }
135         }
136     }
137 
138 }