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