Coverage Report - org.kuali.rice.kew.actionrequest.service.impl.ActionRequestServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionRequestServiceImpl
0%
0/461
0%
0/324
3.486
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.kew.actionrequest.service.impl;
 18  
 
 19  
 import org.apache.commons.lang.ObjectUtils;
 20  
 import org.apache.log4j.Logger;
 21  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 22  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 23  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 24  
 import org.kuali.rice.core.xml.dto.AttributeSet;
 25  
 import org.kuali.rice.kew.actionitem.ActionItem;
 26  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 27  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 28  
 import org.kuali.rice.kew.actionrequest.Recipient;
 29  
 import org.kuali.rice.kew.actionrequest.dao.ActionRequestDAO;
 30  
 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
 31  
 import org.kuali.rice.kew.actionrequest.service.DocumentRequeuerService;
 32  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 33  
 import org.kuali.rice.kew.actiontaken.service.ActionTakenService;
 34  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 35  
 import org.kuali.rice.kew.engine.ActivationContext;
 36  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 37  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 38  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 39  
 import org.kuali.rice.kew.messaging.MessageServiceNames;
 40  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 41  
 import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
 42  
 import org.kuali.rice.kew.routemodule.RouteModule;
 43  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 44  
 import org.kuali.rice.kew.util.FutureRequestDocumentStateManager;
 45  
 import org.kuali.rice.kew.util.KEWConstants;
 46  
 import org.kuali.rice.kew.util.PerformanceLogger;
 47  
 import org.kuali.rice.kew.util.ResponsibleParty;
 48  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 49  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 50  
 import org.kuali.rice.kns.util.KNSConstants;
 51  
 
 52  
 import java.util.ArrayList;
 53  
 import java.util.Collection;
 54  
 import java.util.Collections;
 55  
 import java.util.HashMap;
 56  
 import java.util.HashSet;
 57  
 import java.util.Iterator;
 58  
 import java.util.List;
 59  
 import java.util.Map;
 60  
 import java.util.Set;
 61  
 
 62  
 
 63  
 /**
 64  
  * Default implementation of the {@link ActionRequestService}.
 65  
  *
 66  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 67  
  */
 68  0
 public class ActionRequestServiceImpl implements ActionRequestService {
 69  0
     private static final Logger LOG = Logger.getLogger(ActionRequestServiceImpl.class);
 70  
 
 71  
     private ActionRequestDAO actionRequestDAO;
 72  
 
 73  
     public ActionRequestValue findByActionRequestId(Long actionRequestId) {
 74  0
         return getActionRequestDAO().getActionRequestByActionRequestId(actionRequestId);
 75  
     }
 76  
 
 77  
     public AttributeSet getActionsRequested(DocumentRouteHeaderValue routeHeader, String principalId, boolean completeAndApproveTheSame) {
 78  0
             return getActionsRequested(principalId, routeHeader.getActionRequests(), completeAndApproveTheSame);
 79  
     }
 80  
     
 81  
     /**
 82  
      * Returns a Map of actions that are requested for the given principalId in the given list of action requests.
 83  
      * @param principalId
 84  
      * @param actionRequests
 85  
      * @param completeAndApproveTheSame
 86  
      * @return
 87  
      */
 88  
     protected AttributeSet getActionsRequested(String principalId, List<ActionRequestValue> actionRequests, boolean completeAndApproveTheSame) {
 89  0
             AttributeSet actionsRequested = new AttributeSet();
 90  0
         actionsRequested.put(KEWConstants.ACTION_REQUEST_FYI_REQ, "false");
 91  0
         actionsRequested.put(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
 92  0
         actionsRequested.put(KEWConstants.ACTION_REQUEST_APPROVE_REQ, "false");
 93  0
         actionsRequested.put(KEWConstants.ACTION_REQUEST_COMPLETE_REQ, "false");
 94  0
             String topActionRequested = KEWConstants.ACTION_REQUEST_FYI_REQ;
 95  0
         for (ActionRequestValue actionRequest : actionRequests) {
 96  0
             if (actionRequest.isRecipientRoutedRequest(principalId) && actionRequest.isActive()) {
 97  0
                 int actionRequestComparison = ActionRequestValue.compareActionCode(actionRequest.getActionRequested(), topActionRequested, completeAndApproveTheSame);
 98  0
                 if (actionRequest.isFYIRequest() && actionRequestComparison >= 0) {
 99  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_FYI_REQ, "true");
 100  0
                 } else if (actionRequest.isAcknowledgeRequest() && actionRequestComparison >= 0) {
 101  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "true");
 102  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_FYI_REQ, "false");
 103  0
                     topActionRequested = actionRequest.getActionRequested();
 104  0
                 } else if (actionRequest.isApproveRequest() && actionRequestComparison >= 0) {
 105  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_APPROVE_REQ, "true");
 106  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
 107  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_FYI_REQ, "false");
 108  0
                     topActionRequested = actionRequest.getActionRequested();
 109  0
                 } else if (actionRequest.isCompleteRequst() && actionRequestComparison >= 0) {
 110  0
                         actionsRequested.put(KEWConstants.ACTION_REQUEST_COMPLETE_REQ, "true");
 111  0
                         actionsRequested.put(KEWConstants.ACTION_REQUEST_APPROVE_REQ, "false");
 112  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, "false");
 113  0
                     actionsRequested.put(KEWConstants.ACTION_REQUEST_FYI_REQ, "false");
 114  0
                         if (completeAndApproveTheSame) {
 115  0
                                 actionsRequested.put(KEWConstants.ACTION_REQUEST_APPROVE_REQ, "true");
 116  
                         }
 117  0
                     topActionRequested = actionRequest.getActionRequested();
 118  
                 }
 119  0
             }
 120  
         }
 121  0
         return actionsRequested;
 122  
     }
 123  
 
 124  
     public ActionRequestValue initializeActionRequestGraph(ActionRequestValue actionRequest,
 125  
             DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) {
 126  0
         if (actionRequest.getParentActionRequest() != null) {
 127  0
             LOG.warn("-->A non parent action request from doc " + document.getRouteHeaderId());
 128  0
             actionRequest = KEWServiceLocator.getActionRequestService().getRoot(actionRequest);
 129  
         }
 130  0
         propagatePropertiesToRequestGraph(actionRequest, document, nodeInstance);
 131  0
         return actionRequest;
 132  
     }
 133  
 
 134  
     private void propagatePropertiesToRequestGraph(ActionRequestValue actionRequest, DocumentRouteHeaderValue document,
 135  
             RouteNodeInstance nodeInstance) {
 136  0
         setPropertiesToRequest(actionRequest, document, nodeInstance);
 137  0
         for (ActionRequestValue actionRequestValue : actionRequest.getChildrenRequests())
 138  
         {
 139  0
             propagatePropertiesToRequestGraph(actionRequestValue, document, nodeInstance);
 140  
         }
 141  0
     }
 142  
 
 143  
     private void setPropertiesToRequest(ActionRequestValue actionRequest, DocumentRouteHeaderValue document,
 144  
             RouteNodeInstance nodeInstance) {
 145  0
         actionRequest.setRouteHeaderId(document.getRouteHeaderId());
 146  0
         actionRequest.setDocVersion(document.getDocVersion());
 147  0
         actionRequest.setRouteLevel(document.getDocRouteLevel());
 148  0
         actionRequest.setNodeInstance(nodeInstance);
 149  0
         actionRequest.setStatus(KEWConstants.ACTION_REQUEST_INITIALIZED);
 150  0
     }
 151  
 
 152  
 
 153  
 
 154  
     public void activateRequests(Collection actionRequests) {
 155  0
         activateRequests(actionRequests, new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION));
 156  0
     }
 157  
 
 158  
     public void activateRequests(Collection actionRequests, boolean simulate) {
 159  0
         activateRequests(actionRequests, new ActivationContext(simulate));
 160  0
     }
 161  
 
 162  
     public void activateRequests(Collection actionRequests, ActivationContext activationContext) {
 163  0
         if (actionRequests == null) {
 164  0
             return;
 165  
         }
 166  0
         PerformanceLogger performanceLogger = null;
 167  0
         if ( LOG.isInfoEnabled() ) {
 168  0
                 performanceLogger = new PerformanceLogger();
 169  
         }
 170  0
         activationContext.setGeneratedActionItems(new ArrayList<ActionItem>());
 171  0
         activateRequestsInternal(actionRequests, activationContext);
 172  0
         if (!activationContext.isSimulation()) {
 173  0
             KEWServiceLocator.getNotificationService().notify(activationContext.getGeneratedActionItems());
 174  
         }
 175  0
         if ( LOG.isInfoEnabled() ) {
 176  0
                 performanceLogger.log("Time to " + (activationContext.isSimulation() ? "simulate activation of " : "activate ")
 177  
                                 + actionRequests.size() + " action requests.");
 178  
         }
 179  0
         if ( LOG.isDebugEnabled() ) {
 180  0
                 LOG.debug("Generated " + activationContext.getGeneratedActionItems().size() + " action items.");
 181  
         }
 182  0
     }
 183  
 
 184  
     public void activateRequest(ActionRequestValue actionRequest) {
 185  0
         activateRequests(Collections.singletonList(actionRequest), new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION));
 186  0
     }
 187  
 
 188  
     public void activateRequest(ActionRequestValue actionRequest, boolean simulate) {
 189  0
         activateRequests(Collections.singletonList(actionRequest), new ActivationContext(simulate));
 190  0
     }
 191  
 
 192  
     public void activateRequest(ActionRequestValue actionRequest, ActivationContext activationContext) {
 193  0
         activateRequests(Collections.singletonList(actionRequest), activationContext);
 194  0
     }
 195  
 
 196  
     public List activateRequestNoNotification(ActionRequestValue actionRequest, boolean simulate) {
 197  0
         return activateRequestNoNotification(actionRequest, new ActivationContext(simulate));
 198  
     }
 199  
 
 200  
     public List activateRequestNoNotification(ActionRequestValue actionRequest, ActivationContext activationContext) {
 201  0
         activationContext.setGeneratedActionItems(new ArrayList<ActionItem>());
 202  0
         activateRequestInternal(actionRequest, activationContext);
 203  0
         return activationContext.getGeneratedActionItems();
 204  
     }
 205  
 
 206  
     /**
 207  
      * Internal helper method for activating a Collection of action requests and their children. Maintains an accumulator
 208  
      * for generated action items.
 209  
      * @param actionRequests
 210  
      * @param activationContext
 211  
      */
 212  
     private void activateRequestsInternal(Collection actionRequests, ActivationContext activationContext) {
 213  0
         if (actionRequests == null) {
 214  0
             return;
 215  
         }
 216  0
         List<?> actionRequestList = new ArrayList<Object>(actionRequests);
 217  0
         for (int i = 0; i < actionRequestList.size(); i++) {
 218  0
                 activateRequestInternal((ActionRequestValue) actionRequestList.get(i), activationContext);
 219  
         }
 220  0
     }
 221  
 
 222  
     /**
 223  
      * Internal helper method for activating a single action requests and it's children. Maintains an accumulator for
 224  
      * generated action items.
 225  
      */
 226  
     private void activateRequestInternal(ActionRequestValue actionRequest, ActivationContext activationContext) {
 227  0
         PerformanceLogger performanceLogger = null;
 228  0
         if ( LOG.isInfoEnabled() ) {
 229  0
                 performanceLogger = new PerformanceLogger();
 230  
         }
 231  0
         if (actionRequest == null || actionRequest.isActive() || actionRequest.isDeactivated()) {
 232  0
             return;
 233  
         }
 234  0
         processResponsibilityId(actionRequest);
 235  0
         if (deactivateOnActionAlreadyTaken(actionRequest, activationContext)) {
 236  0
             return;
 237  
         }
 238  0
         if (deactivateOnEmptyGroup(actionRequest, activationContext)) {
 239  0
                 return;
 240  
         }
 241  0
         actionRequest.setStatus(KEWConstants.ACTION_REQUEST_ACTIVATED);
 242  0
         if (!activationContext.isSimulation()) {
 243  0
             saveActionRequest(actionRequest);
 244  0
             activationContext.getGeneratedActionItems().addAll(generateActionItems(actionRequest, activationContext));
 245  
         }
 246  0
         activateRequestsInternal(actionRequest.getChildrenRequests(), activationContext);
 247  0
         activateRequestInternal(actionRequest.getParentActionRequest(), activationContext);
 248  0
         if ( LOG.isInfoEnabled() ) {
 249  0
                 if (activationContext.isSimulation()) {
 250  0
                 performanceLogger.log("Time to simulate activation of request.");
 251  
                 } else {
 252  0
                     performanceLogger.log("Time to activate action request with id " + actionRequest.getActionRequestId());
 253  
                 }
 254  
         }
 255  0
     }
 256  
 
 257  
     /**
 258  
      * Generates ActionItems for the given ActionRequest and returns the List of generated Action Items.
 259  
      *
 260  
      * @param actionRequest
 261  
      * @param activationContext
 262  
      * @return the List of generated ActionItems
 263  
      */
 264  
     private List<ActionItem> generateActionItems(ActionRequestValue actionRequest, ActivationContext activationContext) {
 265  0
             if ( LOG.isDebugEnabled() ) {
 266  0
                     LOG.debug("generating the action items for request " + actionRequest.getActionRequestId());
 267  
             }
 268  0
         List<ActionItem> actionItems = new ArrayList<ActionItem>();
 269  0
         if (!actionRequest.isPrimaryDelegator()) {
 270  0
             if (actionRequest.isGroupRequest()) {
 271  0
                 List<String> principalIds =  KIMServiceLocator.getIdentityManagementService().getGroupMemberPrincipalIds(actionRequest.getGroupId());
 272  0
                 actionItems.addAll(createActionItemsForPrincipals(actionRequest, principalIds));
 273  0
             } else if (actionRequest.isUserRequest()) {
 274  0
                 ActionItem actionItem = getActionListService().createActionItemForActionRequest(actionRequest);
 275  0
                 actionItems.add(actionItem);
 276  
             }
 277  
         }
 278  0
         if (!activationContext.isSimulation()) {
 279  0
             for (ActionItem actionItem: actionItems) {
 280  0
                     if ( LOG.isDebugEnabled() ) {
 281  0
                             LOG.debug("Saving action item: " + actionItems);
 282  
                     }
 283  0
                 getActionListService().saveActionItem(actionItem);
 284  
             }
 285  
         } else {
 286  0
                 actionRequest.getSimulatedActionItems().addAll(actionItems);
 287  
         }
 288  0
         return actionItems;
 289  
     }
 290  
 
 291  
     private List<ActionItem> createActionItemsForPrincipals(ActionRequestValue actionRequest, List<String> principalIds) {
 292  0
         List<ActionItem> actionItems = new ArrayList<ActionItem>();
 293  0
         for (String principalId: principalIds) {
 294  0
             ActionItem actionItem = getActionListService().createActionItemForActionRequest(actionRequest);
 295  0
             actionItem.setPrincipalId(principalId);
 296  0
             actionItem.setRoleName(actionRequest.getQualifiedRoleName());
 297  0
             actionItems.add(actionItem);
 298  0
         }
 299  0
         return actionItems;
 300  
     }
 301  
 
 302  
     private void processResponsibilityId(ActionRequestValue actionRequest) {
 303  0
             if (actionRequest.getResolveResponsibility()) {
 304  0
                 Long responsibilityId = actionRequest.getResponsibilityId();
 305  
                 try {
 306  0
                     RouteModule routeModule = KEWServiceLocator.getRouteModuleService().findRouteModule(actionRequest);
 307  0
                     if (responsibilityId != null && actionRequest.isRouteModuleRequest()) {
 308  0
                             if ( LOG.isDebugEnabled() ) {
 309  0
                                     LOG.debug("Resolving responsibility id for action request id=" + actionRequest.getActionRequestId()
 310  
                                 + " and responsibility id=" + actionRequest.getResponsibilityId());
 311  
                             }
 312  0
                         ResponsibleParty responsibleParty = routeModule.resolveResponsibilityId(actionRequest.getResponsibilityId());
 313  0
                         if (responsibleParty == null) {
 314  0
                             return;
 315  
                         }
 316  0
                         if (responsibleParty.getPrincipalId() != null) {
 317  0
                             KimPrincipal user = KIMServiceLocator.getIdentityManagementService()
 318  
                                     .getPrincipal(responsibleParty.getPrincipalId());
 319  0
                             actionRequest.setPrincipalId(user.getPrincipalId());
 320  0
                         } else if (responsibleParty.getGroupId() != null) {
 321  0
                                 actionRequest.setGroupId(responsibleParty.getGroupId());
 322  0
                         } else if (responsibleParty.getRoleName() != null) {
 323  0
                             actionRequest.setRoleName(responsibleParty.getRoleName());
 324  
                         }
 325  
                     }
 326  0
                 } catch (Exception e) {
 327  0
                     LOG.error("Exception thrown when trying to resolve responsibility id " + responsibilityId, e);
 328  0
                     throw new RuntimeException(e);
 329  0
                 }
 330  
             }
 331  0
     }
 332  
 
 333  
     protected boolean deactivateOnActionAlreadyTaken(ActionRequestValue actionRequestToActivate,
 334  
             ActivationContext activationContext) {
 335  
 
 336  0
         FutureRequestDocumentStateManager futureRequestStateMngr = null;
 337  
 
 338  0
         if (actionRequestToActivate.isGroupRequest()) {
 339  0
             futureRequestStateMngr = new FutureRequestDocumentStateManager(actionRequestToActivate.getRouteHeader(), actionRequestToActivate.getGroup());
 340  0
         } else if (actionRequestToActivate.isUserRequest()) {
 341  0
             futureRequestStateMngr = new FutureRequestDocumentStateManager(actionRequestToActivate.getRouteHeader(), actionRequestToActivate.getPrincipalId());
 342  
         } else {
 343  0
             return false;
 344  
         }
 345  
 
 346  0
         if (futureRequestStateMngr.isReceiveFutureRequests()) {
 347  0
             return false;
 348  
         }
 349  0
         if (!actionRequestToActivate.getForceAction() || futureRequestStateMngr.isDoNotReceiveFutureRequests()) {
 350  0
             ActionTakenValue previousActionTaken = null;
 351  0
             if (!activationContext.isSimulation()) {
 352  0
                 previousActionTaken = getActionTakenService().getPreviousAction(actionRequestToActivate);
 353  
             } else {
 354  0
                 previousActionTaken = getActionTakenService().getPreviousAction(actionRequestToActivate,
 355  
                         activationContext.getSimulatedActionsTaken());
 356  
             }
 357  0
             if (previousActionTaken != null) {
 358  0
                 if ( LOG.isDebugEnabled() ) {
 359  0
                         LOG.debug("found a satisfying action taken so setting this request done.  Action Request Id "
 360  
                             + actionRequestToActivate.getActionRequestId());
 361  
                 }
 362  
                 // set up the delegation for an action taken if this is a delegate request and the delegate has
 363  
                 // already taken action.
 364  0
                 if (!previousActionTaken.isForDelegator() && actionRequestToActivate.getParentActionRequest() != null) {
 365  0
                     previousActionTaken.setDelegator(actionRequestToActivate.getParentActionRequest().getRecipient());
 366  0
                     if (!activationContext.isSimulation()) {
 367  0
                         getActionTakenService().saveActionTaken(previousActionTaken);
 368  
                     }
 369  
                 }
 370  0
                 deactivateRequest(previousActionTaken, actionRequestToActivate, null, activationContext);
 371  0
                 return true;
 372  
             }
 373  
         }
 374  0
         if ( LOG.isDebugEnabled() ) {
 375  0
                 LOG.debug("Forcing action for action request " + actionRequestToActivate.getActionRequestId());
 376  
         }
 377  0
         return false;
 378  
     }
 379  
     
 380  
     /**
 381  
      * Checks if the action request which is being activated has a group with no members.  If this is the case then it will immediately
 382  
      * initiate de-activation on the request since a group with no members will result in no action items being generated so should be
 383  
      * effectively skipped.
 384  
      */
 385  
     protected boolean deactivateOnEmptyGroup(ActionRequestValue actionRequestToActivate, ActivationContext activationContext) {
 386  0
             if (actionRequestToActivate.isGroupRequest()) {
 387  0
                      if (KIMServiceLocator.getGroupService().getMemberPrincipalIds(actionRequestToActivate.getGroup().getGroupId()).isEmpty()) {
 388  0
                              deactivateRequest(null, actionRequestToActivate, null, activationContext);
 389  0
                              return true;
 390  
                  }
 391  
             }
 392  0
             return false;
 393  
     }
 394  
 
 395  
     public void deactivateRequest(ActionTakenValue actionTaken, ActionRequestValue actionRequest) {
 396  0
         deactivateRequest(actionTaken, actionRequest, null, new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION));
 397  0
     }
 398  
 
 399  
     public void deactivateRequest(ActionTakenValue actionTaken, ActionRequestValue actionRequest, boolean simulate) {
 400  0
         deactivateRequest(actionTaken, actionRequest, null, new ActivationContext(simulate));
 401  0
     }
 402  
 
 403  
     public void deactivateRequest(ActionTakenValue actionTaken, ActionRequestValue actionRequest,
 404  
             ActivationContext activationContext) {
 405  0
         deactivateRequest(actionTaken, actionRequest, null, activationContext);
 406  0
     }
 407  
 
 408  
     public void deactivateRequests(ActionTakenValue actionTaken, List actionRequests) {
 409  0
         deactivateRequests(actionTaken, actionRequests, null,
 410  
                 new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION));
 411  0
     }
 412  
 
 413  
     public void deactivateRequests(ActionTakenValue actionTaken, List actionRequests, boolean simulate) {
 414  0
         deactivateRequests(actionTaken, actionRequests, null, new ActivationContext(simulate));
 415  0
     }
 416  
 
 417  
     public void deactivateRequests(ActionTakenValue actionTaken, List actionRequests, ActivationContext activationContext) {
 418  0
         deactivateRequests(actionTaken, actionRequests, null, activationContext);
 419  0
     }
 420  
 
 421  
     private void deactivateRequests(ActionTakenValue actionTaken, Collection actionRequests,
 422  
             ActionRequestValue deactivationRequester, ActivationContext activationContext) {
 423  0
         if (actionRequests == null) {
 424  0
             return;
 425  
         }
 426  0
         for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
 427  0
             ActionRequestValue actionRequest = (ActionRequestValue) iterator.next();
 428  0
             deactivateRequest(actionTaken, actionRequest, deactivationRequester, activationContext);
 429  0
         }
 430  0
     }
 431  
 
 432  
     private void deactivateRequest(ActionTakenValue actionTaken, ActionRequestValue actionRequest,
 433  
             ActionRequestValue deactivationRequester, ActivationContext activationContext) {
 434  0
         if (actionRequest == null || actionRequest.isDeactivated()
 435  
                 || haltForAllApprove(actionRequest, deactivationRequester)) {
 436  0
             return;
 437  
         }
 438  0
         actionRequest.setStatus(KEWConstants.ACTION_REQUEST_DONE_STATE);
 439  0
         actionRequest.setActionTaken(actionTaken);
 440  0
         if (actionTaken != null) {
 441  0
             actionTaken.getActionRequests().add(actionRequest);
 442  
         }
 443  0
         if (!activationContext.isSimulation()) {
 444  0
             getActionRequestDAO().saveActionRequest(actionRequest);
 445  0
             deleteActionItems(actionRequest);
 446  
         }
 447  0
         deactivateRequests(actionTaken, actionRequest.getChildrenRequests(), actionRequest, activationContext);
 448  0
         deactivateRequest(actionTaken, actionRequest.getParentActionRequest(), actionRequest, activationContext);
 449  0
     }
 450  
 
 451  
     /**
 452  
      * Returns true if we are dealing with an 'All Approve' request, the requester of the deactivation is a child of the
 453  
      * 'All Approve' request, and all of the children have not been deactivated. If all of the children are already
 454  
      * deactivated or a non-child request initiated deactivation, then this method returns false. false otherwise.
 455  
      * @param actionRequest
 456  
      * @param deactivationRequester
 457  
      * @return
 458  
      */
 459  
     private boolean haltForAllApprove(ActionRequestValue actionRequest, ActionRequestValue deactivationRequester) {
 460  0
         if (KEWConstants.APPROVE_POLICY_ALL_APPROVE.equals(actionRequest.getApprovePolicy())
 461  
                 && actionRequest.hasChild(deactivationRequester)) {
 462  0
             boolean allDeactivated = true;
 463  0
             for (ActionRequestValue childRequest : actionRequest.getChildrenRequests())
 464  
             {
 465  0
                 if (!(allDeactivated = childRequest.isDeactivated()))
 466  
                 {
 467  0
                     return true;
 468  
                 }
 469  
             }
 470  
         }
 471  0
         return false;
 472  
     }
 473  
 
 474  
     public List<ActionRequestValue> getRootRequests(Collection<ActionRequestValue> actionRequests) {
 475  0
             Set<ActionRequestValue> unsavedRequests = new HashSet<ActionRequestValue>();
 476  0
             Map<Long, ActionRequestValue> requestMap = new HashMap<Long, ActionRequestValue>();
 477  0
         for (ActionRequestValue actionRequest1 : actionRequests)
 478  
         {
 479  0
             ActionRequestValue actionRequest = (ActionRequestValue) actionRequest1;
 480  0
             ActionRequestValue rootRequest = getRoot(actionRequest);
 481  0
             if (rootRequest.getActionRequestId() != null)
 482  
             {
 483  0
                 requestMap.put(rootRequest.getActionRequestId(), rootRequest);
 484  
             } else
 485  
             {
 486  0
                 unsavedRequests.add(rootRequest);
 487  
             }
 488  0
         }
 489  0
             List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>();
 490  0
             requests.addAll(requestMap.values());
 491  0
             requests.addAll(unsavedRequests);
 492  0
             return requests;
 493  
         }
 494  
 
 495  
     public ActionRequestValue getRoot(ActionRequestValue actionRequest) {
 496  0
         if (actionRequest == null) {
 497  0
             return null;
 498  
         }
 499  0
         if (actionRequest.getParentActionRequest() != null) {
 500  0
             return getRoot(actionRequest.getParentActionRequest());
 501  
         }
 502  0
         return actionRequest;
 503  
     }
 504  
     
 505  
     /**
 506  
      * Returns all pending requests for a given routing entity
 507  
      * @param routeHeaderId the id of the document header being routed
 508  
      * @return a List of all pending ActionRequestValues for the document
 509  
      */
 510  
     public List<ActionRequestValue> findAllPendingRequests(Long routeHeaderId) {
 511  0
             ActionRequestDAO arDAO = getActionRequestDAO();
 512  0
         List<ActionRequestValue> pendingArs = arDAO.findByStatusAndDocId(KEWConstants.ACTION_REQUEST_ACTIVATED, routeHeaderId);
 513  0
         return pendingArs;
 514  
     }
 515  
 
 516  
     public List findAllValidRequests(String principalId, Long routeHeaderId, String requestCode) {
 517  0
         ActionRequestDAO arDAO = getActionRequestDAO();
 518  0
         Collection pendingArs = arDAO.findByStatusAndDocId(KEWConstants.ACTION_REQUEST_ACTIVATED, routeHeaderId);
 519  0
         return findAllValidRequests(principalId, pendingArs, requestCode);
 520  
     }
 521  
 
 522  
     public List findAllValidRequests(String principalId, Collection actionRequests, String requestCode) {
 523  0
         List matchedArs = new ArrayList();
 524  0
         List<String> arGroups = KIMServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId);
 525  0
         return filterActionRequestsByCode((List<ActionRequestValue>)actionRequests, principalId, arGroups, requestCode);
 526  
     }
 527  
     
 528  
     /**
 529  
          * Filters action requests based on if they occur after the given requestCode, and if they relate to 
 530  
          * the given principal
 531  
          * @param actionRequests the List of ActionRequestValues to filter
 532  
          * @param principalId the id of the principal to find active requests for
 533  
          * @param principalGroupIds List of group ids that the principal belongs to
 534  
          * @param requestCode the request code for all ActionRequestValues to be after
 535  
          * @return the filtered List of ActionRequestValues
 536  
          */
 537  
         public List<ActionRequestValue> filterActionRequestsByCode(List<ActionRequestValue> actionRequests, String principalId, List<String> principalGroupIds, String requestCode) {
 538  0
                 List<ActionRequestValue> filteredActionRequests = new ArrayList<ActionRequestValue>();
 539  
                 
 540  0
                 List<String> arGroups = null;
 541  0
         for (ActionRequestValue ar : actionRequests) {
 542  0
             if (ActionRequestValue.compareActionCode(ar.getActionRequested(), requestCode, true) > 0) {
 543  0
                 continue;
 544  
             }
 545  0
             if (ar.isUserRequest() && principalId.equals(ar.getPrincipalId())) {
 546  0
                     filteredActionRequests.add(ar);
 547  0
             } else if (ar.isGroupRequest() && principalGroupIds != null && !principalGroupIds.isEmpty()) {
 548  0
                     for (String groupId : principalGroupIds) {
 549  0
                             if (groupId.equals(ar.getGroupId())) {
 550  0
                                     filteredActionRequests.add(ar);
 551  
                             }
 552  
                     }
 553  
             }
 554  
         }
 555  
                 
 556  0
                 return filteredActionRequests;
 557  
         }
 558  
 
 559  
     public void updateActionRequestsForResponsibilityChange(Set<Long> responsibilityIds) {
 560  0
             PerformanceLogger performanceLogger = null;
 561  0
             if ( LOG.isInfoEnabled() ) {
 562  0
                     performanceLogger = new PerformanceLogger();
 563  
             }
 564  0
         Collection documentsAffected = getRouteHeaderService().findPendingByResponsibilityIds(responsibilityIds);
 565  0
         String cacheWaitValue = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.RULE_DETAIL_TYPE, KEWConstants.RULE_CACHE_REQUEUE_DELAY);
 566  0
         Long cacheWait = KEWConstants.DEFAULT_CACHE_REQUEUE_WAIT_TIME;
 567  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(cacheWaitValue)) {
 568  
             try {
 569  0
                 cacheWait = Long.valueOf(cacheWaitValue);
 570  0
             } catch (NumberFormatException e) {
 571  0
                 LOG.warn("Cache wait time is not a valid number: " + cacheWaitValue);
 572  0
             }
 573  
         }
 574  0
         if ( LOG.isInfoEnabled() ) {
 575  0
                 LOG.info("Scheduling requeue of " + documentsAffected.size() + " documents, affected by " + responsibilityIds.size()
 576  
                     + " responsibility changes.  Installing a processing wait time of " + cacheWait
 577  
                     + " milliseconds to avoid stale rule cache.");
 578  
         }
 579  0
         for (Object aDocumentsAffected : documentsAffected)
 580  
         {
 581  0
             Long routeHeaderId = (Long) aDocumentsAffected;
 582  
 
 583  0
              String serviceNamespace = null;
 584  0
              DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByDocumentId(routeHeaderId);
 585  
                     
 586  0
              if (documentType != null) {
 587  0
                 serviceNamespace = documentType.getServiceNamespace();
 588  
              }
 589  
 
 590  0
             if (serviceNamespace == null)
 591  
             {
 592  0
                 serviceNamespace = ConfigContext.getCurrentContextConfig().getServiceNamespace();
 593  
             }
 594  0
             DocumentRequeuerService documentRequeuer = MessageServiceNames.getDocumentRequeuerService(serviceNamespace,
 595  
                     routeHeaderId, cacheWait);
 596  0
             documentRequeuer.requeueDocument(routeHeaderId);
 597  0
         }
 598  0
         if ( LOG.isInfoEnabled() ) {
 599  0
                 performanceLogger.log("Time to updateActionRequestsForResponsibilityChange");
 600  
         }
 601  0
     }
 602  
 
 603  
     /**
 604  
      * Deletes an action request and all of its action items following the graph down through the action request's
 605  
      * children. This method should be invoked on a top-level action request.
 606  
      */
 607  
     public void deleteActionRequestGraph(ActionRequestValue actionRequest) {
 608  0
         deleteActionItems(actionRequest);
 609  0
         if (actionRequest.getActionTakenId() != null) {
 610  0
             ActionTakenValue actionTaken = getActionTakenService().findByActionTakenId(actionRequest.getActionTakenId());
 611  
 
 612  0
             if(actionTaken != null){//iu patch
 613  0
             getActionTakenService().delete(actionTaken);
 614  
             }//iu patch
 615  
            
 616  
         }
 617  0
         getActionRequestDAO().delete(actionRequest.getActionRequestId());
 618  0
         for (ActionRequestValue child: actionRequest.getChildrenRequests()) {
 619  0
             deleteActionRequestGraph(child);
 620  
         }
 621  0
     }
 622  
 
 623  
     /**
 624  
      * Deletes the action items for the action request
 625  
      * @param actionRequest the action request whose action items to delete
 626  
      */
 627  
     private void deleteActionItems(ActionRequestValue actionRequest) {
 628  0
             List<ActionItem> actionItems = actionRequest.getActionItems();
 629  0
             if ( LOG.isDebugEnabled() ) {
 630  0
                     LOG.debug("deleting " + actionItems.size() + " action items for action request: " + actionRequest);
 631  
             }
 632  0
         for (ActionItem actionItem: actionItems) {
 633  0
                 if ( LOG.isDebugEnabled() ) {
 634  0
                         LOG.debug("deleting action item: " + actionItem);
 635  
                 }
 636  0
             getActionListService().deleteActionItem(actionItem);
 637  
         }
 638  0
     }
 639  
 
 640  
 
 641  
     public List findByRouteHeaderIdIgnoreCurrentInd(Long routeHeaderId) {
 642  0
         return getActionRequestDAO().findByRouteHeaderIdIgnoreCurrentInd(routeHeaderId);
 643  
     }
 644  
 
 645  
     public List findAllActionRequestsByRouteHeaderId(Long routeHeaderId) {
 646  0
         return getActionRequestDAO().findAllByDocId(routeHeaderId);
 647  
     }
 648  
 
 649  
     public List findAllRootActionRequestsByRouteHeaderId(Long routeHeaderId) {
 650  0
         return getActionRequestDAO().findAllRootByDocId(routeHeaderId);
 651  
     }
 652  
 
 653  
     public List findPendingByActionRequestedAndDocId(String actionRequestedCd, Long routeHeaderId) {
 654  0
         return getActionRequestDAO().findPendingByActionRequestedAndDocId(actionRequestedCd, routeHeaderId);
 655  
     }
 656  
 
 657  
     /**
 658  
      * @see org.kuali.rice.kew.actionrequest.service.ActionRequestService#getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(java.lang.String, java.lang.Long)
 659  
      */
 660  
     public List<String> getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(String actionRequestedCd, Long routeHeaderId) {
 661  0
             List<String> principalIds = new ArrayList<String>();
 662  0
             List<ActionRequestValue> actionRequests = findPendingByActionRequestedAndDocId(actionRequestedCd, routeHeaderId);
 663  0
                 for(ActionRequestValue actionRequest: actionRequests){
 664  0
                         if(actionRequest.isUserRequest()){
 665  0
                                 principalIds.add(actionRequest.getPrincipalId());
 666  0
                         } else if(actionRequest.isGroupRequest()){
 667  0
                                 principalIds.addAll(
 668  
                                                 KIMServiceLocator.getIdentityManagementService().getGroupMemberPrincipalIds(actionRequest.getGroupId()));
 669  
                         }
 670  
                 }
 671  0
             return principalIds;
 672  
     }
 673  
 
 674  
     public List findPendingByDocIdAtOrBelowRouteLevel(Long routeHeaderId, Integer routeLevel) {
 675  0
         return getActionRequestDAO().findPendingByDocIdAtOrBelowRouteLevel(routeHeaderId, routeLevel);
 676  
     }
 677  
 
 678  
     public List findPendingRootRequestsByDocId(Long routeHeaderId) {
 679  0
         return getRootRequests(findPendingByDoc(routeHeaderId));
 680  
     }
 681  
 
 682  
     public List findPendingRootRequestsByDocIdAtRouteNode(Long routeHeaderId, Long nodeInstanceId) {
 683  0
         return getActionRequestDAO().findPendingRootRequestsByDocIdAtRouteNode(routeHeaderId, nodeInstanceId);
 684  
     }
 685  
 
 686  
     public List findRootRequestsByDocIdAtRouteNode(Long documentId, Long nodeInstanceId) {
 687  0
         return getActionRequestDAO().findRootRequestsByDocIdAtRouteNode(documentId, nodeInstanceId);
 688  
     }
 689  
 
 690  
     public List findPendingRootRequestsByDocIdAtOrBelowRouteLevel(Long routeHeaderId, Integer routeLevel) {
 691  0
         return getActionRequestDAO().findPendingRootRequestsByDocIdAtOrBelowRouteLevel(routeHeaderId, routeLevel);
 692  
     }
 693  
 
 694  
     public List findPendingRootRequestsByDocIdAtRouteLevel(Long routeHeaderId, Integer routeLevel) {
 695  0
         return getActionRequestDAO().findPendingRootRequestsByDocIdAtRouteLevel(routeHeaderId, routeLevel);
 696  
     }
 697  
 
 698  
     public List findPendingRootRequestsByDocumentType(Long documentTypeId) {
 699  0
         return getActionRequestDAO().findPendingRootRequestsByDocumentType(documentTypeId);
 700  
     }
 701  
 
 702  
     public void saveActionRequest(ActionRequestValue actionRequest) {
 703  0
         if (actionRequest.isGroupRequest()) {
 704  0
                 if (!actionRequest.getGroup().isActive()) {
 705  0
                         throw new RiceRuntimeException("Attempted to save an action request with an inactive group.");
 706  
                 }
 707  
         }
 708  0
         getActionRequestDAO().saveActionRequest(actionRequest);
 709  0
     }
 710  
 
 711  
     public List findPendingByDoc(Long routeHeaderId) {
 712  0
         return getActionRequestDAO().findAllPendingByDocId(routeHeaderId);
 713  
     }
 714  
 
 715  
     public List findPendingByDocRequestCdRouteLevel(Long routeHeaderId, String requestCode, Integer routeLevel) {
 716  0
         List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>();
 717  0
         for (Object object : getActionRequestDAO().findAllPendingByDocId(routeHeaderId))
 718  
         {
 719  0
             ActionRequestValue actionRequest = (ActionRequestValue) object;
 720  0
             if (ActionRequestValue.compareActionCode(actionRequest.getActionRequested(), requestCode, true) > 0)
 721  
             {
 722  0
                 continue;
 723  
             }
 724  0
             if (actionRequest.getRouteLevel().intValue() == routeLevel.intValue())
 725  
             {
 726  0
                 requests.add(actionRequest);
 727  
             }
 728  0
         }
 729  0
         return requests;
 730  
     }
 731  
 
 732  
     public List findPendingByDocRequestCdNodeName(Long routeHeaderId, String requestCode, String nodeName) {
 733  0
         List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>();
 734  0
         for (Object object : getActionRequestDAO().findAllPendingByDocId(routeHeaderId))
 735  
         {
 736  0
             ActionRequestValue actionRequest = (ActionRequestValue) object;
 737  0
             if (ActionRequestValue.compareActionCode(actionRequest.getActionRequested(), requestCode, true) > 0)
 738  
             {
 739  0
                 continue;
 740  
             }
 741  0
             if (actionRequest.getNodeInstance() != null && actionRequest.getNodeInstance().getName().equals(nodeName))
 742  
             {
 743  0
                 requests.add(actionRequest);
 744  
             }
 745  0
         }
 746  0
         return requests;
 747  
     }
 748  
 
 749  
     public List findActivatedByGroup(String groupId) {
 750  0
         return getActionRequestDAO().findActivatedByGroup(groupId);
 751  
     }
 752  
 
 753  
     private ActionListService getActionListService() {
 754  0
         return (ActionListService) KEWServiceLocator.getActionListService();
 755  
     }
 756  
 
 757  
     private ActionTakenService getActionTakenService() {
 758  0
         return (ActionTakenService) KEWServiceLocator.getActionTakenService();
 759  
     }
 760  
 
 761  
     public ActionRequestDAO getActionRequestDAO() {
 762  0
         return actionRequestDAO;
 763  
     }
 764  
 
 765  
     public void setActionRequestDAO(ActionRequestDAO actionRequestDAO) {
 766  0
         this.actionRequestDAO = actionRequestDAO;
 767  0
     }
 768  
 
 769  
     private RouteHeaderService getRouteHeaderService() {
 770  0
         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
 771  
     }
 772  
 
 773  
     public List<ActionRequestValue> findByStatusAndDocId(String statusCd, Long routeHeaderId) {
 774  0
         return getActionRequestDAO().findByStatusAndDocId(statusCd, routeHeaderId);
 775  
     }
 776  
 
 777  
     public void alterActionRequested(List actionRequests, String actionRequestCd) {
 778  0
         for (Object actionRequest1 : actionRequests)
 779  
         {
 780  0
             ActionRequestValue actionRequest = (ActionRequestValue) actionRequest1;
 781  
 
 782  0
             actionRequest.setActionRequested(actionRequestCd);
 783  0
             for (ActionItem item : actionRequest.getActionItems())
 784  
             {
 785  0
                 item.setActionRequestCd(actionRequestCd);
 786  
             }
 787  
 
 788  0
             saveActionRequest(actionRequest);
 789  0
         }
 790  0
     }
 791  
 
 792  
     // TODO this still won't work in certain cases when checking from the root
 793  
     public boolean isDuplicateRequest(ActionRequestValue actionRequest) {
 794  0
         List<ActionRequestValue> requests = findAllRootActionRequestsByRouteHeaderId(actionRequest.getRouteHeaderId());
 795  0
         for (ActionRequestValue existingRequest : requests) {
 796  0
             if (existingRequest.getStatus().equals(KEWConstants.ACTION_REQUEST_DONE_STATE)
 797  
                     && existingRequest.getRouteLevel().equals(actionRequest.getRouteLevel())
 798  
                     && ObjectUtils.equals(existingRequest.getPrincipalId(), actionRequest.getPrincipalId())
 799  
                     && ObjectUtils.equals(existingRequest.getGroupId(), actionRequest.getGroupId())
 800  
                     && ObjectUtils.equals(existingRequest.getRoleName(), actionRequest.getRoleName())
 801  
                     && ObjectUtils.equals(existingRequest.getQualifiedRoleName(), actionRequest.getQualifiedRoleName())
 802  
                     && existingRequest.getActionRequested().equals(actionRequest.getActionRequested())) {
 803  0
                 return true;
 804  
             }
 805  
         }
 806  0
         return false;
 807  
     }
 808  
 
 809  
     public Recipient findDelegator(List actionRequests) {
 810  0
         Recipient delegator = null;
 811  0
         String requestCode = KEWConstants.ACTION_REQUEST_FYI_REQ;
 812  0
         for (Object actionRequest1 : actionRequests)
 813  
         {
 814  0
             ActionRequestValue actionRequest = (ActionRequestValue) actionRequest1;
 815  0
             ActionRequestValue delegatorRequest = findDelegatorRequest(actionRequest);
 816  0
             if (delegatorRequest != null)
 817  
             {
 818  0
                 if (ActionRequestValue.compareActionCode(delegatorRequest.getActionRequested(), requestCode, true) >= 0)
 819  
                 {
 820  0
                     delegator = delegatorRequest.getRecipient();
 821  0
                     requestCode = delegatorRequest.getActionRequested();
 822  
                 }
 823  
             }
 824  0
         }
 825  0
         return delegator;
 826  
     }
 827  
 
 828  
     public Recipient findDelegator(ActionRequestValue actionRequest) {
 829  0
         ActionRequestValue delegatorRequest = findDelegatorRequest(actionRequest);
 830  0
         Recipient delegator = null;
 831  0
         if (delegatorRequest != null) {
 832  0
             delegator = delegatorRequest.getRecipient();
 833  
         }
 834  0
         return delegator;
 835  
     }
 836  
 
 837  
     public ActionRequestValue findDelegatorRequest(ActionRequestValue actionRequest) {
 838  0
         ActionRequestValue parentRequest = actionRequest.getParentActionRequest();
 839  0
         if (parentRequest != null && !(parentRequest.isUserRequest() || parentRequest.isGroupRequest())) {
 840  0
             parentRequest = findDelegatorRequest(parentRequest);
 841  
         }
 842  0
         return parentRequest;
 843  
     }
 844  
 
 845  
     public void deleteByRouteHeaderId(Long routeHeaderId) {
 846  0
         actionRequestDAO.deleteByRouteHeaderId(routeHeaderId);
 847  0
     }
 848  
 
 849  
     public void deleteByActionRequestId(Long actionRequestId) {
 850  0
         actionRequestDAO.delete(actionRequestId);
 851  0
     }
 852  
 
 853  
     public void validateActionRequest(ActionRequestValue actionRequest) {
 854  0
         LOG.debug("Enter validateActionRequest(..)");
 855  0
         List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
 856  
 
 857  0
         String actionRequestCd = actionRequest.getActionRequested();
 858  0
         if (actionRequestCd == null || actionRequestCd.trim().equals("")) {
 859  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest cd null.", "actionrequest.actionrequestcd.empty",
 860  
                     actionRequest.getActionRequestId().toString()));
 861  0
         } else if (!KEWConstants.ACTION_REQUEST_CD.containsKey(actionRequestCd)) {
 862  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest cd invalid.", "actionrequest.actionrequestcd.invalid",
 863  
                     actionRequest.getActionRequestId().toString()));
 864  
         }
 865  
 
 866  0
         Long routeHeaderId = actionRequest.getRouteHeaderId();
 867  0
         if (routeHeaderId == null || routeHeaderId == 0) {
 868  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest Document id empty.", "actionrequest.routeheaderid.empty",
 869  
                     actionRequest.getActionRequestId().toString()));
 870  0
         } else if (getRouteHeaderService().getRouteHeader(routeHeaderId) == null) {
 871  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest Document id invalid.",
 872  
                     "actionrequest.routeheaderid.invalid", actionRequest.getActionRequestId().toString()));
 873  
         }
 874  
 
 875  0
         String actionRequestStatus = actionRequest.getStatus();
 876  0
         if (actionRequestStatus == null || actionRequestStatus.trim().equals("")) {
 877  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest status null.", "actionrequest.actionrequeststatus.empty",
 878  
                     actionRequest.getActionRequestId().toString()));
 879  0
         } else if (!KEWConstants.ACTION_REQUEST_STATUS.containsKey(actionRequestStatus)) {
 880  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest status invalid.",
 881  
                     "actionrequest.actionrequeststatus.invalid", actionRequest.getActionRequestId().toString()));
 882  
         }
 883  
 
 884  0
         if (actionRequest.getResponsibilityId() == null) {
 885  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest responsibility id null.",
 886  
                     "actionrequest.responsibilityid.empty", actionRequest.getActionRequestId().toString()));
 887  
         }
 888  
 
 889  0
         Integer priority = actionRequest.getPriority();
 890  0
         if (priority == null) {
 891  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest priority null.", "actionrequest.priority.empty",
 892  
                     actionRequest.getActionRequestId().toString()));
 893  
         }
 894  
 
 895  
         // if(actionRequest.getRouteMethodName() == null || actionRequest.getRouteMethodName().trim().equals("")){
 896  
         // errors.add(new WorkflowServiceErrorImpl("ActionRequest route method name null.",
 897  
         // "actionrequest.routemethodname.empty", actionRequest.getActionRequestId().toString()));
 898  
         // }
 899  
 
 900  0
         Integer routeLevel = actionRequest.getRouteLevel();
 901  0
         if (routeLevel == null) {
 902  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest route level null.", "actionrequest.routelevel.empty",
 903  
                     actionRequest.getActionRequestId().toString()));
 904  0
         } else if (routeLevel < -1) {
 905  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest route level invalid.",
 906  
                     "actionrequest.routelevel.invalid", actionRequest.getActionRequestId().toString()));
 907  
         }
 908  
 
 909  0
         Integer version = actionRequest.getDocVersion();
 910  0
         if (version == null) {
 911  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest doc version null.", "actionrequest.docversion.empty",
 912  
                     actionRequest.getActionRequestId().toString()));
 913  
         }
 914  
 
 915  0
         if (actionRequest.getCreateDate() == null) {
 916  0
             errors.add(new WorkflowServiceErrorImpl("ActionRequest create date null.", "actionrequest.createdate.empty",
 917  
                     actionRequest.getActionRequestId().toString()));
 918  
         }
 919  
 
 920  0
         String recipientType = actionRequest.getRecipientTypeCd();
 921  0
         if (recipientType != null && !recipientType.trim().equals("")) {
 922  0
             if (recipientType.equals(KEWConstants.WORKGROUP)) {
 923  0
                 String workgroupId = actionRequest.getGroupId();
 924  0
                 if (workgroupId == null) {
 925  0
                     errors.add(new WorkflowServiceErrorImpl("ActionRequest workgroup null.",
 926  
                             "actionrequest.workgroup.empty", actionRequest.getActionRequestId().toString()));
 927  0
                 } else if (KIMServiceLocator.getIdentityManagementService().getGroup(workgroupId) == null) {
 928  0
                     errors.add(new WorkflowServiceErrorImpl("ActionRequest workgroup invalid.",
 929  
                             "actionrequest.workgroup.invalid", actionRequest.getActionRequestId().toString()));
 930  
                 }
 931  
 
 932  
             }
 933  0
             if (recipientType.equals(KEWConstants.PERSON)) {
 934  0
                 String principalId = actionRequest.getPrincipalId();
 935  0
                 if (principalId == null || principalId.trim().equals("")) {
 936  0
                     errors.add(new WorkflowServiceErrorImpl("ActionRequest person id null.", "actionrequest.persosn.empty",
 937  
                             actionRequest.getActionRequestId().toString()));
 938  
                 } else {
 939  0
                         KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipal(principalId);
 940  0
                         if (principal == null) {
 941  0
                                 errors.add(new WorkflowServiceErrorImpl("ActionRequest person id invalid.",
 942  
                                                 "actionrequest.personid.invalid", actionRequest.getActionRequestId().toString()));
 943  
                         }
 944  
                 }
 945  
 
 946  0
                 if (recipientType.equals(KEWConstants.ROLE)
 947  
                         && (actionRequest.getRoleName() == null || actionRequest.getRoleName().trim().equals(""))) {
 948  0
                     errors.add(new WorkflowServiceErrorImpl("ActionRequest role name null.", "actionrequest.rolename.null",
 949  
                             actionRequest.getActionRequestId().toString()));
 950  
                 }
 951  
             }
 952  0
             LOG.debug("Exit validateActionRequest(..) ");
 953  0
             if (!errors.isEmpty()) {
 954  0
                 throw new WorkflowServiceErrorException("ActionRequest Validation Error", errors);
 955  
             }
 956  
         }
 957  0
     }
 958  
 
 959  
     public List getDelegateRequests(ActionRequestValue actionRequest) {
 960  0
         List<ActionRequestValue> delegateRequests = new ArrayList<ActionRequestValue>();
 961  0
         List requests = getTopLevelRequests(actionRequest);
 962  0
         for (Object request : requests)
 963  
         {
 964  0
             ActionRequestValue parentActionRequest = (ActionRequestValue) request;
 965  0
             delegateRequests.addAll(parentActionRequest.getChildrenRequests());
 966  0
         }
 967  0
         return delegateRequests;
 968  
     }
 969  
 
 970  
     public List getTopLevelRequests(ActionRequestValue actionRequest) {
 971  0
         List<ActionRequestValue> topLevelRequests = new ArrayList<ActionRequestValue>();
 972  0
         if (actionRequest.isRoleRequest()) {
 973  0
             topLevelRequests.addAll(actionRequest.getChildrenRequests());
 974  
         } else {
 975  0
             topLevelRequests.add(actionRequest);
 976  
         }
 977  0
         return topLevelRequests;
 978  
     }
 979  
 
 980  
     public boolean isValidActionRequestCode(String actionRequestCode) {
 981  0
         return actionRequestCode != null && KEWConstants.ACTION_REQUEST_CODES.containsKey(actionRequestCode);
 982  
     }
 983  
 
 984  
     public boolean doesPrincipalHaveRequest(String principalId, Long documentId) {
 985  0
         if (getActionRequestDAO().doesDocumentHaveUserRequest(principalId, documentId)) {
 986  0
             return true;
 987  
         }
 988  
         // TODO since we only store the workgroup id for workgroup requests, if the user is in a workgroup that has a request
 989  
         // than we need get all the requests with workgroup ids and see if our user is in that group
 990  0
         List<String> groupIds = getActionRequestDAO().getRequestGroupIds(documentId);
 991  0
         for (String groupId : groupIds) {
 992  0
             if (KIMServiceLocator.getIdentityManagementService().isMemberOfGroup(principalId, groupId)) {
 993  0
                 return true;
 994  
             }
 995  
         }
 996  0
         return false;
 997  
     }
 998  
 
 999  
 }