View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.engine.transition;
18  
19  import org.kuali.rice.kew.engine.RouteContext;
20  import org.kuali.rice.kew.engine.RouteHelper;
21  import org.kuali.rice.kew.engine.node.Node;
22  import org.kuali.rice.kew.engine.node.ProcessResult;
23  import org.kuali.rice.kew.engine.node.RouteNode;
24  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  
30  /**
31   * Common superclass for all Transition Engines.  A TransitionEngine handles transitioning into and out of
32   * a {@link RouteNodeInstance}.  The TransitionEngine is also responsible for determining if a Node has completed.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public abstract class TransitionEngine {
37      
38  	private RouteHelper helper;
39  	
40  	public RouteNodeInstance transitionTo(RouteNodeInstance nextNodeInstance, RouteContext context) throws Exception {
41  		return nextNodeInstance;
42  	}
43      
44      /**
45       * Tell the WorkflowEngine processing the activeNodeInstance if the node is complete and transitionFrom can 
46       * be called.
47       *
48       * @return boolean
49       * @param context for routing
50       * @throws Exception
51       */
52      public abstract ProcessResult isComplete(RouteContext context) throws Exception;
53  	
54      public Transition transitionFrom(RouteContext context, ProcessResult processResult) throws Exception {
55          return new Transition(resolveNextNodeInstances(context.getNodeInstance()));
56      }
57      
58      protected void setRouteHelper(RouteHelper helper) {
59      	this.helper = helper;
60      }
61      
62      protected RouteHelper getRouteHelper() {
63      	return helper;
64      }
65      
66      protected Node getNode(RouteNode routeNode, Class nodeClass) throws Exception {
67  		return helper.getNode(routeNode);
68      }
69      
70      /**
71       * Determines the next nodes instances for the transition.  If the node instance already
72       * has next nodes instances (i.e. a dynamic node), then those will be returned.  Otherwise
73       * it will resolve the next nodes from the RouteNode prototype.
74       * @param nodeInstance for the transition
75       * @param nextRouteNodes list of route notes
76       * @return list of route note instances
77       */
78      protected List<RouteNodeInstance> resolveNextNodeInstances(RouteNodeInstance nodeInstance, List<RouteNode> nextRouteNodes) {
79          List<RouteNodeInstance> nextNodeInstances = new ArrayList<RouteNodeInstance>();
80          for (RouteNode nextRouteNode : nextRouteNodes)
81          {
82              RouteNode nextNode = (RouteNode) nextRouteNode;
83              RouteNodeInstance nextNodeInstance = getRouteHelper().getNodeFactory().createRouteNodeInstance(nodeInstance.getDocumentId(), nextNode);
84              nextNodeInstance.setBranch(nodeInstance.getBranch());
85              nextNodeInstance.setProcess(nodeInstance.getProcess());
86              nextNodeInstances.add(nextNodeInstance);
87          }
88          return nextNodeInstances;
89      }
90      
91      protected List<RouteNodeInstance> resolveNextNodeInstances(RouteNodeInstance nodeInstance) {
92          return resolveNextNodeInstances(nodeInstance, nodeInstance.getRouteNode().getNextNodes());
93      }
94      
95  }