1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.engine.node; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collections; |
21 | |
import java.util.Comparator; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.kuali.rice.core.reflect.ObjectDefinition; |
26 | |
import org.kuali.rice.core.resourceloader.GlobalResourceLoader; |
27 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
28 | |
import org.kuali.rice.kew.exception.ResourceUnavailableException; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
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 | |
|
53 | |
|
54 | 0 | private static final List typeList = new ArrayList(); |
55 | 0 | private static final Comparator depthComparator = new ExtensionDepthComparator(); |
56 | |
|
57 | |
|
58 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
157 | |
|
158 | 0 | for (Iterator iterator = typeList.iterator(); iterator.hasNext();) { |
159 | 0 | NodeType type = (NodeType) iterator.next(); |
160 | |
|
161 | |
|
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 | |
|
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 | |
} |