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;
17  
18  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
19  import org.kuali.rice.kew.api.KewApiConstants;
20  import org.kuali.rice.kew.api.WorkflowRuntimeException;
21  import org.kuali.rice.kew.doctype.bo.DocumentType;
22  import org.kuali.rice.kew.engine.node.ProcessDefinitionBo;
23  import org.kuali.rice.kew.engine.node.RouteNode;
24  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
25  import org.kuali.rice.kew.api.KewApiConstants;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  
31  /**
32   * Provides utility methods for handling backwards compatibility between KEW releases.
33   * Currently, it's primary function is to handle backward compatability between the
34   * deprecated "route level" concept and the "node" concept which was introduced in
35   * KEW 2.1.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public final class CompatUtils {
40  	
41      private static RouteHelper helper = new RouteHelper();
42      
43  	private CompatUtils() {
44  		throw new UnsupportedOperationException("do not call");
45  	}
46      
47      public static Integer getLevelForNode(DocumentType documentType, String nodeName) {
48          if (isRouteLevelCompatible(documentType)) {
49              return getLevelForNode(documentType.getPrimaryProcess().getInitialRouteNode(), nodeName, new Integer(0));
50          }
51          return new Integer(KewApiConstants.INVALID_ROUTE_LEVEL);
52      }
53      
54      private static Integer getLevelForNode(RouteNode node, String nodeName, Integer currentLevel) {
55          // TODO potential for infinite recursion here if their document type has loops in it.  Should this be a concern?
56          // If their routing version is really "route level" then there should be no cycles.
57          if (node.getRouteNodeName().equals(nodeName)) {
58              return currentLevel;
59          }
60          List<RouteNode> nextNodes = node.getNextNodes();
61          if (nextNodes.isEmpty()) {
62              throw new WorkflowRuntimeException("Could not locate node with name '"+nodeName+"'");
63          }
64          if (nextNodes.size() > 1) {
65              throw new WorkflowRuntimeException("Can only determine route level for document types with no splitting");
66          }
67          RouteNode nextNode = (RouteNode)nextNodes.get(0);
68          return getLevelForNode(nextNode, nodeName, new Integer(currentLevel.intValue()+1));
69      }
70      
71      /**
72       * Returns the RouteNode at the given numerical route level for the given document type.
73       * This currently throws a WorkflowException if the document has parallel routing structures
74       * because the route level as a number becomes arbitrary in that case. 
75       */
76      public static RouteNode getNodeForLevel(DocumentType documentType, Integer routeLevel) {
77          Object[] node = getNodeForLevel(documentType.getPrimaryProcess().getInitialRouteNode(), routeLevel, new Integer(0));
78          return (RouteNode)node[0];
79      }
80      
81      private static Object[] getNodeForLevel(RouteNode node, Integer routeLevel, Integer currentLevel) {
82          if (helper.isSubProcessNode(node)) {
83              Object[] result = getNodeForLevel(node.getDocumentType().getNamedProcess(node.getRouteNodeName()).getInitialRouteNode(), routeLevel, currentLevel);
84              if (result[0] != null) {
85                  node = (RouteNode)result[0];
86              }
87              currentLevel = (Integer)result[1];
88          }
89          if (currentLevel.equals(routeLevel)) {
90              return new Object[] { node, currentLevel };
91          }
92          List<RouteNode> nextNodes = node.getNextNodes();
93          if (nextNodes.isEmpty()) {
94              return new Object[] { null, currentLevel };
95          }
96          if (nextNodes.size() > 1) {
97              throw new WorkflowRuntimeException("Cannot determine a route level number for documents with splitting.");
98          }
99          currentLevel = new Integer(currentLevel.intValue()+1);
100         return getNodeForLevel((RouteNode)nextNodes.get(0), routeLevel, currentLevel);
101     }
102 
103     public static boolean isRouteLevelCompatible(DocumentType documentType) {
104         return KewApiConstants.ROUTING_VERSION_ROUTE_LEVEL.equals(documentType.getRoutingVersion());
105     }
106     
107     public static boolean isRouteLevelCompatible(DocumentRouteHeaderValue document) {
108         return isRouteLevelCompatible(document.getDocumentType());
109     }
110     
111     public static boolean isNodalDocument(DocumentRouteHeaderValue document) {
112         return KewApiConstants.DocumentContentVersions.NODAL == document.getDocVersion().intValue();
113     }
114     
115     public static boolean isNodalRequest(ActionRequestValue request) {
116         return KewApiConstants.DocumentContentVersions.NODAL == request.getDocVersion().intValue();
117     }
118     
119     public static boolean isRouteLevelDocument(DocumentRouteHeaderValue document) {
120         return KewApiConstants.DocumentContentVersions.ROUTE_LEVEL == document.getDocVersion().intValue();
121     }
122     
123     public static boolean isRouteLevelRequest(ActionRequestValue request) {
124         return KewApiConstants.DocumentContentVersions.ROUTE_LEVEL == request.getDocVersion().intValue();
125     }
126     
127     /**
128      * Returns a list of RouteNodes in a flat list which is equivalent to the route level concept of
129      * Workflow <= version 2.0.  If the document type is not route level compatible, then this method will throw an error.
130      */
131     public static List<RouteNode> getRouteLevelCompatibleNodeList(DocumentType documentType) {
132         if (!isRouteLevelCompatible(documentType)) {
133             throw new WorkflowRuntimeException("Attempting to invoke a 'route level' operation on a document which is not route level compatible.");
134         }
135         ProcessDefinitionBo primaryProcess = documentType.getPrimaryProcess();
136         RouteNode routeNode = primaryProcess.getInitialRouteNode();
137         List<RouteNode> nodes = new ArrayList<RouteNode>();
138         int count = 0;
139         int maxCount = 100;
140         while (true) {
141             nodes.add(routeNode);
142             List<RouteNode> nextNodes = routeNode.getNextNodes();
143             if (nextNodes.size() == 0) {
144                 break;
145             }
146             if (nextNodes.size() > 1) {
147                 throw new RuntimeException("Node has more than one next node!  It is not route level compatible!" + routeNode.getRouteNodeName());
148             }
149             if (count >= maxCount) {
150                 throw new RuntimeException("A runaway loop was detected when attempting to create route level compatible node graph.  documentType=" + documentType.getDocumentTypeId()+","+documentType.getName());
151             }
152             routeNode = nextNodes.iterator().next();
153         }
154         return nodes;
155     }
156     
157     public static int getMaxRouteLevel(DocumentType documentType) {
158         return getRouteLevelCompatibleNodeList(documentType).size();
159     }
160 }