Coverage Report - org.kuali.rice.kew.engine.node.NodeType
 
Classes in this File Line Coverage Branch Coverage Complexity
NodeType
0%
0/52
0%
0/14
1.65
NodeType$1
N/A
N/A
1.65
NodeType$ExtensionDepthComparator
0%
0/8
0%
0/4
1.65
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.engine.node;
 18  
 
 19  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 20  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 21  
 import org.kuali.rice.core.api.util.ClassLoaderUtils;
 22  
 import org.kuali.rice.kew.exception.ResourceUnavailableException;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.Comparator;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 
 30  
 
 31  
 /**
 32  
  * A typesafe enumeration defining the various types of Nodes in the Workflow Engine.
 33  
  * This class was added to aid in unifying the node type concept across multiple
 34  
  * components within the system.  It also defines type hierarchys for the various
 35  
  * node types.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  
 public class NodeType {
 40  
 
 41  
     private static final String SIMPLE_NAME = "simple";
 42  
     private static final String JOIN_NAME = "join";
 43  
     private static final String SPLIT_NAME = "split";
 44  
     private static final String SUB_PROCESS_NAME = "process";
 45  
     private static final String DYNAMIC_NAME = "dynamic";
 46  
     private static final String START_NAME = "start";
 47  
     private static final String REQUEST_ACTIVATION_NAME = "requestActivation";
 48  
     private static final String REQUESTS_NAME = "requests";
 49  
     private static final String ROLE_NAME = "role";
 50  
 
 51  
     /**
 52  
      * A Sorted List containing the NodeTypes in order sorted by largest extension depth first, i.e. in bottom-up extension order.
 53  
      */
 54  0
     private static final List typeList = new ArrayList();
 55  0
     private static final Comparator depthComparator = new ExtensionDepthComparator();
 56  
 
 57  
 
 58  
     // primitive node types
 59  0
     public static final NodeType SIMPLE = new NodeType(SIMPLE_NAME, SimpleNode.class, null);
 60  0
     public static final NodeType SPLIT = new NodeType(SPLIT_NAME, SplitNode.class, SimpleSplitNode.class);
 61  0
     public static final NodeType JOIN = new NodeType(JOIN_NAME, JoinNode.class, SimpleJoinNode.class);
 62  0
     public static final NodeType SUB_PROCESS = new NodeType(SUB_PROCESS_NAME, SubProcessNode.class, SimpleSubProcessNode.class);
 63  0
     public static final NodeType DYNAMIC = new NodeType(DYNAMIC_NAME, DynamicNode.class, null);
 64  
 
 65  
     // derivative node types
 66  0
     public static final NodeType REQUEST_ACTIVATION = new NodeType(SIMPLE, REQUEST_ACTIVATION_NAME, RequestActivationNode.class, RequestActivationNode.class);
 67  0
     public static final NodeType START = new NodeType(REQUEST_ACTIVATION, START_NAME, InitialNode.class, InitialNode.class);
 68  0
     public static final NodeType REQUESTS = new NodeType(REQUEST_ACTIVATION, REQUESTS_NAME, RequestsNode.class, RequestsNode.class);
 69  0
     public static final NodeType ROLE = new NodeType(REQUESTS, ROLE_NAME, RoleNode.class, RoleNode.class);
 70  
 
 71  
     private NodeType extensionBase;
 72  
     private String name;
 73  
     private Class baseClass;
 74  
     private Class defaultClass;
 75  0
     private int extensionDepth = 0;
 76  
 
 77  
     private NodeType(String name, Class baseClass, Class defaultClass) {
 78  0
         this(null, name, baseClass, defaultClass);
 79  0
     }
 80  
 
 81  0
     private NodeType(NodeType extensionBase, String name, Class baseClass, Class defaultClass) {
 82  0
         this.extensionBase = extensionBase;
 83  0
         this.name = name;
 84  0
         this.baseClass = baseClass;
 85  0
         this.defaultClass = defaultClass;
 86  0
         while (extensionBase != null) {
 87  0
             extensionDepth++;
 88  0
             extensionBase = extensionBase.getExtensionBase();
 89  
         }
 90  0
         typeList.add(this);
 91  
         // keep the list sorted
 92  0
         Collections.sort(typeList, depthComparator);
 93  0
     }
 94  
 
 95  
     public NodeType getExtensionBase() {
 96  0
         return extensionBase;
 97  
     }
 98  
 
 99  
     public Class getBaseClass() {
 100  0
         return baseClass;
 101  
     }
 102  
 
 103  
     public Class getDefaultClass() {
 104  0
         return defaultClass;
 105  
     }
 106  
 
 107  
     public String getName() {
 108  0
         return name;
 109  
     }
 110  
 
 111  
     public int getExtensionDepth() {
 112  0
         return extensionDepth;
 113  
     }
 114  
 
 115  
     public boolean isAssignableFrom(Class typeClass) {
 116  0
         return getBaseClass().isAssignableFrom(typeClass);
 117  
     }
 118  
 
 119  
     public boolean isAssignableFrom(NodeType type) {
 120  0
         return getBaseClass().isAssignableFrom(type.getBaseClass());
 121  
     }
 122  
 
 123  
     public boolean isInstanceOf(Object object) {
 124  0
             return ClassLoaderUtils.isInstanceOf(object, getBaseClass());
 125  
     }
 126  
 
 127  
     public boolean isTypeOf(Class typeClass) {
 128  0
         return typeClass.isAssignableFrom(getBaseClass());
 129  
     }
 130  
 
 131  
     public boolean isTypeOf(NodeType type) {
 132  0
         return type.getBaseClass().isAssignableFrom(getBaseClass());
 133  
     }
 134  
 
 135  
     public boolean isCustomNode(String className) {
 136  0
         return getDefaultClass() == null || !getDefaultClass().getName().equals(className);
 137  
     }
 138  
 
 139  
     public boolean equals(Object object) {
 140  0
         if (object instanceof NodeType) {
 141  0
             return ((NodeType)object).name.equals(name);
 142  
         }
 143  0
         return false;
 144  
     }
 145  
 
 146  
     public int hashCode() {
 147  0
         return name.hashCode();
 148  
     }
 149  
 
 150  
     public static NodeType fromClassName(String className) throws ResourceUnavailableException {
 151  
         //Class typeClass = SpringServiceLocator.getExtensionService().loadClass(className);
 152  0
         Object typeObject = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(className));
 153  0
         if (typeObject == null) {
 154  0
                 throw new ResourceUnavailableException("Could not locate the node with the given class name '" + className + "'.");
 155  
         }
 156  
         //Class typeClass = typeObject.getClass();
 157  
             // depends upon the proper sorting of typeList
 158  0
         for (Iterator iterator = typeList.iterator(); iterator.hasNext();) {
 159  0
             NodeType type = (NodeType) iterator.next();
 160  
             //if (type.isAssignableFrom(typeClass)) {
 161  
             //if (ClassLoaderUtils.isInstanceOf(typeObject, type))
 162  0
             if (type.isInstanceOf(typeObject)) {
 163  0
                 return type;
 164  
             }
 165  0
         }
 166  0
         return null;
 167  
     }
 168  
 
 169  
     public static NodeType fromNode(RouteNode node) throws ResourceUnavailableException {
 170  0
         return fromClassName(node.getNodeType());
 171  
     }
 172  
 
 173  
     public static NodeType fromNodeInstance(RouteNodeInstance nodeInstance) throws ResourceUnavailableException {
 174  0
         return fromNode(nodeInstance.getRouteNode());
 175  
     }
 176  
 
 177  
     /**
 178  
      * Sorts in descending extension-depth order.
 179  
      */
 180  0
     private static class ExtensionDepthComparator implements Comparator {
 181  
 
 182  
         public int compare(Object object1, Object object2) {
 183  0
             NodeType type1 = (NodeType)object1;
 184  0
             NodeType type2 = (NodeType)object2;
 185  0
             if (type1.getExtensionDepth() > type2.getExtensionDepth()) {
 186  0
                 return -1;
 187  0
             } else if (type1.getExtensionDepth() < type2.getExtensionDepth()) {
 188  0
                 return 1;
 189  
             }
 190  0
             return 0;
 191  
         }
 192  
 
 193  
     }
 194  
 
 195  
     public static List<NodeType> getTypeList(){
 196  0
             return typeList;
 197  
     }
 198  
 
 199  
 }