1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.role.service.impl; |
18 | |
|
19 | |
import org.apache.commons.collections.CollectionUtils; |
20 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
21 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
22 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
23 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
24 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
25 | |
import org.kuali.rice.kew.engine.RouteContext; |
26 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
27 | |
import org.kuali.rice.kew.exception.WorkflowException; |
28 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
29 | |
import org.kuali.rice.kew.role.service.RoleService; |
30 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
31 | |
import org.kuali.rice.kew.rule.FlexRM; |
32 | |
import org.kuali.rice.kew.rule.RoleAttribute; |
33 | |
import org.kuali.rice.kew.rule.RolePoker; |
34 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
35 | |
import org.kuali.rice.kew.rule.bo.RuleTemplate; |
36 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; |
37 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
38 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
39 | |
import org.kuali.rice.ksb.messaging.service.KSBXMLService; |
40 | |
|
41 | |
import javax.xml.namespace.QName; |
42 | |
import java.util.*; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class RoleServiceImpl implements RoleService { |
50 | |
|
51 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RoleServiceImpl.class); |
52 | |
|
53 | |
public void reResolveRole(DocumentType documentType, String roleName) throws WorkflowException { |
54 | 0 | String infoString = "documentType="+(documentType == null ? null : documentType.getName())+", role="+roleName; |
55 | 0 | if (documentType == null || |
56 | |
org.apache.commons.lang.StringUtils.isEmpty(roleName)) { |
57 | 0 | throw new IllegalArgumentException("Cannot pass null or empty arguments to reResolveRole: "+infoString); |
58 | |
} |
59 | 0 | LOG.debug("Re-resolving role asynchronously for "+infoString); |
60 | 0 | Set documentIds = new HashSet(); |
61 | 0 | findAffectedDocuments(documentType, roleName, null, documentIds); |
62 | 0 | LOG.debug(documentIds.size()+" documents were affected by this re-resolution, requeueing with the RolePokerProcessor"); |
63 | 0 | for (Iterator iterator = documentIds.iterator(); iterator.hasNext();) { |
64 | 0 | String documentId = (String) iterator.next(); |
65 | 0 | QName rolePokerName = new QName(documentType.getApplicationId(), MessageServiceNames.ROLE_POKER); |
66 | 0 | RolePoker rolePoker = (RolePoker) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(rolePokerName); |
67 | 0 | rolePoker.reResolveRole(documentId, roleName); |
68 | 0 | } |
69 | 0 | } |
70 | |
|
71 | |
public void reResolveQualifiedRole(DocumentType documentType, String roleName, String qualifiedRoleNameLabel) throws WorkflowException { |
72 | 0 | String infoString = "documentType="+(documentType == null ? null : documentType.getName())+", role="+roleName+", qualifiedRole="+qualifiedRoleNameLabel; |
73 | 0 | if (documentType == null || |
74 | |
org.apache.commons.lang.StringUtils.isEmpty(roleName) || |
75 | |
org.apache.commons.lang.StringUtils.isEmpty(qualifiedRoleNameLabel)) { |
76 | 0 | throw new IllegalArgumentException("Cannot pass null or empty arguments to reResolveQualifiedRole: "+infoString); |
77 | |
} |
78 | 0 | LOG.debug("Re-resolving qualified role asynchronously for "+infoString); |
79 | 0 | Set documentIds = new HashSet(); |
80 | 0 | findAffectedDocuments(documentType, roleName, qualifiedRoleNameLabel, documentIds); |
81 | 0 | LOG.debug(documentIds.size()+" documents were affected by this re-resolution, requeueing with the RolePokerProcessor"); |
82 | 0 | for (Iterator iterator = documentIds.iterator(); iterator.hasNext();) { |
83 | 0 | String documentId = (String) iterator.next(); |
84 | 0 | QName rolePokerName = new QName(documentType.getApplicationId(), MessageServiceNames.ROLE_POKER); |
85 | 0 | RolePoker rolePoker = (RolePoker) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(rolePokerName); |
86 | 0 | rolePoker.reResolveRole(documentId, roleName, qualifiedRoleNameLabel); |
87 | 0 | } |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void reResolveQualifiedRole(DocumentRouteHeaderValue routeHeader, String roleName, String qualifiedRoleNameLabel) throws WorkflowException { |
95 | 0 | String infoString = "routeHeader="+(routeHeader == null ? null : routeHeader.getDocumentId())+", role="+roleName+", qualifiedRole="+qualifiedRoleNameLabel; |
96 | 0 | if (routeHeader == null || |
97 | |
org.apache.commons.lang.StringUtils.isEmpty(roleName) || |
98 | |
org.apache.commons.lang.StringUtils.isEmpty(qualifiedRoleNameLabel)) { |
99 | 0 | throw new IllegalArgumentException("Cannot pass null arguments to reResolveQualifiedRole: "+infoString); |
100 | |
} |
101 | 0 | LOG.debug("Re-resolving qualified role synchronously for "+infoString); |
102 | 0 | List nodeInstances = findNodeInstances(routeHeader, roleName); |
103 | 0 | int requestsGenerated = 0; |
104 | 0 | if (!nodeInstances.isEmpty()) { |
105 | 0 | deletePendingRoleRequests(routeHeader.getDocumentId(), roleName, qualifiedRoleNameLabel); |
106 | 0 | for (Iterator nodeIt = nodeInstances.iterator(); nodeIt.hasNext();) { |
107 | 0 | RouteNodeInstance nodeInstance = (RouteNodeInstance)nodeIt.next(); |
108 | 0 | RuleTemplate ruleTemplate = nodeInstance.getRouteNode().getRuleTemplate(); |
109 | 0 | FlexRM flexRM = new FlexRM(); |
110 | 0 | RouteContext context = RouteContext.getCurrentRouteContext(); |
111 | 0 | context.setDocument(routeHeader); |
112 | 0 | context.setNodeInstance(nodeInstance); |
113 | |
try { |
114 | 0 | List actionRequests = flexRM.getActionRequests(routeHeader, nodeInstance, ruleTemplate.getName()); |
115 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
116 | 0 | ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); |
117 | 0 | if (roleName.equals(actionRequest.getRoleName()) && qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel())) { |
118 | 0 | actionRequest = KEWServiceLocator.getActionRequestService().initializeActionRequestGraph(actionRequest, routeHeader, nodeInstance); |
119 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); |
120 | 0 | requestsGenerated++; |
121 | |
} |
122 | 0 | } |
123 | 0 | } catch (Exception e) { |
124 | 0 | RouteContext.clearCurrentRouteContext(); |
125 | 0 | } |
126 | |
|
127 | 0 | } |
128 | |
} |
129 | 0 | LOG.debug("Generated "+requestsGenerated+" action requests after re-resolve: "+infoString); |
130 | 0 | requeueDocument(routeHeader); |
131 | 0 | } |
132 | |
|
133 | |
public void reResolveRole(DocumentRouteHeaderValue routeHeader, String roleName) throws WorkflowException { |
134 | 0 | String infoString = "routeHeader="+(routeHeader == null ? null : routeHeader.getDocumentId())+", role="+roleName; |
135 | 0 | if (routeHeader == null || |
136 | |
org.apache.commons.lang.StringUtils.isEmpty(roleName)) { |
137 | 0 | throw new IllegalArgumentException("Cannot pass null arguments to reResolveRole: "+infoString); |
138 | |
} |
139 | 0 | LOG.debug("Re-resolving role synchronously for "+infoString); |
140 | 0 | List nodeInstances = findNodeInstances(routeHeader, roleName); |
141 | 0 | int requestsGenerated = 0; |
142 | 0 | if (!nodeInstances.isEmpty()) { |
143 | 0 | deletePendingRoleRequests(routeHeader.getDocumentId(), roleName, null); |
144 | 0 | for (Iterator nodeIt = nodeInstances.iterator(); nodeIt.hasNext();) { |
145 | 0 | RouteNodeInstance nodeInstance = (RouteNodeInstance)nodeIt.next(); |
146 | 0 | RuleTemplate ruleTemplate = nodeInstance.getRouteNode().getRuleTemplate(); |
147 | 0 | FlexRM flexRM = new FlexRM(); |
148 | 0 | RouteContext context = RouteContext.getCurrentRouteContext(); |
149 | 0 | context.setDocument(routeHeader); |
150 | 0 | context.setNodeInstance(nodeInstance); |
151 | |
try { |
152 | 0 | List actionRequests = flexRM.getActionRequests(routeHeader, nodeInstance, ruleTemplate.getName()); |
153 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
154 | 0 | ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); |
155 | 0 | if (roleName.equals(actionRequest.getRoleName())) { |
156 | 0 | actionRequest = KEWServiceLocator.getActionRequestService().initializeActionRequestGraph(actionRequest, routeHeader, nodeInstance); |
157 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); |
158 | 0 | requestsGenerated++; |
159 | |
} |
160 | 0 | } |
161 | |
} finally { |
162 | 0 | RouteContext.clearCurrentRouteContext(); |
163 | 0 | } |
164 | 0 | } |
165 | |
} |
166 | 0 | LOG.debug("Generated "+requestsGenerated+" action requests after re-resolve: "+infoString); |
167 | 0 | requeueDocument(routeHeader); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
private void findAffectedDocuments(DocumentType documentType, String roleName, String qualifiedRoleNameLabel, Set documentIds) { |
172 | 0 | List pendingRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(documentType.getDocumentTypeId()); |
173 | 0 | for (Iterator iterator = pendingRequests.iterator(); iterator.hasNext();) { |
174 | 0 | ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); |
175 | 0 | if (roleName.equals(actionRequest.getRoleName()) && |
176 | |
(qualifiedRoleNameLabel == null || qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel()))) { |
177 | 0 | documentIds.add(actionRequest.getDocumentId()); |
178 | |
} |
179 | 0 | } |
180 | 0 | for (Iterator iterator = documentType.getChildrenDocTypes().iterator(); iterator.hasNext();) { |
181 | 0 | DocumentType childDocumentType = (DocumentType) iterator.next(); |
182 | 0 | findAffectedDocuments(childDocumentType, roleName, qualifiedRoleNameLabel, documentIds); |
183 | 0 | } |
184 | 0 | } |
185 | |
|
186 | |
private void deletePendingRoleRequests(String documentId, String roleName, String qualifiedRoleNameLabel) { |
187 | 0 | List pendingRequests = KEWServiceLocator.getActionRequestService().findPendingByDoc(documentId); |
188 | 0 | pendingRequests = KEWServiceLocator.getActionRequestService().getRootRequests(pendingRequests); |
189 | 0 | List requestsToDelete = new ArrayList(); |
190 | 0 | for (Iterator iterator = pendingRequests.iterator(); iterator.hasNext();) { |
191 | 0 | ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); |
192 | 0 | if (roleName.equals(actionRequest.getRoleName()) && |
193 | |
(qualifiedRoleNameLabel == null || qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel()))) { |
194 | 0 | requestsToDelete.add(actionRequest); |
195 | |
} |
196 | 0 | } |
197 | 0 | LOG.debug("Deleting "+requestsToDelete.size()+" action requests for roleName="+roleName+", qualifiedRoleNameLabel="+qualifiedRoleNameLabel); |
198 | 0 | for (Iterator iterator = requestsToDelete.iterator(); iterator.hasNext();) { |
199 | 0 | KEWServiceLocator.getActionRequestService().deleteActionRequestGraph((ActionRequestValue)iterator.next()); |
200 | |
} |
201 | 0 | } |
202 | |
|
203 | |
private List findNodeInstances(DocumentRouteHeaderValue routeHeader, String roleName) throws WorkflowException { |
204 | 0 | List nodeInstances = new ArrayList(); |
205 | 0 | Collection activeNodeInstances = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(routeHeader.getDocumentId()); |
206 | 0 | if (CollectionUtils.isEmpty(activeNodeInstances)) { |
207 | 0 | throw new WorkflowException("Document does not currently have any active nodes so re-resolving is not legal."); |
208 | |
} |
209 | 0 | for (Iterator iterator = activeNodeInstances.iterator(); iterator.hasNext();) { |
210 | 0 | RouteNodeInstance activeNodeInstance = (RouteNodeInstance) iterator.next(); |
211 | 0 | RuleTemplate template = activeNodeInstance.getRouteNode().getRuleTemplate(); |
212 | 0 | if (templateHasRole(template, roleName)) { |
213 | 0 | nodeInstances.add(activeNodeInstance); |
214 | |
} |
215 | 0 | } |
216 | 0 | if (nodeInstances.isEmpty()) { |
217 | 0 | throw new WorkflowException("Could not locate given role to re-resolve: " + roleName); |
218 | |
} |
219 | 0 | return nodeInstances; |
220 | |
} |
221 | |
|
222 | |
private boolean templateHasRole(RuleTemplate template, String roleName) throws WorkflowException { |
223 | 0 | List templateAttributes = template.getRuleTemplateAttributes(); |
224 | 0 | for (Iterator iterator = templateAttributes.iterator(); iterator.hasNext();) { |
225 | 0 | RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iterator.next(); |
226 | 0 | RuleAttribute ruleAttribute = templateAttribute.getRuleAttribute(); |
227 | 0 | Object workflowAttribute = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(ruleAttribute.getClassName())); |
228 | 0 | if (workflowAttribute instanceof RoleAttribute) { |
229 | 0 | List roleNames = ((RoleAttribute)workflowAttribute).getRoleNames(); |
230 | 0 | for (Iterator roleIt = roleNames.iterator(); roleIt.hasNext();) { |
231 | 0 | org.kuali.rice.kew.rule.Role role = (org.kuali.rice.kew.rule.Role) roleIt.next(); |
232 | 0 | if (role.getLabel().equals(roleName)) { |
233 | 0 | return true; |
234 | |
} |
235 | 0 | } |
236 | |
} |
237 | 0 | } |
238 | 0 | return false; |
239 | |
} |
240 | |
|
241 | |
protected void requeueDocument(DocumentRouteHeaderValue document) { |
242 | 0 | QName documentServiceName = new QName(document.getDocumentType().getApplicationId(), MessageServiceNames.DOCUMENT_ROUTING_SERVICE); |
243 | 0 | KSBXMLService documentRoutingService = (KSBXMLService)MessageServiceNames.getServiceAsynchronously(documentServiceName, document); |
244 | |
try { |
245 | 0 | documentRoutingService.invoke(document.getDocumentId()); |
246 | 0 | } catch (Exception e) { |
247 | 0 | throw new WorkflowRuntimeException(e); |
248 | 0 | } |
249 | 0 | } |
250 | |
|
251 | |
} |