1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.role; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
24 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
25 | |
import org.kuali.rice.core.api.mo.common.Attributes; |
26 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
27 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
28 | |
import org.kuali.rice.core.util.AttributeSet; |
29 | |
import org.kuali.rice.kew.actionrequest.ActionRequestFactory; |
30 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
31 | |
import org.kuali.rice.kew.engine.RouteContext; |
32 | |
import org.kuali.rice.kew.engine.node.RouteNodeUtils; |
33 | |
import org.kuali.rice.kew.exception.WorkflowException; |
34 | |
import org.kuali.rice.kew.routemodule.RouteModule; |
35 | |
import org.kuali.rice.kew.rule.XmlConfiguredAttribute; |
36 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
37 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
38 | |
import org.kuali.rice.kew.util.KEWConstants; |
39 | |
import org.kuali.rice.kew.util.ResponsibleParty; |
40 | |
import org.kuali.rice.kim.api.responsibility.ResponsibilityAction; |
41 | |
import org.kuali.rice.kim.api.responsibility.ResponsibilityService; |
42 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
43 | |
import org.kuali.rice.kim.util.KimConstants; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class RoleRouteModule implements RouteModule { |
52 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RoleRouteModule.class); |
53 | |
|
54 | |
protected static final String QUALIFIER_RESOLVER_ELEMENT = KEWConstants.ROLEROUTE_QUALIFIER_RESOLVER_ELEMENT; |
55 | |
protected static final String QUALIFIER_RESOLVER_CLASS_ELEMENT = KEWConstants.ROLEROUTE_QUALIFIER_RESOLVER_CLASS_ELEMENT; |
56 | |
protected static final String RESPONSIBILITY_TEMPLATE_NAME_ELEMENT = KEWConstants.ROLEROUTE_RESPONSIBILITY_TEMPLATE_NAME_ELEMENT; |
57 | |
protected static final String NAMESPACE_ELEMENT = KEWConstants.ROLEROUTE_NAMESPACE_ELEMENT; |
58 | |
|
59 | |
private static ResponsibilityService responsibilityService; |
60 | |
|
61 | |
private String qualifierResolverName; |
62 | |
private String qualifierResolverClassName; |
63 | |
private String responsibilityTemplateName; |
64 | |
private String namespace; |
65 | |
|
66 | |
@SuppressWarnings("unchecked") |
67 | |
public List<ActionRequestValue> findActionRequests(RouteContext context) |
68 | |
throws Exception { |
69 | |
|
70 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(context.getDocument(), context.getNodeInstance()); |
71 | |
|
72 | 0 | QualifierResolver qualifierResolver = loadQualifierResolver(context); |
73 | 0 | List<AttributeSet> qualifiers = qualifierResolver.resolve(context); |
74 | 0 | String responsibilityTemplateName = loadResponsibilityTemplateName(context); |
75 | 0 | String namespaceCode = loadNamespace(context); |
76 | 0 | AttributeSet responsibilityDetails = loadResponsibilityDetails(context); |
77 | 0 | if (LOG.isDebugEnabled()) { |
78 | 0 | logQualifierCheck(namespaceCode, responsibilityTemplateName, responsibilityDetails, qualifiers); |
79 | |
} |
80 | 0 | if ( qualifiers != null ) { |
81 | 0 | for (AttributeSet qualifier : qualifiers) { |
82 | 0 | if ( qualifier.containsKey( KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER ) ) { |
83 | 0 | responsibilityDetails.put(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, qualifier.get(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER)); |
84 | |
} else { |
85 | 0 | responsibilityDetails.remove( KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER ); |
86 | |
} |
87 | 0 | List<ResponsibilityAction> responsibilities = getResponsibilityService().getResponsibilityActionsByTemplateName(namespaceCode, responsibilityTemplateName, Attributes.fromMap(qualifier), Attributes.fromMap(responsibilityDetails)); |
88 | 0 | if (LOG.isDebugEnabled()) { |
89 | 0 | LOG.debug("Found " + responsibilities.size() + " responsibilities from ResponsibilityService"); |
90 | |
} |
91 | |
|
92 | 0 | List<ResponsibilitySet> responsibilitySets = partitionResponsibilities(responsibilities); |
93 | 0 | if (LOG.isDebugEnabled()) { |
94 | 0 | LOG.debug("Found " + responsibilitySets.size() + " responsibility sets from ResponsibilityActionInfo list"); |
95 | |
} |
96 | 0 | for (ResponsibilitySet responsibilitySet : responsibilitySets) { |
97 | 0 | String approvePolicy = responsibilitySet.getApprovePolicy(); |
98 | |
|
99 | 0 | if (KEWConstants.APPROVE_POLICY_ALL_APPROVE.equals(approvePolicy)) { |
100 | 0 | for (ResponsibilityAction responsibility : responsibilitySet.getResponsibilities()) { |
101 | 0 | arFactory.addRoleResponsibilityRequest(Collections.singletonList(responsibility), approvePolicy); |
102 | |
} |
103 | |
} else { |
104 | |
|
105 | 0 | arFactory.addRoleResponsibilityRequest(responsibilitySet.getResponsibilities(), approvePolicy); |
106 | |
} |
107 | 0 | } |
108 | 0 | } |
109 | |
} |
110 | 0 | List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>(arFactory.getRequestGraphs()); |
111 | 0 | disableResolveResponsibility(actionRequests); |
112 | 0 | return actionRequests; |
113 | |
} |
114 | |
|
115 | |
protected void logQualifierCheck(String namespaceCode, String responsibilityName, AttributeSet responsibilityDetails, List<AttributeSet> qualifiers ) { |
116 | 0 | StringBuilder sb = new StringBuilder(); |
117 | 0 | sb.append( '\n' ); |
118 | 0 | sb.append( "Get Resp Actions: " ).append( namespaceCode ).append( "/" ).append( responsibilityName ).append( '\n' ); |
119 | 0 | sb.append( " Details:\n" ); |
120 | 0 | if ( responsibilityDetails != null ) { |
121 | 0 | sb.append( responsibilityDetails.formattedDump( 25 ) ); |
122 | |
} else { |
123 | 0 | sb.append( " [null]\n" ); |
124 | |
} |
125 | 0 | sb.append( " Qualifiers:\n" ); |
126 | 0 | for (AttributeSet qualification : qualifiers) { |
127 | 0 | if ( qualification != null ) { |
128 | 0 | sb.append( qualification.formattedDump( 25 ) ); |
129 | |
} else { |
130 | 0 | sb.append( " [null]\n" ); |
131 | |
} |
132 | |
} |
133 | 0 | if (LOG.isTraceEnabled()) { |
134 | 0 | LOG.trace( sb.append(ExceptionUtils.getStackTrace(new Throwable()))); |
135 | |
} else { |
136 | 0 | LOG.debug(sb.toString()); |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected void disableResolveResponsibility(List<ActionRequestValue> actionRequests) { |
150 | 0 | for (ActionRequestValue actionRequest : actionRequests) { |
151 | 0 | actionRequest.setResolveResponsibility(false); |
152 | 0 | disableResolveResponsibility(actionRequest.getChildrenRequests()); |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | |
protected QualifierResolver loadQualifierResolver(RouteContext context) { |
157 | 0 | if (StringUtils.isBlank(qualifierResolverName)) { |
158 | 0 | this.qualifierResolverName = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), QUALIFIER_RESOLVER_ELEMENT); |
159 | |
} |
160 | 0 | if (StringUtils.isBlank(qualifierResolverClassName)) { |
161 | 0 | this.qualifierResolverClassName = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), QUALIFIER_RESOLVER_CLASS_ELEMENT); |
162 | |
} |
163 | 0 | QualifierResolver resolver = null; |
164 | 0 | if (!StringUtils.isBlank(qualifierResolverName)) { |
165 | 0 | RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(qualifierResolverName); |
166 | 0 | if (ruleAttribute == null) { |
167 | 0 | throw new RiceRuntimeException("Failed to locate QualifierResolver for attribute name: " + qualifierResolverName); |
168 | |
} |
169 | 0 | ObjectDefinition definition = getAttributeObjectDefinition(ruleAttribute); |
170 | 0 | resolver = (QualifierResolver)GlobalResourceLoader.getObject(definition); |
171 | 0 | if (resolver instanceof XmlConfiguredAttribute) { |
172 | 0 | ((XmlConfiguredAttribute)resolver).setRuleAttribute(ruleAttribute); |
173 | |
} |
174 | |
} |
175 | 0 | if (resolver == null && !StringUtils.isBlank(qualifierResolverClassName)) { |
176 | 0 | resolver = (QualifierResolver)GlobalResourceLoader.getObject(new ObjectDefinition(qualifierResolverClassName)); |
177 | |
} |
178 | 0 | if (resolver == null) { |
179 | 0 | resolver = new NullQualifierResolver(); |
180 | |
} |
181 | 0 | if (LOG.isDebugEnabled()) { |
182 | 0 | LOG.debug("Resolver class being returned: " + resolver.getClass().getName()); |
183 | |
} |
184 | 0 | return resolver; |
185 | |
} |
186 | |
|
187 | |
protected AttributeSet loadResponsibilityDetails(RouteContext context) { |
188 | 0 | String documentTypeName = context.getDocument().getDocumentType().getName(); |
189 | 0 | String nodeName = context.getNodeInstance().getName(); |
190 | 0 | AttributeSet responsibilityDetails = new AttributeSet(); |
191 | 0 | responsibilityDetails.put(KEWConstants.DOCUMENT_TYPE_NAME_DETAIL, documentTypeName); |
192 | 0 | responsibilityDetails.put(KEWConstants.ROUTE_NODE_NAME_DETAIL, nodeName); |
193 | 0 | return responsibilityDetails; |
194 | |
} |
195 | |
|
196 | |
protected String loadResponsibilityTemplateName(RouteContext context) { |
197 | 0 | if (StringUtils.isBlank(responsibilityTemplateName)) { |
198 | 0 | this.responsibilityTemplateName = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), RESPONSIBILITY_TEMPLATE_NAME_ELEMENT); |
199 | |
} |
200 | 0 | if (StringUtils.isBlank(responsibilityTemplateName)) { |
201 | 0 | this.responsibilityTemplateName = KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME; |
202 | |
} |
203 | 0 | return responsibilityTemplateName; |
204 | |
} |
205 | |
|
206 | |
protected String loadNamespace(RouteContext context) { |
207 | 0 | if (StringUtils.isBlank(namespace)) { |
208 | 0 | this.namespace = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), NAMESPACE_ELEMENT); |
209 | |
} |
210 | 0 | if (StringUtils.isBlank(namespace)) { |
211 | 0 | this.namespace = KEWConstants.KEW_NAMESPACE; |
212 | |
} |
213 | 0 | return namespace; |
214 | |
} |
215 | |
|
216 | |
protected ObjectDefinition getAttributeObjectDefinition(RuleAttribute ruleAttribute) { |
217 | 0 | return new ObjectDefinition(ruleAttribute.getClassName(), ruleAttribute.getApplicationId()); |
218 | |
} |
219 | |
|
220 | |
protected List<ResponsibilitySet> partitionResponsibilities(List<ResponsibilityAction> responsibilities) { |
221 | 0 | List<ResponsibilitySet> responsibilitySets = new ArrayList<ResponsibilitySet>(); |
222 | 0 | for (ResponsibilityAction responsibility : responsibilities) { |
223 | 0 | ResponsibilitySet targetResponsibilitySet = null; |
224 | 0 | for (ResponsibilitySet responsibiliySet : responsibilitySets) { |
225 | 0 | if (responsibiliySet.matches(responsibility)) { |
226 | 0 | targetResponsibilitySet = responsibiliySet; |
227 | |
} |
228 | |
} |
229 | 0 | if (targetResponsibilitySet == null) { |
230 | 0 | targetResponsibilitySet = new ResponsibilitySet(responsibility); |
231 | 0 | responsibilitySets.add(targetResponsibilitySet); |
232 | |
} |
233 | 0 | targetResponsibilitySet.getResponsibilities().add(responsibility); |
234 | 0 | } |
235 | 0 | return responsibilitySets; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public ResponsibleParty resolveResponsibilityId(Long responsibilityId) |
244 | |
throws WorkflowException { |
245 | 0 | return null; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | 0 | private static class ResponsibilitySet { |
251 | |
private String actionRequestCode; |
252 | |
private String approvePolicy; |
253 | |
private Integer priorityNumber; |
254 | |
private String parallelRoutingGroupingCode; |
255 | |
private String roleResponsibilityActionId; |
256 | 0 | private List<ResponsibilityAction> responsibilities = new ArrayList<ResponsibilityAction>(); |
257 | |
|
258 | 0 | public ResponsibilitySet(ResponsibilityAction responsibility) { |
259 | 0 | this.actionRequestCode = responsibility.getActionTypeCode(); |
260 | 0 | this.approvePolicy = responsibility.getActionPolicyCode(); |
261 | 0 | this.priorityNumber = responsibility.getPriorityNumber(); |
262 | 0 | this.parallelRoutingGroupingCode = responsibility.getParallelRoutingGroupingCode(); |
263 | 0 | this.roleResponsibilityActionId = responsibility.getRoleResponsibilityActionId(); |
264 | 0 | } |
265 | |
|
266 | |
public boolean matches(ResponsibilityAction responsibility) { |
267 | 0 | return responsibility.getActionTypeCode().equals(actionRequestCode) && |
268 | |
responsibility.getActionPolicyCode().equals(approvePolicy) && |
269 | |
responsibility.getPriorityNumber().equals( priorityNumber ) && |
270 | |
responsibility.getParallelRoutingGroupingCode().equals( parallelRoutingGroupingCode ) && |
271 | |
responsibility.getRoleResponsibilityActionId().equals( roleResponsibilityActionId ); |
272 | |
} |
273 | |
|
274 | |
public String getActionRequestCode() { |
275 | 0 | return this.actionRequestCode; |
276 | |
} |
277 | |
|
278 | |
public String getApprovePolicy() { |
279 | 0 | return this.approvePolicy; |
280 | |
} |
281 | |
|
282 | |
public Integer getPriorityNumber() { |
283 | 0 | return priorityNumber; |
284 | |
} |
285 | |
|
286 | |
public List<ResponsibilityAction> getResponsibilities() { |
287 | 0 | return this.responsibilities; |
288 | |
} |
289 | |
|
290 | |
public String getParallelRoutingGroupingCode() { |
291 | 0 | return this.parallelRoutingGroupingCode; |
292 | |
} |
293 | |
|
294 | |
public String getRoleResponsibilityActionId() { |
295 | 0 | return this.roleResponsibilityActionId; |
296 | |
} |
297 | |
|
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public void setQualifierResolverName(String qualifierResolverName) { |
306 | 0 | this.qualifierResolverName = qualifierResolverName; |
307 | 0 | } |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public void setQualifierResolverClassName(String qualifierResolverClassName) { |
313 | 0 | this.qualifierResolverClassName = qualifierResolverClassName; |
314 | 0 | } |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void setResponsibilityTemplateName(String responsibilityTemplateName) { |
320 | 0 | this.responsibilityTemplateName = responsibilityTemplateName; |
321 | 0 | } |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
public void setNamespace(String namespace) { |
327 | 0 | this.namespace = namespace; |
328 | 0 | } |
329 | |
|
330 | |
protected ResponsibilityService getResponsibilityService() { |
331 | 0 | if ( responsibilityService == null ) { |
332 | 0 | responsibilityService = KimApiServiceLocator.getResponsibilityService(); |
333 | |
} |
334 | 0 | return responsibilityService; |
335 | |
} |
336 | |
|
337 | |
} |