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