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