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