1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.engine.node; |
17 | |
|
18 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
19 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
20 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
21 | |
import org.kuali.rice.kew.api.exception.ResourceUnavailableException; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Collections; |
25 | |
import java.util.Comparator; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class NodeType { |
39 | |
|
40 | |
private static final String SIMPLE_NAME = "simple"; |
41 | |
private static final String JOIN_NAME = "join"; |
42 | |
private static final String SPLIT_NAME = "split"; |
43 | |
private static final String SUB_PROCESS_NAME = "process"; |
44 | |
private static final String DYNAMIC_NAME = "dynamic"; |
45 | |
private static final String START_NAME = "start"; |
46 | |
private static final String REQUEST_ACTIVATION_NAME = "requestActivation"; |
47 | |
private static final String REQUESTS_NAME = "requests"; |
48 | |
private static final String ROLE_NAME = "role"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | private static final List typeList = new ArrayList(); |
54 | 0 | private static final Comparator depthComparator = new ExtensionDepthComparator(); |
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public static final NodeType SIMPLE = new NodeType(SIMPLE_NAME, SimpleNode.class, null); |
59 | 0 | public static final NodeType SPLIT = new NodeType(SPLIT_NAME, SplitNode.class, SimpleSplitNode.class); |
60 | 0 | public static final NodeType JOIN = new NodeType(JOIN_NAME, JoinNode.class, SimpleJoinNode.class); |
61 | 0 | public static final NodeType SUB_PROCESS = new NodeType(SUB_PROCESS_NAME, SubProcessNode.class, SimpleSubProcessNode.class); |
62 | 0 | public static final NodeType DYNAMIC = new NodeType(DYNAMIC_NAME, DynamicNode.class, null); |
63 | |
|
64 | |
|
65 | 0 | public static final NodeType REQUEST_ACTIVATION = new NodeType(SIMPLE, REQUEST_ACTIVATION_NAME, RequestActivationNode.class, RequestActivationNode.class); |
66 | 0 | public static final NodeType START = new NodeType(REQUEST_ACTIVATION, START_NAME, InitialNode.class, InitialNode.class); |
67 | 0 | public static final NodeType REQUESTS = new NodeType(REQUEST_ACTIVATION, REQUESTS_NAME, RequestsNode.class, RequestsNode.class); |
68 | 0 | public static final NodeType ROLE = new NodeType(REQUESTS, ROLE_NAME, RoleNode.class, RoleNode.class); |
69 | |
|
70 | |
private NodeType extensionBase; |
71 | |
private String name; |
72 | |
private Class baseClass; |
73 | |
private Class defaultClass; |
74 | 0 | private int extensionDepth = 0; |
75 | |
|
76 | |
private NodeType(String name, Class baseClass, Class defaultClass) { |
77 | 0 | this(null, name, baseClass, defaultClass); |
78 | 0 | } |
79 | |
|
80 | 0 | private NodeType(NodeType extensionBase, String name, Class baseClass, Class defaultClass) { |
81 | 0 | this.extensionBase = extensionBase; |
82 | 0 | this.name = name; |
83 | 0 | this.baseClass = baseClass; |
84 | 0 | this.defaultClass = defaultClass; |
85 | 0 | while (extensionBase != null) { |
86 | 0 | extensionDepth++; |
87 | 0 | extensionBase = extensionBase.getExtensionBase(); |
88 | |
} |
89 | 0 | typeList.add(this); |
90 | |
|
91 | 0 | Collections.sort(typeList, depthComparator); |
92 | 0 | } |
93 | |
|
94 | |
public NodeType getExtensionBase() { |
95 | 0 | return extensionBase; |
96 | |
} |
97 | |
|
98 | |
public Class getBaseClass() { |
99 | 0 | return baseClass; |
100 | |
} |
101 | |
|
102 | |
public Class getDefaultClass() { |
103 | 0 | return defaultClass; |
104 | |
} |
105 | |
|
106 | |
public String getName() { |
107 | 0 | return name; |
108 | |
} |
109 | |
|
110 | |
public int getExtensionDepth() { |
111 | 0 | return extensionDepth; |
112 | |
} |
113 | |
|
114 | |
public boolean isAssignableFrom(Class typeClass) { |
115 | 0 | return getBaseClass().isAssignableFrom(typeClass); |
116 | |
} |
117 | |
|
118 | |
public boolean isAssignableFrom(NodeType type) { |
119 | 0 | return getBaseClass().isAssignableFrom(type.getBaseClass()); |
120 | |
} |
121 | |
|
122 | |
public boolean isInstanceOf(Object object) { |
123 | 0 | return ClassLoaderUtils.isInstanceOf(object, getBaseClass()); |
124 | |
} |
125 | |
|
126 | |
public boolean isTypeOf(Class typeClass) { |
127 | 0 | return typeClass.isAssignableFrom(getBaseClass()); |
128 | |
} |
129 | |
|
130 | |
public boolean isTypeOf(NodeType type) { |
131 | 0 | return type.getBaseClass().isAssignableFrom(getBaseClass()); |
132 | |
} |
133 | |
|
134 | |
public boolean isCustomNode(String className) { |
135 | 0 | return getDefaultClass() == null || !getDefaultClass().getName().equals(className); |
136 | |
} |
137 | |
|
138 | |
public boolean equals(Object object) { |
139 | 0 | if (object instanceof NodeType) { |
140 | 0 | return ((NodeType)object).name.equals(name); |
141 | |
} |
142 | 0 | return false; |
143 | |
} |
144 | |
|
145 | |
public int hashCode() { |
146 | 0 | return name.hashCode(); |
147 | |
} |
148 | |
|
149 | |
public static NodeType fromClassName(String className) throws ResourceUnavailableException { |
150 | |
|
151 | 0 | Object typeObject = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(className)); |
152 | 0 | if (typeObject == null) { |
153 | 0 | throw new ResourceUnavailableException("Could not locate the node with the given class name '" + className + "'."); |
154 | |
} |
155 | |
|
156 | |
|
157 | 0 | for (Iterator iterator = typeList.iterator(); iterator.hasNext();) { |
158 | 0 | NodeType type = (NodeType) iterator.next(); |
159 | |
|
160 | |
|
161 | 0 | if (type.isInstanceOf(typeObject)) { |
162 | 0 | return type; |
163 | |
} |
164 | 0 | } |
165 | 0 | return null; |
166 | |
} |
167 | |
|
168 | |
public static NodeType fromNode(RouteNode node) throws ResourceUnavailableException { |
169 | 0 | return fromClassName(node.getNodeType()); |
170 | |
} |
171 | |
|
172 | |
public static NodeType fromNodeInstance(RouteNodeInstance nodeInstance) throws ResourceUnavailableException { |
173 | 0 | return fromNode(nodeInstance.getRouteNode()); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | 0 | private static class ExtensionDepthComparator implements Comparator { |
180 | |
|
181 | |
public int compare(Object object1, Object object2) { |
182 | 0 | NodeType type1 = (NodeType)object1; |
183 | 0 | NodeType type2 = (NodeType)object2; |
184 | 0 | if (type1.getExtensionDepth() > type2.getExtensionDepth()) { |
185 | 0 | return -1; |
186 | 0 | } else if (type1.getExtensionDepth() < type2.getExtensionDepth()) { |
187 | 0 | return 1; |
188 | |
} |
189 | 0 | return 0; |
190 | |
} |
191 | |
|
192 | |
} |
193 | |
|
194 | |
public static List<NodeType> getTypeList(){ |
195 | 0 | return typeList; |
196 | |
} |
197 | |
|
198 | |
} |