|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  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 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
   public final class CompatUtils { | 
  |  40 |     | 
             | 
  |  41 |    0 |        private static RouteHelper helper = new RouteHelper();  | 
  |  42 |     | 
         | 
  |  43 |    0 |            private CompatUtils() { | 
  |  44 |    0 |                    throw new UnsupportedOperationException("do not call"); | 
  |  45 |     | 
           }  | 
  |  46 |     | 
         | 
  |  47 |     | 
       public static Integer getLevelForNode(DocumentType documentType, String nodeName) { | 
  |  48 |    0 |            if (isRouteLevelCompatible(documentType)) { | 
  |  49 |    0 |                return getLevelForNode(documentType.getPrimaryProcess().getInitialRouteNode(), nodeName, new Integer(0));  | 
  |  50 |     | 
           }  | 
  |  51 |    0 |            return new Integer(KEWConstants.INVALID_ROUTE_LEVEL);  | 
  |  52 |     | 
       }  | 
  |  53 |     | 
         | 
  |  54 |     | 
       private static Integer getLevelForNode(RouteNode node, String nodeName, Integer currentLevel) { | 
  |  55 |     | 
             | 
  |  56 |     | 
             | 
  |  57 |    0 |            if (node.getRouteNodeName().equals(nodeName)) { | 
  |  58 |    0 |                return currentLevel;  | 
  |  59 |     | 
           }  | 
  |  60 |    0 |            List nextNodes = node.getNextNodes();  | 
  |  61 |    0 |            if (nextNodes.isEmpty()) { | 
  |  62 |    0 |                throw new WorkflowRuntimeException("Could not locate node with name '"+nodeName+"'"); | 
  |  63 |     | 
           }  | 
  |  64 |    0 |            if (nextNodes.size() > 1) { | 
  |  65 |    0 |                throw new WorkflowRuntimeException("Can only determine route level for document types with no splitting"); | 
  |  66 |     | 
           }  | 
  |  67 |    0 |            RouteNode nextNode = (RouteNode)nextNodes.get(0);  | 
  |  68 |    0 |            return getLevelForNode(nextNode, nodeName, new Integer(currentLevel.intValue()+1));  | 
  |  69 |     | 
       }  | 
  |  70 |     | 
         | 
  |  71 |     | 
         | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
     | 
  |  75 |     | 
     | 
  |  76 |     | 
       public static RouteNode getNodeForLevel(DocumentType documentType, Integer routeLevel) { | 
  |  77 |    0 |            Object[] node = getNodeForLevel(documentType.getPrimaryProcess().getInitialRouteNode(), routeLevel, new Integer(0));  | 
  |  78 |    0 |            return (RouteNode)node[0];  | 
  |  79 |     | 
       }  | 
  |  80 |     | 
         | 
  |  81 |     | 
       private static Object[] getNodeForLevel(RouteNode node, Integer routeLevel, Integer currentLevel) { | 
  |  82 |    0 |            if (helper.isSubProcessNode(node)) { | 
  |  83 |    0 |                Object[] result = getNodeForLevel(node.getDocumentType().getNamedProcess(node.getRouteNodeName()).getInitialRouteNode(), routeLevel, currentLevel);  | 
  |  84 |    0 |                if (result[0] != null) { | 
  |  85 |    0 |                    node = (RouteNode)result[0];  | 
  |  86 |     | 
               }  | 
  |  87 |    0 |                currentLevel = (Integer)result[1];  | 
  |  88 |     | 
           }  | 
  |  89 |    0 |            if (currentLevel.equals(routeLevel)) { | 
  |  90 |    0 |                return new Object[] { node, currentLevel }; | 
  |  91 |     | 
           }  | 
  |  92 |    0 |            List nextNodes = node.getNextNodes();  | 
  |  93 |    0 |            if (nextNodes.isEmpty()) { | 
  |  94 |    0 |                return new Object[] { null, currentLevel }; | 
  |  95 |     | 
           }  | 
  |  96 |    0 |            if (nextNodes.size() > 1) { | 
  |  97 |    0 |                throw new WorkflowRuntimeException("Cannot determine a route level number for documents with splitting."); | 
  |  98 |     | 
           }  | 
  |  99 |    0 |            currentLevel = new Integer(currentLevel.intValue()+1);  | 
  |  100 |    0 |            return getNodeForLevel((RouteNode)nextNodes.get(0), routeLevel, currentLevel);  | 
  |  101 |     | 
       }  | 
  |  102 |     | 
     | 
  |  103 |     | 
       public static boolean isRouteLevelCompatible(DocumentType documentType) { | 
  |  104 |    0 |            return KEWConstants.ROUTING_VERSION_ROUTE_LEVEL.equals(documentType.getRoutingVersion());  | 
  |  105 |     | 
       }  | 
  |  106 |     | 
         | 
  |  107 |     | 
       public static boolean isRouteLevelCompatible(DocumentRouteHeaderValue document) { | 
  |  108 |    0 |            return isRouteLevelCompatible(document.getDocumentType());  | 
  |  109 |     | 
       }  | 
  |  110 |     | 
         | 
  |  111 |     | 
       public static boolean isNodalDocument(DocumentRouteHeaderValue document) { | 
  |  112 |    0 |            return KEWConstants.DOCUMENT_VERSION_NODAL == document.getDocVersion().intValue();  | 
  |  113 |     | 
       }  | 
  |  114 |     | 
         | 
  |  115 |     | 
       public static boolean isNodalRequest(ActionRequestValue request) { | 
  |  116 |    0 |            return KEWConstants.DOCUMENT_VERSION_NODAL == request.getDocVersion().intValue();  | 
  |  117 |     | 
       }  | 
  |  118 |     | 
         | 
  |  119 |     | 
       public static boolean isRouteLevelDocument(DocumentRouteHeaderValue document) { | 
  |  120 |    0 |            return KEWConstants.DOCUMENT_VERSION_ROUTE_LEVEL == document.getDocVersion().intValue();  | 
  |  121 |     | 
       }  | 
  |  122 |     | 
         | 
  |  123 |     | 
       public static boolean isRouteLevelRequest(ActionRequestValue request) { | 
  |  124 |    0 |            return KEWConstants.DOCUMENT_VERSION_ROUTE_LEVEL == request.getDocVersion().intValue();  | 
  |  125 |     | 
       }  | 
  |  126 |     | 
         | 
  |  127 |     | 
         | 
  |  128 |     | 
     | 
  |  129 |     | 
     | 
  |  130 |     | 
     | 
  |  131 |     | 
       public static List getRouteLevelCompatibleNodeList(DocumentType documentType) { | 
  |  132 |    0 |            if (!isRouteLevelCompatible(documentType)) { | 
  |  133 |    0 |                throw new WorkflowRuntimeException("Attempting to invoke a 'route level' operation on a document which is not route level compatible."); | 
  |  134 |     | 
           }  | 
  |  135 |    0 |            Process primaryProcess = documentType.getPrimaryProcess();  | 
  |  136 |    0 |            RouteNode routeNode = primaryProcess.getInitialRouteNode();  | 
  |  137 |    0 |            List nodes = new ArrayList();  | 
  |  138 |    0 |            int count = 0;  | 
  |  139 |    0 |            int maxCount = 100;  | 
  |  140 |     | 
           while (true) { | 
  |  141 |    0 |                nodes.add(routeNode);  | 
  |  142 |    0 |                List nextNodes = routeNode.getNextNodes();  | 
  |  143 |    0 |                if (nextNodes.size() == 0) { | 
  |  144 |    0 |                    break;  | 
  |  145 |     | 
               }  | 
  |  146 |    0 |                if (nextNodes.size() > 1) { | 
  |  147 |    0 |                    throw new RuntimeException("Node has more than one next node!  It is not route level compatible!" + routeNode.getRouteNodeName()); | 
  |  148 |     | 
               }  | 
  |  149 |    0 |                if (count >= maxCount) { | 
  |  150 |    0 |                    throw new RuntimeException("A runaway loop was detected when attempting to create route level compatible node graph.  documentType=" + documentType.getDocumentTypeId()+","+documentType.getName()); | 
  |  151 |     | 
               }  | 
  |  152 |    0 |                routeNode = (RouteNode) nextNodes.iterator().next();  | 
  |  153 |    0 |            }  | 
  |  154 |    0 |            return nodes;  | 
  |  155 |     | 
       }  | 
  |  156 |     | 
         | 
  |  157 |     | 
       public static int getMaxRouteLevel(DocumentType documentType) { | 
  |  158 |    0 |            return getRouteLevelCompatibleNodeList(documentType).size();  | 
  |  159 |     | 
       }  | 
  |  160 |     | 
   }  |