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