|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.engine.node.service.impl;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.commons.collections.ComparatorUtils;  | 
  |  20 |     | 
   import org.kuali.rice.kew.doctype.bo.DocumentType;  | 
  |  21 |     | 
   import org.kuali.rice.kew.engine.RouteHelper;  | 
  |  22 |     | 
   import org.kuali.rice.kew.engine.node.*;  | 
  |  23 |     | 
   import org.kuali.rice.kew.engine.node.Process;  | 
  |  24 |     | 
   import org.kuali.rice.kew.engine.node.dao.RouteNodeDAO;  | 
  |  25 |     | 
   import org.kuali.rice.kew.engine.node.service.RouteNodeService;  | 
  |  26 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;  | 
  |  27 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  28 |     | 
     | 
  |  29 |     | 
   import java.util.*;  | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |    0 |    public class RouteNodeServiceImpl implements RouteNodeService { | 
  |  34 |     | 
     | 
  |  35 |    0 |            protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());  | 
  |  36 |     | 
             | 
  |  37 |     | 
           public static final String REVOKED_NODE_INSTANCES_STATE_KEY = "NodeInstances.Revoked";  | 
  |  38 |     | 
     | 
  |  39 |    0 |            private static final Comparator NODE_INSTANCE_FORWARD_SORT = new NodeInstanceIdSorter();  | 
  |  40 |    0 |            private static final Comparator NODE_INSTANCE_BACKWARD_SORT =   | 
  |  41 |     | 
                   ComparatorUtils.reversedComparator(NODE_INSTANCE_FORWARD_SORT);  | 
  |  42 |    0 |        private RouteHelper helper = new RouteHelper();  | 
  |  43 |     | 
           private RouteNodeDAO routeNodeDAO;  | 
  |  44 |     | 
             | 
  |  45 |     | 
       public void save(RouteNode node) { | 
  |  46 |    0 |                routeNodeDAO.save(node);  | 
  |  47 |    0 |        }  | 
  |  48 |     | 
         | 
  |  49 |     | 
       public void save(RouteNodeInstance nodeInstance) { | 
  |  50 |    0 |                routeNodeDAO.save(nodeInstance);  | 
  |  51 |    0 |        }  | 
  |  52 |     | 
         | 
  |  53 |     | 
       public void save(NodeState nodeState) { | 
  |  54 |    0 |            routeNodeDAO.save(nodeState);  | 
  |  55 |    0 |        }  | 
  |  56 |     | 
         | 
  |  57 |     | 
       public void save(Branch branch) { | 
  |  58 |    0 |            routeNodeDAO.save(branch);  | 
  |  59 |    0 |        }  | 
  |  60 |     | 
     | 
  |  61 |     | 
       public RouteNode findRouteNodeById(Long nodeId) { | 
  |  62 |    0 |                return routeNodeDAO.findRouteNodeById(nodeId);  | 
  |  63 |     | 
       }  | 
  |  64 |     | 
         | 
  |  65 |     | 
       public RouteNodeInstance findRouteNodeInstanceById(Long nodeInstanceId) { | 
  |  66 |    0 |                return routeNodeDAO.findRouteNodeInstanceById(nodeInstanceId);  | 
  |  67 |     | 
       }  | 
  |  68 |     | 
     | 
  |  69 |     | 
       public RouteNodeInstance findRouteNodeInstanceById(Long nodeInstanceId, DocumentRouteHeaderValue document) { | 
  |  70 |    0 |                return RouteNodeUtils.findRouteNodeInstanceById(nodeInstanceId, document);  | 
  |  71 |     | 
       }  | 
  |  72 |     | 
         | 
  |  73 |     | 
       public List getCurrentNodeInstances(Long documentId) { | 
  |  74 |    0 |            List<RouteNodeInstance> currentNodeInstances = getActiveNodeInstances(documentId);  | 
  |  75 |    0 |            if (currentNodeInstances.isEmpty()) { | 
  |  76 |    0 |                currentNodeInstances = getTerminalNodeInstances(documentId);  | 
  |  77 |     | 
           }  | 
  |  78 |    0 |            return currentNodeInstances;  | 
  |  79 |     | 
       }  | 
  |  80 |     | 
         | 
  |  81 |     | 
       public List<RouteNodeInstance> getActiveNodeInstances(Long documentId) { | 
  |  82 |    0 |                return routeNodeDAO.getActiveNodeInstances(documentId);  | 
  |  83 |     | 
       }  | 
  |  84 |     | 
         | 
  |  85 |     | 
       public List<RouteNodeInstance> getActiveNodeInstances(DocumentRouteHeaderValue document) { | 
  |  86 |    0 |           List<RouteNodeInstance> flattenedNodeInstances = getFlattenedNodeInstances(document, true);  | 
  |  87 |    0 |            List<RouteNodeInstance> activeNodeInstances = new ArrayList<RouteNodeInstance>();  | 
  |  88 |    0 |            for (RouteNodeInstance nodeInstance : flattenedNodeInstances) { | 
  |  89 |    0 |                if (nodeInstance.isActive()) { | 
  |  90 |    0 |                    activeNodeInstances.add(nodeInstance);  | 
  |  91 |     | 
               }  | 
  |  92 |     | 
           }  | 
  |  93 |    0 |            return activeNodeInstances;  | 
  |  94 |     | 
       }  | 
  |  95 |     | 
         | 
  |  96 |     | 
       public List<RouteNodeInstance> getTerminalNodeInstances(Long documentId) { | 
  |  97 |    0 |            return routeNodeDAO.getTerminalNodeInstances(documentId);  | 
  |  98 |     | 
       }  | 
  |  99 |     | 
         | 
  |  100 |     | 
       public List getInitialNodeInstances(Long documentId) { | 
  |  101 |    0 |                return routeNodeDAO.getInitialNodeInstances(documentId);  | 
  |  102 |     | 
       }  | 
  |  103 |     | 
         | 
  |  104 |     | 
       public NodeState findNodeState(Long nodeInstanceId, String key) { | 
  |  105 |    0 |            return routeNodeDAO.findNodeState(nodeInstanceId, key);  | 
  |  106 |     | 
       }  | 
  |  107 |     | 
         | 
  |  108 |     | 
       public RouteNode findRouteNodeByName(Long documentTypeId, String name) { | 
  |  109 |    0 |            return routeNodeDAO.findRouteNodeByName(documentTypeId, name);  | 
  |  110 |     | 
       }  | 
  |  111 |     | 
         | 
  |  112 |     | 
       public List findFinalApprovalRouteNodes(Long documentTypeId) { | 
  |  113 |    0 |            DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findById(documentTypeId);  | 
  |  114 |    0 |            documentType = documentType.getRouteDefiningDocumentType();  | 
  |  115 |    0 |            return routeNodeDAO.findFinalApprovalRouteNodes(documentType.getDocumentTypeId());  | 
  |  116 |     | 
       }  | 
  |  117 |     | 
         | 
  |  118 |     | 
       public List findNextRouteNodesInPath(RouteNodeInstance nodeInstance, String nodeName) { | 
  |  119 |    0 |            List<RouteNode> nodesInPath = new ArrayList<RouteNode>();  | 
  |  120 |    0 |            for (Iterator<RouteNode> iterator = nodeInstance.getRouteNode().getNextNodes().iterator(); iterator.hasNext();) { | 
  |  121 |    0 |                RouteNode nextNode = iterator.next();  | 
  |  122 |    0 |                nodesInPath.addAll(findNextRouteNodesInPath(nodeName, nextNode, new HashSet<Long>()));  | 
  |  123 |    0 |            }  | 
  |  124 |    0 |            return nodesInPath;  | 
  |  125 |     | 
       }  | 
  |  126 |     | 
         | 
  |  127 |     | 
       private List<RouteNode> findNextRouteNodesInPath(String nodeName, RouteNode node, Set<Long> inspected) { | 
  |  128 |    0 |            List<RouteNode> nextNodesInPath = new ArrayList<RouteNode>();  | 
  |  129 |    0 |            if (inspected.contains(node.getRouteNodeId())) { | 
  |  130 |    0 |                return nextNodesInPath;  | 
  |  131 |     | 
           }  | 
  |  132 |    0 |            inspected.add(node.getRouteNodeId());  | 
  |  133 |    0 |            if (node.getRouteNodeName().equals(nodeName)) { | 
  |  134 |    0 |                nextNodesInPath.add(node);  | 
  |  135 |     | 
           } else { | 
  |  136 |    0 |                if (helper.isSubProcessNode(node)) { | 
  |  137 |    0 |                    Process subProcess = node.getDocumentType().getNamedProcess(node.getRouteNodeName());  | 
  |  138 |    0 |                    RouteNode subNode = subProcess.getInitialRouteNode();  | 
  |  139 |    0 |                    nextNodesInPath.addAll(findNextRouteNodesInPath(nodeName, subNode, inspected));  | 
  |  140 |     | 
               }  | 
  |  141 |    0 |                for (Iterator<RouteNode> iterator = node.getNextNodes().iterator(); iterator.hasNext();) { | 
  |  142 |    0 |                    RouteNode nextNode = iterator.next();  | 
  |  143 |    0 |                    nextNodesInPath.addAll(findNextRouteNodesInPath(nodeName, nextNode, inspected));  | 
  |  144 |    0 |                }  | 
  |  145 |     | 
           }  | 
  |  146 |    0 |            return nextNodesInPath;  | 
  |  147 |     | 
       }  | 
  |  148 |     | 
         | 
  |  149 |     | 
       public boolean isNodeInPath(DocumentRouteHeaderValue document, String nodeName) { | 
  |  150 |    0 |            boolean isInPath = false;  | 
  |  151 |    0 |            Collection<RouteNodeInstance> activeNodes = getActiveNodeInstances(document.getRouteHeaderId());  | 
  |  152 |    0 |            for (Iterator<RouteNodeInstance> iterator = activeNodes.iterator(); iterator.hasNext();) { | 
  |  153 |    0 |                RouteNodeInstance nodeInstance = iterator.next();  | 
  |  154 |    0 |                List nextNodesInPath = findNextRouteNodesInPath(nodeInstance, nodeName);  | 
  |  155 |    0 |                isInPath = isInPath || !nextNodesInPath.isEmpty();  | 
  |  156 |    0 |            }  | 
  |  157 |    0 |            return isInPath;  | 
  |  158 |     | 
       }  | 
  |  159 |     | 
         | 
  |  160 |     | 
       public List findRouteNodeInstances(Long documentId) { | 
  |  161 |    0 |            return this.routeNodeDAO.findRouteNodeInstances(documentId);  | 
  |  162 |     | 
       }  | 
  |  163 |     | 
         | 
  |  164 |     | 
           public void setRouteNodeDAO(RouteNodeDAO dao) { | 
  |  165 |    0 |                    this.routeNodeDAO = dao;  | 
  |  166 |    0 |            }  | 
  |  167 |     | 
         | 
  |  168 |     | 
       public List findProcessNodeInstances(RouteNodeInstance process) { | 
  |  169 |    0 |           return this.routeNodeDAO.findProcessNodeInstances(process);  | 
  |  170 |     | 
       }  | 
  |  171 |     | 
         | 
  |  172 |     | 
       public Set findPreviousNodeNames(Long documentId) { | 
  |  173 |    0 |            List currentNodeInstances = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(documentId);  | 
  |  174 |    0 |            List<RouteNodeInstance> nodeInstances = new ArrayList<RouteNodeInstance>();  | 
  |  175 |    0 |            for (Iterator iterator = currentNodeInstances.iterator(); iterator.hasNext();) { | 
  |  176 |    0 |                RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next();  | 
  |  177 |    0 |                nodeInstances.addAll(nodeInstance.getPreviousNodeInstances());  | 
  |  178 |    0 |            }  | 
  |  179 |    0 |            Set<String> nodeNames = new HashSet<String>();  | 
  |  180 |    0 |            while (!nodeInstances.isEmpty()) { | 
  |  181 |    0 |                RouteNodeInstance nodeInstance = nodeInstances.remove(0);  | 
  |  182 |    0 |                nodeNames.add(nodeInstance.getName());  | 
  |  183 |    0 |                nodeInstances.addAll(nodeInstance.getPreviousNodeInstances());  | 
  |  184 |    0 |            }  | 
  |  185 |    0 |            return nodeNames;  | 
  |  186 |     | 
       }  | 
  |  187 |     | 
         | 
  |  188 |     | 
       public List<String> findFutureNodeNames(Long documentId) { | 
  |  189 |    0 |            List currentNodeInstances = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(documentId);  | 
  |  190 |    0 |            List<RouteNode> nodes = new ArrayList<RouteNode>();  | 
  |  191 |    0 |            for (Iterator iterator = currentNodeInstances.iterator(); iterator.hasNext();) { | 
  |  192 |    0 |                RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next();  | 
  |  193 |    0 |                nodes.addAll(nodeInstance.getRouteNode().getNextNodes());  | 
  |  194 |    0 |            }  | 
  |  195 |    0 |            List<String> nodeNames = new ArrayList<String>();  | 
  |  196 |    0 |            while (!nodes.isEmpty()) { | 
  |  197 |    0 |                RouteNode node = nodes.remove(0);  | 
  |  198 |    0 |                if (!nodeNames.contains(node.getRouteNodeName())) { | 
  |  199 |    0 |                    nodeNames.add(node.getRouteNodeName());  | 
  |  200 |     | 
               }  | 
  |  201 |    0 |                nodes.addAll(node.getNextNodes());  | 
  |  202 |    0 |            }  | 
  |  203 |    0 |            return nodeNames;  | 
  |  204 |     | 
       }  | 
  |  205 |     | 
         | 
  |  206 |     | 
       public List<RouteNode> getFlattenedNodes(DocumentType documentType, boolean climbHierarchy) { | 
  |  207 |    0 |            List<RouteNode> nodes = new ArrayList<RouteNode>();  | 
  |  208 |    0 |            if (!documentType.isRouteInherited() || climbHierarchy) { | 
  |  209 |    0 |                for (Iterator iterator = documentType.getProcesses().iterator(); iterator.hasNext();) { | 
  |  210 |    0 |                    Process process = (Process) iterator.next();  | 
  |  211 |    0 |                    nodes.addAll(getFlattenedNodes(process));  | 
  |  212 |    0 |                }  | 
  |  213 |     | 
           }  | 
  |  214 |    0 |            Collections.sort(nodes, new RouteNodeSorter());  | 
  |  215 |    0 |            return nodes;  | 
  |  216 |     | 
       }  | 
  |  217 |     | 
         | 
  |  218 |     | 
       public List<RouteNode> getFlattenedNodes(Process process) { | 
  |  219 |    0 |            Map<String, RouteNode> nodesMap = new HashMap<String, RouteNode>();  | 
  |  220 |    0 |            if (process.getInitialRouteNode() != null) { | 
  |  221 |    0 |                flattenNodeGraph(nodesMap, process.getInitialRouteNode());  | 
  |  222 |    0 |                List<RouteNode> nodes = new ArrayList<RouteNode>(nodesMap.values());  | 
  |  223 |    0 |                Collections.sort(nodes, new RouteNodeSorter());  | 
  |  224 |    0 |                return nodes;  | 
  |  225 |     | 
           } else { | 
  |  226 |    0 |                List<RouteNode> nodes = new ArrayList<RouteNode>();  | 
  |  227 |    0 |                nodes.add(new RouteNode());  | 
  |  228 |    0 |                return nodes;  | 
  |  229 |     | 
           }  | 
  |  230 |     | 
     | 
  |  231 |     | 
       }  | 
  |  232 |     | 
         | 
  |  233 |     | 
         | 
  |  234 |     | 
     | 
  |  235 |     | 
     | 
  |  236 |     | 
     | 
  |  237 |     | 
       private void flattenNodeGraph(Map<String, RouteNode> nodes, RouteNode node) { | 
  |  238 |    0 |            if (node != null) { | 
  |  239 |    0 |                if (nodes.containsKey(node.getRouteNodeName())) { | 
  |  240 |    0 |                    return;  | 
  |  241 |     | 
               }  | 
  |  242 |    0 |                nodes.put(node.getRouteNodeName(), node);  | 
  |  243 |    0 |                for (Iterator<RouteNode> iterator = node.getNextNodes().iterator(); iterator.hasNext();) { | 
  |  244 |    0 |                    RouteNode nextNode = iterator.next();  | 
  |  245 |    0 |                    flattenNodeGraph(nodes, nextNode);  | 
  |  246 |    0 |                }  | 
  |  247 |     | 
           } else { | 
  |  248 |    0 |                return;  | 
  |  249 |     | 
           }  | 
  |  250 |    0 |        }          | 
  |  251 |     | 
         | 
  |  252 |     | 
       public List<RouteNodeInstance> getFlattenedNodeInstances(DocumentRouteHeaderValue document, boolean includeProcesses) { | 
  |  253 |    0 |            List<RouteNodeInstance> nodeInstances = new ArrayList<RouteNodeInstance>();  | 
  |  254 |    0 |            Set<Long> visitedNodeInstanceIds = new HashSet<Long>();  | 
  |  255 |    0 |            for (Iterator<RouteNodeInstance> iterator = document.getInitialRouteNodeInstances().iterator(); iterator.hasNext();) { | 
  |  256 |    0 |                RouteNodeInstance initialNodeInstance = iterator.next();  | 
  |  257 |    0 |                flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, initialNodeInstance, includeProcesses);      | 
  |  258 |    0 |            }  | 
  |  259 |    0 |            return nodeInstances;  | 
  |  260 |     | 
       }  | 
  |  261 |     | 
         | 
  |  262 |     | 
           private void flattenNodeInstanceGraph(List<RouteNodeInstance> nodeInstances, Set<Long> visitedNodeInstanceIds, RouteNodeInstance nodeInstance, boolean includeProcesses) { | 
  |  263 |     | 
     | 
  |  264 |    0 |                    if (nodeInstance != null) { | 
  |  265 |    0 |                            if (visitedNodeInstanceIds.contains(nodeInstance.getRouteNodeInstanceId())) { | 
  |  266 |    0 |                                    return;  | 
  |  267 |     | 
                           }  | 
  |  268 |    0 |                            if (includeProcesses && nodeInstance.getProcess() != null) { | 
  |  269 |    0 |                                    flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nodeInstance.getProcess(), includeProcesses);  | 
  |  270 |     | 
                           }  | 
  |  271 |    0 |                            visitedNodeInstanceIds.add(nodeInstance.getRouteNodeInstanceId());  | 
  |  272 |    0 |                            nodeInstances.add(nodeInstance);  | 
  |  273 |    0 |                            for (Iterator<RouteNodeInstance> iterator = nodeInstance.getNextNodeInstances().iterator(); iterator.hasNext();) { | 
  |  274 |    0 |                                    RouteNodeInstance nextNodeInstance = iterator.next();  | 
  |  275 |    0 |                                    flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nextNodeInstance, includeProcesses);  | 
  |  276 |    0 |                            }  | 
  |  277 |     | 
     | 
  |  278 |     | 
                   }  | 
  |  279 |     | 
     | 
  |  280 |    0 |        }        | 
  |  281 |     | 
         | 
  |  282 |     | 
       public NodeGraphSearchResult searchNodeGraph(NodeGraphSearchCriteria criteria) { | 
  |  283 |    0 |                NodeGraphContext context = new NodeGraphContext();  | 
  |  284 |    0 |                if (criteria.getSearchDirection() == NodeGraphSearchCriteria.SEARCH_DIRECTION_BACKWARD) { | 
  |  285 |    0 |                    searchNodeGraphBackward(context, criteria.getMatcher(), null, criteria.getStartingNodeInstances());  | 
  |  286 |     | 
               } else { | 
  |  287 |    0 |                        throw new UnsupportedOperationException("Search feature can only search backward currently."); | 
  |  288 |     | 
               }  | 
  |  289 |    0 |                List exactPath = determineExactPath(context, criteria.getSearchDirection(), criteria.getStartingNodeInstances());  | 
  |  290 |    0 |            return new NodeGraphSearchResult(context.getCurrentNodeInstance(), exactPath);  | 
  |  291 |     | 
       }  | 
  |  292 |     | 
         | 
  |  293 |     | 
       private void searchNodeGraphBackward(NodeGraphContext context, NodeMatcher matcher, RouteNodeInstance previousNodeInstance, Collection nodeInstances) { | 
  |  294 |    0 |            if (nodeInstances == null) { | 
  |  295 |    0 |                return;  | 
  |  296 |     | 
           }  | 
  |  297 |    0 |                for (Iterator iterator = nodeInstances.iterator(); iterator.hasNext();) { | 
  |  298 |    0 |                RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next();  | 
  |  299 |    0 |                context.setPreviousNodeInstance(previousNodeInstance);  | 
  |  300 |    0 |                context.setCurrentNodeInstance(nodeInstance);  | 
  |  301 |    0 |                searchNodeGraphBackward(context, matcher);  | 
  |  302 |    0 |                if (context.getResultNodeInstance() != null) { | 
  |  303 |     | 
                         | 
  |  304 |    0 |                        break;  | 
  |  305 |     | 
               }  | 
  |  306 |    0 |            }  | 
  |  307 |    0 |        }  | 
  |  308 |     | 
         | 
  |  309 |     | 
       private void searchNodeGraphBackward(NodeGraphContext context, NodeMatcher matcher) { | 
  |  310 |    0 |            RouteNodeInstance current = context.getCurrentNodeInstance();  | 
  |  311 |    0 |            int numBranches = current.getNextNodeInstances().size();  | 
  |  312 |     | 
             | 
  |  313 |    0 |            if (numBranches > 1) { | 
  |  314 |     | 
                     | 
  |  315 |    0 |                Integer joinCount = (Integer)context.getSplitState().get(current.getRouteNodeInstanceId());  | 
  |  316 |    0 |                if (joinCount == null) { | 
  |  317 |    0 |                    joinCount = new Integer(0);  | 
  |  318 |     | 
               }  | 
  |  319 |     | 
                 | 
  |  320 |    0 |                if (context.getPreviousNodeInstance() != null) { | 
  |  321 |    0 |                    joinCount = new Integer(joinCount.intValue()+1);  | 
  |  322 |     | 
               }  | 
  |  323 |    0 |                context.getSplitState().put(current.getRouteNodeInstanceId(), joinCount);  | 
  |  324 |     | 
                 | 
  |  325 |    0 |                if (joinCount.intValue() != numBranches) { | 
  |  326 |    0 |                    return;  | 
  |  327 |     | 
               }  | 
  |  328 |     | 
           }  | 
  |  329 |    0 |            if (matcher.isMatch(context)) { | 
  |  330 |    0 |                context.setResultNodeInstance(current);  | 
  |  331 |     | 
           } else { | 
  |  332 |    0 |                context.getVisited().put(current.getRouteNodeInstanceId(), current);  | 
  |  333 |    0 |                searchNodeGraphBackward(context, matcher, current, current.getPreviousNodeInstances());  | 
  |  334 |     | 
           }  | 
  |  335 |    0 |        }  | 
  |  336 |     | 
         | 
  |  337 |     | 
       public List<RouteNodeInstance> getActiveNodeInstances(DocumentRouteHeaderValue document, String nodeName) { | 
  |  338 |    0 |                    Collection<RouteNodeInstance> activeNodes = getActiveNodeInstances(document.getRouteHeaderId());  | 
  |  339 |    0 |                    List<RouteNodeInstance> foundNodes = new ArrayList<RouteNodeInstance>();  | 
  |  340 |    0 |            for (Iterator<RouteNodeInstance> iterator = activeNodes.iterator(); iterator.hasNext();) { | 
  |  341 |    0 |                RouteNodeInstance nodeInstance = iterator.next();  | 
  |  342 |    0 |                if (nodeInstance.getName().equals(nodeName)) { | 
  |  343 |    0 |                        foundNodes.add(nodeInstance);  | 
  |  344 |     | 
               }  | 
  |  345 |    0 |            }  | 
  |  346 |    0 |            return foundNodes;  | 
  |  347 |     | 
       }  | 
  |  348 |     | 
         | 
  |  349 |     | 
           private List determineExactPath(NodeGraphContext context, int searchDirection, Collection startingNodeInstances) { | 
  |  350 |    0 |                List exactPath = new ArrayList();  | 
  |  351 |    0 |                if (context.getResultNodeInstance() == null) { | 
  |  352 |    0 |                        exactPath.addAll(context.getVisited().values());  | 
  |  353 |     | 
               } else { | 
  |  354 |    0 |                        determineExactPath(exactPath, new HashMap<Long, RouteNodeInstance>(), startingNodeInstances, context.getResultNodeInstance());  | 
  |  355 |     | 
               }  | 
  |  356 |    0 |                if (NodeGraphSearchCriteria.SEARCH_DIRECTION_FORWARD == searchDirection) { | 
  |  357 |    0 |                        Collections.sort(exactPath, NODE_INSTANCE_BACKWARD_SORT);  | 
  |  358 |     | 
               } else { | 
  |  359 |    0 |                        Collections.sort(exactPath, NODE_INSTANCE_FORWARD_SORT);  | 
  |  360 |     | 
               }  | 
  |  361 |    0 |                return exactPath;  | 
  |  362 |     | 
       }  | 
  |  363 |     | 
         | 
  |  364 |     | 
       private void determineExactPath(List exactPath, Map<Long, RouteNodeInstance> visited, Collection startingNodeInstances, RouteNodeInstance nodeInstance) { | 
  |  365 |    0 |                if (nodeInstance == null) { | 
  |  366 |    0 |                        return;  | 
  |  367 |     | 
               }  | 
  |  368 |    0 |                if (visited.containsKey(nodeInstance.getRouteNodeInstanceId())) { | 
  |  369 |    0 |                        return;  | 
  |  370 |     | 
               }  | 
  |  371 |    0 |                visited.put(nodeInstance.getRouteNodeInstanceId(), nodeInstance);  | 
  |  372 |    0 |                exactPath.add(nodeInstance);  | 
  |  373 |    0 |                for (Iterator iterator = startingNodeInstances.iterator(); iterator.hasNext(); ) { | 
  |  374 |    0 |                            RouteNodeInstance startingNode = (RouteNodeInstance) iterator.next();  | 
  |  375 |    0 |                            if (startingNode.getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())) { | 
  |  376 |    0 |                                    return;  | 
  |  377 |     | 
                           }  | 
  |  378 |    0 |                    }  | 
  |  379 |    0 |                for (Iterator<RouteNodeInstance> iterator = nodeInstance.getNextNodeInstances().iterator(); iterator.hasNext(); ) { | 
  |  380 |    0 |                            RouteNodeInstance nextNodeInstance = iterator.next();  | 
  |  381 |    0 |                            determineExactPath(exactPath, visited, startingNodeInstances, nextNodeInstance);  | 
  |  382 |    0 |                    }  | 
  |  383 |    0 |        }  | 
  |  384 |     | 
         | 
  |  385 |     | 
            | 
  |  386 |     | 
         | 
  |  387 |     | 
     | 
  |  388 |     | 
     | 
  |  389 |     | 
     | 
  |  390 |     | 
     | 
  |  391 |     | 
     | 
  |  392 |    0 |        private static class RouteNodeSorter implements Comparator { | 
  |  393 |     | 
           public int compare(Object arg0, Object arg1) { | 
  |  394 |    0 |                RouteNode rn1 = (RouteNode)arg0;  | 
  |  395 |    0 |                RouteNode rn2 = (RouteNode)arg1;  | 
  |  396 |    0 |                return rn1.getRouteNodeId().compareTo(rn2.getRouteNodeId());  | 
  |  397 |     | 
           }  | 
  |  398 |     | 
       }  | 
  |  399 |     | 
         | 
  |  400 |    0 |        private static class NodeInstanceIdSorter implements Comparator { | 
  |  401 |     | 
           public int compare(Object arg0, Object arg1) { | 
  |  402 |    0 |                RouteNodeInstance nodeInstance1 = (RouteNodeInstance)arg0;  | 
  |  403 |    0 |                RouteNodeInstance nodeInstance2 = (RouteNodeInstance)arg1;  | 
  |  404 |    0 |                return nodeInstance1.getRouteNodeInstanceId().compareTo(nodeInstance2.getRouteNodeInstanceId());  | 
  |  405 |     | 
           }  | 
  |  406 |     | 
       }  | 
  |  407 |     | 
         | 
  |  408 |     | 
         | 
  |  409 |     | 
       public void deleteByRouteNodeInstance(RouteNodeInstance routeNodeInstance){ | 
  |  410 |     | 
                 | 
  |  411 |    0 |                routeNodeDAO.deleteLinksToPreNodeInstances(routeNodeInstance);  | 
  |  412 |     | 
                 | 
  |  413 |    0 |                routeNodeDAO.deleteRouteNodeInstancesHereAfter(routeNodeInstance);  | 
  |  414 |    0 |        }  | 
  |  415 |     | 
         | 
  |  416 |     | 
       public void deleteNodeStateById(Long nodeStateId){ | 
  |  417 |    0 |                routeNodeDAO.deleteNodeStateById(nodeStateId);  | 
  |  418 |    0 |        }  | 
  |  419 |     | 
         | 
  |  420 |     | 
       public void deleteNodeStates(List statesToBeDeleted){ | 
  |  421 |    0 |                routeNodeDAO.deleteNodeStates(statesToBeDeleted);  | 
  |  422 |    0 |        }  | 
  |  423 |     | 
         | 
  |  424 |     | 
         | 
  |  425 |     | 
     | 
  |  426 |     | 
     | 
  |  427 |     | 
       public void revokeNodeInstance(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) { | 
  |  428 |    0 |                if (document == null) { | 
  |  429 |    0 |                        throw new IllegalArgumentException("Document must not be null."); | 
  |  430 |     | 
               }  | 
  |  431 |    0 |                    if (nodeInstance == null || nodeInstance.getRouteNodeInstanceId() == null) { | 
  |  432 |    0 |                            throw new IllegalArgumentException("In order to revoke a final approval node the node instance must be persisent and have an id."); | 
  |  433 |     | 
                   }  | 
  |  434 |     | 
                     | 
  |  435 |    0 |                Branch rootBranch = document.getRootBranch();  | 
  |  436 |    0 |                BranchState state = null;  | 
  |  437 |    0 |                if (rootBranch != null) { | 
  |  438 |    0 |                    state = rootBranch.getBranchState(REVOKED_NODE_INSTANCES_STATE_KEY);  | 
  |  439 |     | 
               }  | 
  |  440 |    0 |                if (state == null) { | 
  |  441 |    0 |                        state = new BranchState();  | 
  |  442 |    0 |                        state.setKey(REVOKED_NODE_INSTANCES_STATE_KEY);  | 
  |  443 |    0 |                        state.setValue(""); | 
  |  444 |    0 |                        rootBranch.addBranchState(state);  | 
  |  445 |     | 
               }  | 
  |  446 |    0 |                if (state.getValue() == null) { | 
  |  447 |    0 |                        state.setValue(""); | 
  |  448 |     | 
               }  | 
  |  449 |    0 |                state.setValue(state.getValue() + nodeInstance.getRouteNodeInstanceId() + ",");  | 
  |  450 |    0 |                save(rootBranch);  | 
  |  451 |    0 |            }  | 
  |  452 |     | 
     | 
  |  453 |     | 
         | 
  |  454 |     | 
     | 
  |  455 |     | 
     | 
  |  456 |     | 
     | 
  |  457 |     | 
           public List getRevokedNodeInstances(DocumentRouteHeaderValue document) { | 
  |  458 |    0 |                    if (document == null) { | 
  |  459 |    0 |                        throw new IllegalArgumentException("Document must not be null."); | 
  |  460 |     | 
               }  | 
  |  461 |    0 |                    List<RouteNodeInstance> revokedNodeInstances = new ArrayList<RouteNodeInstance>();  | 
  |  462 |     | 
                 | 
  |  463 |    0 |                Branch rootBranch = document.getRootBranch();  | 
  |  464 |    0 |                BranchState state = null;  | 
  |  465 |    0 |                if (rootBranch != null) { | 
  |  466 |    0 |                    state = rootBranch.getBranchState(REVOKED_NODE_INSTANCES_STATE_KEY);  | 
  |  467 |     | 
               }  | 
  |  468 |    0 |                if (state == null || org.apache.commons.lang.StringUtils.isEmpty(state.getValue())) { | 
  |  469 |    0 |                        return revokedNodeInstances;  | 
  |  470 |     | 
               }  | 
  |  471 |    0 |                String[] revokedNodes = state.getValue().split(","); | 
  |  472 |    0 |                for (int index = 0; index < revokedNodes.length; index++) { | 
  |  473 |    0 |                            String revokedNodeInstanceIdValue = revokedNodes[index];  | 
  |  474 |    0 |                            Long revokedNodeInstanceId = Long.valueOf(revokedNodeInstanceIdValue);  | 
  |  475 |    0 |                            RouteNodeInstance revokedNodeInstance = findRouteNodeInstanceById(revokedNodeInstanceId);  | 
  |  476 |    0 |                            if (revokedNodeInstance == null) { | 
  |  477 |    0 |                                    LOG.warn("Could not locate revoked RouteNodeInstance with the given id: " + revokedNodeInstanceId); | 
  |  478 |     | 
                           } else { | 
  |  479 |    0 |                                    revokedNodeInstances.add(revokedNodeInstance);  | 
  |  480 |     | 
                           }  | 
  |  481 |     | 
                   }  | 
  |  482 |    0 |                return revokedNodeInstances;  | 
  |  483 |     | 
           }  | 
  |  484 |     | 
         | 
  |  485 |     | 
         | 
  |  486 |     | 
   }  |