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