001 /* 002 * Copyright 2005-2007 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.opensource.org/licenses/ecl2.php 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.kuali.rice.kew.role.service.impl; 018 019 import org.apache.commons.collections.CollectionUtils; 020 import org.kuali.rice.core.api.reflect.ObjectDefinition; 021 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 022 import org.kuali.rice.kew.actionrequest.ActionRequestValue; 023 import org.kuali.rice.kew.api.WorkflowRuntimeException; 024 import org.kuali.rice.kew.doctype.bo.DocumentType; 025 import org.kuali.rice.kew.engine.RouteContext; 026 import org.kuali.rice.kew.engine.node.RouteNodeInstance; 027 import org.kuali.rice.kew.exception.WorkflowException; 028 import org.kuali.rice.kew.messaging.MessageServiceNames; 029 import org.kuali.rice.kew.role.service.RoleService; 030 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 031 import org.kuali.rice.kew.rule.FlexRM; 032 import org.kuali.rice.kew.rule.RoleAttribute; 033 import org.kuali.rice.kew.rule.RolePoker; 034 import org.kuali.rice.kew.rule.bo.RuleAttribute; 035 import org.kuali.rice.kew.rule.bo.RuleTemplate; 036 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; 037 import org.kuali.rice.kew.service.KEWServiceLocator; 038 import org.kuali.rice.ksb.api.KsbApiServiceLocator; 039 import org.kuali.rice.ksb.messaging.service.KSBXMLService; 040 041 import javax.xml.namespace.QName; 042 import java.util.*; 043 044 045 /** 046 * 047 * @author Kuali Rice Team (rice.collab@kuali.org) 048 */ 049 public class RoleServiceImpl implements RoleService { 050 051 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RoleServiceImpl.class); 052 053 public void reResolveRole(DocumentType documentType, String roleName) throws WorkflowException { 054 String infoString = "documentType="+(documentType == null ? null : documentType.getName())+", role="+roleName; 055 if (documentType == null || 056 org.apache.commons.lang.StringUtils.isEmpty(roleName)) { 057 throw new IllegalArgumentException("Cannot pass null or empty arguments to reResolveRole: "+infoString); 058 } 059 LOG.debug("Re-resolving role asynchronously for "+infoString); 060 Set documentIds = new HashSet(); 061 findAffectedDocuments(documentType, roleName, null, documentIds); 062 LOG.debug(documentIds.size()+" documents were affected by this re-resolution, requeueing with the RolePokerProcessor"); 063 for (Iterator iterator = documentIds.iterator(); iterator.hasNext();) { 064 String documentId = (String) iterator.next(); 065 QName rolePokerName = new QName(documentType.getApplicationId(), MessageServiceNames.ROLE_POKER); 066 RolePoker rolePoker = (RolePoker) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(rolePokerName); 067 rolePoker.reResolveRole(documentId, roleName); 068 } 069 } 070 071 public void reResolveQualifiedRole(DocumentType documentType, String roleName, String qualifiedRoleNameLabel) throws WorkflowException { 072 String infoString = "documentType="+(documentType == null ? null : documentType.getName())+", role="+roleName+", qualifiedRole="+qualifiedRoleNameLabel; 073 if (documentType == null || 074 org.apache.commons.lang.StringUtils.isEmpty(roleName) || 075 org.apache.commons.lang.StringUtils.isEmpty(qualifiedRoleNameLabel)) { 076 throw new IllegalArgumentException("Cannot pass null or empty arguments to reResolveQualifiedRole: "+infoString); 077 } 078 LOG.debug("Re-resolving qualified role asynchronously for "+infoString); 079 Set documentIds = new HashSet(); 080 findAffectedDocuments(documentType, roleName, qualifiedRoleNameLabel, documentIds); 081 LOG.debug(documentIds.size()+" documents were affected by this re-resolution, requeueing with the RolePokerProcessor"); 082 for (Iterator iterator = documentIds.iterator(); iterator.hasNext();) { 083 String documentId = (String) iterator.next(); 084 QName rolePokerName = new QName(documentType.getApplicationId(), MessageServiceNames.ROLE_POKER); 085 RolePoker rolePoker = (RolePoker) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(rolePokerName); 086 rolePoker.reResolveRole(documentId, roleName, qualifiedRoleNameLabel); 087 } 088 } 089 090 /** 091 * 092 * route level and then filters in the approriate ones. 093 */ 094 public void reResolveQualifiedRole(DocumentRouteHeaderValue routeHeader, String roleName, String qualifiedRoleNameLabel) throws WorkflowException { 095 String infoString = "routeHeader="+(routeHeader == null ? null : routeHeader.getDocumentId())+", role="+roleName+", qualifiedRole="+qualifiedRoleNameLabel; 096 if (routeHeader == null || 097 org.apache.commons.lang.StringUtils.isEmpty(roleName) || 098 org.apache.commons.lang.StringUtils.isEmpty(qualifiedRoleNameLabel)) { 099 throw new IllegalArgumentException("Cannot pass null arguments to reResolveQualifiedRole: "+infoString); 100 } 101 LOG.debug("Re-resolving qualified role synchronously for "+infoString); 102 List nodeInstances = findNodeInstances(routeHeader, roleName); 103 int requestsGenerated = 0; 104 if (!nodeInstances.isEmpty()) { 105 deletePendingRoleRequests(routeHeader.getDocumentId(), roleName, qualifiedRoleNameLabel); 106 for (Iterator nodeIt = nodeInstances.iterator(); nodeIt.hasNext();) { 107 RouteNodeInstance nodeInstance = (RouteNodeInstance)nodeIt.next(); 108 RuleTemplate ruleTemplate = nodeInstance.getRouteNode().getRuleTemplate(); 109 FlexRM flexRM = new FlexRM(); 110 RouteContext context = RouteContext.getCurrentRouteContext(); 111 context.setDocument(routeHeader); 112 context.setNodeInstance(nodeInstance); 113 try { 114 List actionRequests = flexRM.getActionRequests(routeHeader, nodeInstance, ruleTemplate.getName()); 115 for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { 116 ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); 117 if (roleName.equals(actionRequest.getRoleName()) && qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel())) { 118 actionRequest = KEWServiceLocator.getActionRequestService().initializeActionRequestGraph(actionRequest, routeHeader, nodeInstance); 119 KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); 120 requestsGenerated++; 121 } 122 } 123 } catch (Exception e) { 124 RouteContext.clearCurrentRouteContext(); 125 } 126 127 } 128 } 129 LOG.debug("Generated "+requestsGenerated+" action requests after re-resolve: "+infoString); 130 requeueDocument(routeHeader); 131 } 132 133 public void reResolveRole(DocumentRouteHeaderValue routeHeader, String roleName) throws WorkflowException { 134 String infoString = "routeHeader="+(routeHeader == null ? null : routeHeader.getDocumentId())+", role="+roleName; 135 if (routeHeader == null || 136 org.apache.commons.lang.StringUtils.isEmpty(roleName)) { 137 throw new IllegalArgumentException("Cannot pass null arguments to reResolveRole: "+infoString); 138 } 139 LOG.debug("Re-resolving role synchronously for "+infoString); 140 List nodeInstances = findNodeInstances(routeHeader, roleName); 141 int requestsGenerated = 0; 142 if (!nodeInstances.isEmpty()) { 143 deletePendingRoleRequests(routeHeader.getDocumentId(), roleName, null); 144 for (Iterator nodeIt = nodeInstances.iterator(); nodeIt.hasNext();) { 145 RouteNodeInstance nodeInstance = (RouteNodeInstance)nodeIt.next(); 146 RuleTemplate ruleTemplate = nodeInstance.getRouteNode().getRuleTemplate(); 147 FlexRM flexRM = new FlexRM(); 148 RouteContext context = RouteContext.getCurrentRouteContext(); 149 context.setDocument(routeHeader); 150 context.setNodeInstance(nodeInstance); 151 try { 152 List actionRequests = flexRM.getActionRequests(routeHeader, nodeInstance, ruleTemplate.getName()); 153 for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { 154 ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); 155 if (roleName.equals(actionRequest.getRoleName())) { 156 actionRequest = KEWServiceLocator.getActionRequestService().initializeActionRequestGraph(actionRequest, routeHeader, nodeInstance); 157 KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); 158 requestsGenerated++; 159 } 160 } 161 } finally { 162 RouteContext.clearCurrentRouteContext(); 163 } 164 } 165 } 166 LOG.debug("Generated "+requestsGenerated+" action requests after re-resolve: "+infoString); 167 requeueDocument(routeHeader); 168 } 169 170 // search the document type and all its children 171 private void findAffectedDocuments(DocumentType documentType, String roleName, String qualifiedRoleNameLabel, Set documentIds) { 172 List pendingRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(documentType.getDocumentTypeId()); 173 for (Iterator iterator = pendingRequests.iterator(); iterator.hasNext();) { 174 ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); 175 if (roleName.equals(actionRequest.getRoleName()) && 176 (qualifiedRoleNameLabel == null || qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel()))) { 177 documentIds.add(actionRequest.getDocumentId()); 178 } 179 } 180 for (Iterator iterator = documentType.getChildrenDocTypes().iterator(); iterator.hasNext();) { 181 DocumentType childDocumentType = (DocumentType) iterator.next(); 182 findAffectedDocuments(childDocumentType, roleName, qualifiedRoleNameLabel, documentIds); 183 } 184 } 185 186 private void deletePendingRoleRequests(String documentId, String roleName, String qualifiedRoleNameLabel) { 187 List pendingRequests = KEWServiceLocator.getActionRequestService().findPendingByDoc(documentId); 188 pendingRequests = KEWServiceLocator.getActionRequestService().getRootRequests(pendingRequests); 189 List requestsToDelete = new ArrayList(); 190 for (Iterator iterator = pendingRequests.iterator(); iterator.hasNext();) { 191 ActionRequestValue actionRequest = (ActionRequestValue) iterator.next(); 192 if (roleName.equals(actionRequest.getRoleName()) && 193 (qualifiedRoleNameLabel == null || qualifiedRoleNameLabel.equals(actionRequest.getQualifiedRoleNameLabel()))) { 194 requestsToDelete.add(actionRequest); 195 } 196 } 197 LOG.debug("Deleting "+requestsToDelete.size()+" action requests for roleName="+roleName+", qualifiedRoleNameLabel="+qualifiedRoleNameLabel); 198 for (Iterator iterator = requestsToDelete.iterator(); iterator.hasNext();) { 199 KEWServiceLocator.getActionRequestService().deleteActionRequestGraph((ActionRequestValue)iterator.next()); 200 } 201 } 202 203 private List findNodeInstances(DocumentRouteHeaderValue routeHeader, String roleName) throws WorkflowException { 204 List nodeInstances = new ArrayList(); 205 Collection activeNodeInstances = KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(routeHeader.getDocumentId()); 206 if (CollectionUtils.isEmpty(activeNodeInstances)) { 207 throw new WorkflowException("Document does not currently have any active nodes so re-resolving is not legal."); 208 } 209 for (Iterator iterator = activeNodeInstances.iterator(); iterator.hasNext();) { 210 RouteNodeInstance activeNodeInstance = (RouteNodeInstance) iterator.next(); 211 RuleTemplate template = activeNodeInstance.getRouteNode().getRuleTemplate(); 212 if (templateHasRole(template, roleName)) { 213 nodeInstances.add(activeNodeInstance); 214 } 215 } 216 if (nodeInstances.isEmpty()) { 217 throw new WorkflowException("Could not locate given role to re-resolve: " + roleName); 218 } 219 return nodeInstances; 220 } 221 222 private boolean templateHasRole(RuleTemplate template, String roleName) throws WorkflowException { 223 List templateAttributes = template.getRuleTemplateAttributes(); 224 for (Iterator iterator = templateAttributes.iterator(); iterator.hasNext();) { 225 RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iterator.next(); 226 RuleAttribute ruleAttribute = templateAttribute.getRuleAttribute(); 227 Object workflowAttribute = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(ruleAttribute.getClassName()));//SpringServiceLocator.getExtensionService().getWorkflowAttribute(ruleAttribute.getClassName()); 228 if (workflowAttribute instanceof RoleAttribute) { 229 List roleNames = ((RoleAttribute)workflowAttribute).getRoleNames(); 230 for (Iterator roleIt = roleNames.iterator(); roleIt.hasNext();) { 231 org.kuali.rice.kew.rule.Role role = (org.kuali.rice.kew.rule.Role) roleIt.next(); 232 if (role.getLabel().equals(roleName)) { 233 return true; 234 } 235 } 236 } 237 } 238 return false; 239 } 240 241 protected void requeueDocument(DocumentRouteHeaderValue document) { 242 QName documentServiceName = new QName(document.getDocumentType().getApplicationId(), MessageServiceNames.DOCUMENT_ROUTING_SERVICE); 243 KSBXMLService documentRoutingService = (KSBXMLService)MessageServiceNames.getServiceAsynchronously(documentServiceName, document); 244 try { 245 documentRoutingService.invoke(document.getDocumentId()); 246 } catch (Exception e) { 247 throw new WorkflowRuntimeException(e); 248 } 249 } 250 251 }