Coverage Report - org.kuali.rice.kew.engine.node.RouteNodeUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteNodeUtils
0%
0/64
0%
0/34
3.444
RouteNodeUtils$1
N/A
N/A
3.444
RouteNodeUtils$RouteNodeSorter
0%
0/4
N/A
3.444
 
 1  
 /*
 2  
  * Copyright 2007-2008 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;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 20  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 21  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 22  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 23  
 import org.w3c.dom.Document;
 24  
 import org.xml.sax.InputSource;
 25  
 
 26  
 import javax.xml.parsers.DocumentBuilder;
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import java.io.StringReader;
 29  
 import java.util.*;
 30  
 
 31  
 /**
 32  
  * A simple class for performing operations on RouteNode.  In particular, this class provides some
 33  
  * convenience methods for processing custom RouteNode XML content fragments. 
 34  
  * 
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  *
 37  
  */
 38  0
 public class RouteNodeUtils {
 39  
 
 40  
         /**
 41  
          * Searches a RouteNode's "contentFragment" (it's XML definition) for an XML element with
 42  
          * the given name and returns it's value.
 43  
          * 
 44  
          * <p>For example, in a node with the following definition:
 45  
          *
 46  
          * <pre><routeNode name="...">
 47  
          *   ...
 48  
          *   <myCustomProperty>propertyValue</myCustomProperty>
 49  
          * </routeNode></pre>
 50  
          * 
 51  
          * <p>An invocation of getValueOfCustomProperty(routeNode, "myCustomProperty") would return
 52  
          * "propertyValue".
 53  
          * 
 54  
          * @param routeNode RouteNode to examine
 55  
          * @param propertyName name of the XML element to search for
 56  
          * 
 57  
          * @return the value of the XML element, or null if it could not be located
 58  
          */
 59  
         public static String getValueOfCustomProperty(RouteNode routeNode, String propertyName) {
 60  0
                 String contentFragment = routeNode.getContentFragment();
 61  0
                 String elementValue = null;
 62  0
                 if (!StringUtils.isBlank(contentFragment)) {
 63  
                         try {
 64  0
                                 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 65  0
                                 Document document = db.parse(new InputSource(new StringReader(contentFragment)));        
 66  0
                                 elementValue = XPathHelper.newXPath().evaluate("//" + propertyName, document);
 67  0
                         } catch (Exception e) {
 68  0
                                 throw new RiceRuntimeException("Error when attempting to parse Document Type content fragment for property name: " + propertyName, e);
 69  0
                         }
 70  
                 }
 71  0
                 return elementValue;
 72  
         }
 73  
         
 74  
         public static List<RouteNodeInstance> getFlattenedNodeInstances(DocumentRouteHeaderValue document, boolean includeProcesses) {
 75  0
         List<RouteNodeInstance> nodeInstances = new ArrayList<RouteNodeInstance>();
 76  0
         Set<Long> visitedNodeInstanceIds = new HashSet<Long>();
 77  0
         for (RouteNodeInstance initialNodeInstance : document.getInitialRouteNodeInstances())
 78  
         {
 79  0
             flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, initialNodeInstance, includeProcesses);
 80  
         }
 81  0
         return nodeInstances;
 82  
     }
 83  
     
 84  
     private static void flattenNodeInstanceGraph(List<RouteNodeInstance> nodeInstances, Set<Long> visitedNodeInstanceIds, RouteNodeInstance nodeInstance, boolean includeProcesses) {
 85  0
         if (visitedNodeInstanceIds.contains(nodeInstance.getRouteNodeInstanceId())) {
 86  0
             return;
 87  
         }
 88  0
         if (includeProcesses && nodeInstance.getProcess() != null) {
 89  0
             flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nodeInstance.getProcess(), includeProcesses);
 90  
         }
 91  0
         visitedNodeInstanceIds.add(nodeInstance.getRouteNodeInstanceId());
 92  0
         nodeInstances.add(nodeInstance);
 93  0
         for (RouteNodeInstance nextNodeInstance : nodeInstance.getNextNodeInstances())
 94  
         {
 95  0
             flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nextNodeInstance, includeProcesses);
 96  
         }
 97  0
     }
 98  
     
 99  
     public static List<RouteNode> getFlattenedNodes(DocumentType documentType, boolean climbHierarchy) {
 100  0
         List<RouteNode> nodes = new ArrayList<RouteNode>();
 101  0
         if (!documentType.isRouteInherited() || climbHierarchy) {
 102  0
             for (Object o : documentType.getProcesses())
 103  
             {
 104  0
                 Process process = (Process) o;
 105  0
                 nodes.addAll(getFlattenedNodes(process));
 106  0
             }
 107  
         }
 108  0
         Collections.sort(nodes, new RouteNodeSorter());
 109  0
         return nodes;
 110  
     }
 111  
     
 112  
     public static List<RouteNode> getFlattenedNodes(Process process) {
 113  0
         Map<String, RouteNode> nodesMap = new HashMap<String, RouteNode>();
 114  0
         if (process.getInitialRouteNode() != null) {
 115  0
             flattenNodeGraph(nodesMap, process.getInitialRouteNode());
 116  0
             List<RouteNode> nodes = new ArrayList<RouteNode>(nodesMap.values());
 117  0
             Collections.sort(nodes, new RouteNodeSorter());
 118  0
             return nodes;
 119  
         } else {
 120  0
             List<RouteNode> nodes = new ArrayList<RouteNode>();
 121  0
             nodes.add(new RouteNode());
 122  0
             return nodes;
 123  
         }
 124  
 
 125  
     }
 126  
     
 127  
     /**
 128  
      * Recursively walks the node graph and builds up the map.  Uses a map because we will
 129  
      * end up walking through duplicates, as is the case with Join nodes.
 130  
      * @param nodes map
 131  
      * @param node graph
 132  
      */
 133  
     private static void flattenNodeGraph(Map<String, RouteNode> nodes, RouteNode node) {
 134  0
         if (node != null) {
 135  0
             if (nodes.containsKey(node.getRouteNodeName())) {
 136  0
                 return;
 137  
             }
 138  0
             nodes.put(node.getRouteNodeName(), node);
 139  0
             for (RouteNode nextNode : node.getNextNodes())
 140  
             {
 141  0
                 flattenNodeGraph(nodes, nextNode);
 142  
             }
 143  
         } else {
 144  0
             return;
 145  
         }
 146  0
     }
 147  
     
 148  
     /**
 149  
      * Sorts by RouteNodeId or the order the nodes will be evaluated in *roughly*.  This is 
 150  
      * for display purposes when rendering a flattened list of nodes.
 151  
      * 
 152  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 153  
      */
 154  0
     private static class RouteNodeSorter implements Comparator {
 155  
         public int compare(Object arg0, Object arg1) {
 156  0
             RouteNode rn1 = (RouteNode)arg0;
 157  0
             RouteNode rn2 = (RouteNode)arg1;
 158  0
             return rn1.getRouteNodeId().compareTo(rn2.getRouteNodeId());
 159  
         }
 160  
     }
 161  
     
 162  
     public static List<RouteNodeInstance> getActiveNodeInstances(DocumentRouteHeaderValue document) {
 163  0
         List<RouteNodeInstance> flattenedNodeInstances = getFlattenedNodeInstances(document, true);
 164  0
         List<RouteNodeInstance> activeNodeInstances = new ArrayList<RouteNodeInstance>();
 165  0
         for (RouteNodeInstance nodeInstance : flattenedNodeInstances)
 166  
         {
 167  0
             if (nodeInstance.isActive())
 168  
             {
 169  0
                 activeNodeInstances.add(nodeInstance);
 170  
             }
 171  
         }
 172  0
         return activeNodeInstances;
 173  
     }
 174  
     
 175  
     public static RouteNodeInstance findRouteNodeInstanceById(Long nodeInstanceId, DocumentRouteHeaderValue document) {
 176  0
             List<RouteNodeInstance> flattenedNodeInstances = getFlattenedNodeInstances(document, true);
 177  0
             RouteNodeInstance niRet = null;
 178  0
         for (RouteNodeInstance nodeInstance : flattenedNodeInstances)
 179  
         {
 180  0
             if (nodeInstanceId.equals(nodeInstance.getRouteNodeInstanceId()))
 181  
             {
 182  0
                 niRet = nodeInstance;
 183  0
                 break;
 184  
             }
 185  
         }
 186  0
         return niRet;
 187  
     }
 188  
     
 189  
     
 190  
         
 191  
 }