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