View Javadoc

1   /**
2    * Copyright 2005-2011 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.transition;
17  
18  import org.kuali.rice.kew.engine.RouteContext;
19  import org.kuali.rice.kew.engine.RouteHelper;
20  import org.kuali.rice.kew.engine.node.Node;
21  import org.kuali.rice.kew.engine.node.ProcessResult;
22  import org.kuali.rice.kew.engine.node.RouteNode;
23  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  
29  /**
30   * Common superclass for all Transition Engines.  A TransitionEngine handles transitioning into and out of
31   * a {@link RouteNodeInstance}.  The TransitionEngine is also responsible for determining if a Node has completed.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public abstract class TransitionEngine {
36      
37  	private RouteHelper helper;
38  	
39  	public RouteNodeInstance transitionTo(RouteNodeInstance nextNodeInstance, RouteContext context) throws Exception {
40  		return nextNodeInstance;
41  	}
42      
43      /**
44       * Tell the WorkflowEngine processing the activeNodeInstance if the node is complete and transitionFrom can 
45       * be called.
46       *
47       * @return boolean
48       * @param context for routing
49       * @throws Exception
50       */
51      public abstract ProcessResult isComplete(RouteContext context) throws Exception;
52  	
53      public Transition transitionFrom(RouteContext context, ProcessResult processResult) throws Exception {
54          return new Transition(resolveNextNodeInstances(context.getNodeInstance()));
55      }
56      
57      protected void setRouteHelper(RouteHelper helper) {
58      	this.helper = helper;
59      }
60      
61      protected RouteHelper getRouteHelper() {
62      	return helper;
63      }
64      
65      protected Node getNode(RouteNode routeNode, Class nodeClass) throws Exception {
66  		return helper.getNode(routeNode);
67      }
68      
69      /**
70       * Determines the next nodes instances for the transition.  If the node instance already
71       * has next nodes instances (i.e. a dynamic node), then those will be returned.  Otherwise
72       * it will resolve the next nodes from the RouteNode prototype.
73       * @param nodeInstance for the transition
74       * @param nextRouteNodes list of route notes
75       * @return list of route note instances
76       */
77      protected List<RouteNodeInstance> resolveNextNodeInstances(RouteNodeInstance nodeInstance, List<RouteNode> nextRouteNodes) {
78          List<RouteNodeInstance> nextNodeInstances = new ArrayList<RouteNodeInstance>();
79          for (RouteNode nextRouteNode : nextRouteNodes)
80          {
81              RouteNode nextNode = (RouteNode) nextRouteNode;
82              RouteNodeInstance nextNodeInstance = getRouteHelper().getNodeFactory().createRouteNodeInstance(nodeInstance.getDocumentId(), nextNode);
83              nextNodeInstance.setBranch(nodeInstance.getBranch());
84              nextNodeInstance.setProcess(nodeInstance.getProcess());
85              nextNodeInstances.add(nextNodeInstance);
86          }
87          return nextNodeInstances;
88      }
89      
90      protected List<RouteNodeInstance> resolveNextNodeInstances(RouteNodeInstance nodeInstance) {
91          return resolveNextNodeInstances(nodeInstance, nodeInstance.getRouteNode().getNextNodes());
92      }
93      
94  }