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