Coverage Report - org.kuali.rice.kew.actions.AdHocAction
 
Classes in this File Line Coverage Branch Coverage Complexity
AdHocAction
0%
0/68
0%
0/52
5.375
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.actions;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.cxf.common.util.StringUtils;
 23  
 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
 24  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 25  
 import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
 26  
 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
 27  
 import org.kuali.rice.kew.actionrequest.Recipient;
 28  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 29  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 30  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kew.util.KEWConstants;
 33  
 import org.kuali.rice.kew.util.Utilities;
 34  
 import org.kuali.rice.kim.bo.Group;
 35  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 36  
 
 37  
 
 38  
 /**
 39  
  * Responsible for creating adhoc requests that are requested from the client.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class AdHocAction extends ActionTakenEvent {
 44  
     /**
 45  
      * AdHoc actions don't actually result in an action being taken...it's a special case that generates other action requests
 46  
      */
 47  0
     private static final String NO_ACTION_TAKEN_CODE = null;
 48  
 
 49  
         private String actionRequested;
 50  
         private String nodeName;
 51  
         private String responsibilityDesc;
 52  
         private Boolean forceAction;
 53  
         private Recipient recipient;
 54  
         private String annotation;
 55  
         private String requestLabel;
 56  
 
 57  
     public AdHocAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal) {
 58  0
         super(NO_ACTION_TAKEN_CODE, routeHeader, principal);
 59  0
     }
 60  
 
 61  
         public AdHocAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal, String annotation, String actionRequested, String nodeName, Recipient recipient, String responsibilityDesc, Boolean forceAction, String requestLabel) {
 62  0
                 super(NO_ACTION_TAKEN_CODE, routeHeader, principal, annotation);
 63  0
                 this.actionRequested = actionRequested;
 64  0
                 this.nodeName = nodeName;
 65  0
                 this.responsibilityDesc = responsibilityDesc;
 66  0
                 this.forceAction = forceAction;
 67  0
                 this.recipient = recipient;
 68  0
                 this.annotation = annotation;
 69  0
                 this.requestLabel = requestLabel;
 70  0
         }
 71  
 
 72  
         public void recordAction() throws InvalidActionTakenException {
 73  0
                 String errorMessage = validateActionRules();
 74  0
         if (!Utilities.isEmpty(errorMessage)) {
 75  0
             throw new InvalidActionTakenException(errorMessage);
 76  
         }
 77  0
                 List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getRouteHeaderId());
 78  0
         String error = adhocRouteAction(targetNodes, false);
 79  0
         if (!Utilities.isEmpty(error)) {
 80  0
             throw new InvalidActionTakenException(error);
 81  
         }
 82  0
     }
 83  
 
 84  
     /* (non-Javadoc)
 85  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
 86  
      */
 87  
     @Override
 88  
     public String validateActionRules() {
 89  0
         List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getRouteHeaderId());
 90  0
         return validateActionRulesInternal(targetNodes);
 91  
     }
 92  
     
 93  
     @Override
 94  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 95  0
             return validateActionRules();
 96  
     }
 97  
 
 98  
     private String validateActionRulesInternal(List<RouteNodeInstance> targetNodes) {
 99  
             // recipient will be null when this is invoked from ActionRegistry.getValidActions
 100  0
             if (recipient != null) {
 101  0
                     if (recipient instanceof KimPrincipalRecipient) {
 102  0
                             KimPrincipalRecipient principalRecipient = (KimPrincipalRecipient)recipient;
 103  0
                             if (!KEWServiceLocator.getDocumentTypePermissionService().canReceiveAdHocRequest(principalRecipient.getPrincipalId(), getRouteHeader().getDocumentType(), actionRequested)) {
 104  0
                                     return "The principal '" + principalRecipient.getPrincipal().getPrincipalName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
 105  
                             }
 106  0
                     } else if (recipient instanceof KimGroupRecipient) {
 107  0
                             Group group = ((KimGroupRecipient)recipient).getGroup();
 108  0
                             if (!KEWServiceLocator.getDocumentTypePermissionService().canGroupReceiveAdHocRequest("" + group.getGroupId(), getRouteHeader().getDocumentType(), actionRequested)) {
 109  0
                                     return "The group '" + group.getGroupName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
 110  
                             }
 111  0
                     } else {
 112  0
                             return "Invalid Recipient type encountered: " + recipient.getClass();
 113  
                     }
 114  
             }
 115  0
         return adhocRouteAction(targetNodes, true);
 116  
     }
 117  
 
 118  
     private String adhocRouteAction(List<RouteNodeInstance> targetNodes, boolean forValidationOnly) {
 119  
         //if (targetNodes.isEmpty()) {
 120  
         //    return "Could not locate an node instance on the document with the name '" + nodeName + "'";
 121  
         //}
 122  0
         boolean requestCreated = false;
 123  0
         for (RouteNodeInstance routeNode : targetNodes)
 124  
         {
 125  
             // if the node name is null, then adhoc it to the first available node
 126  0
             if (nodeName == null || routeNode.getName().equals(nodeName))
 127  
             {
 128  0
                 String message = createAdHocRequest(routeNode, forValidationOnly);
 129  0
                 if (!StringUtils.isEmpty(message))
 130  
                 {
 131  0
                     return message;
 132  
                 }
 133  0
                 requestCreated = true;
 134  0
                 if (nodeName == null)
 135  
                 {
 136  0
                     break;
 137  
                 }
 138  0
             }
 139  
         }
 140  
         
 141  0
         if (!requestCreated && targetNodes.isEmpty()) {
 142  0
             String message = createAdHocRequest(null, forValidationOnly);
 143  0
             if (!StringUtils.isEmpty(message)) {
 144  0
                 return message;
 145  
             }
 146  0
             requestCreated = true;
 147  
         }
 148  
         
 149  0
         if (!requestCreated) {
 150  0
             return "Didn't create request.  The node name " + nodeName + " given is probably invalid ";
 151  
         }
 152  0
         return "";
 153  
     }
 154  
     
 155  
     private String createAdHocRequest(RouteNodeInstance routeNode, boolean forValidationOnly) {
 156  0
                 ActionRequestValue adhocRequest = new ActionRequestValue();
 157  0
                 if (!forValidationOnly) {
 158  0
                     ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader, routeNode);
 159  0
                     adhocRequest = arFactory.createActionRequest(actionRequested, recipient, responsibilityDesc, forceAction, annotation);
 160  0
                     adhocRequest.setResponsibilityId(KEWConstants.ADHOC_REQUEST_RESPONSIBILITY_ID);
 161  0
                     adhocRequest.setRequestLabel(requestLabel);
 162  0
                 } else {
 163  0
                     adhocRequest.setActionRequested(actionRequested);
 164  
                 }
 165  0
                 if (adhocRequest.isApproveOrCompleteRequest() && ! (routeHeader.isEnroute() || routeHeader.isStateInitiated() ||
 166  
                         routeHeader.isStateSaved())) {
 167  0
             return "Cannot AdHoc a Complete or Approve request when document is in state '" + routeHeader.getDocRouteStatusLabel() + "'.";
 168  
                 }
 169  0
                 if (!forValidationOnly) {
 170  
      
 171  0
                         if (routeHeader.isDisaproved() || routeHeader.isCanceled() || routeHeader.isFinal() || routeHeader.isProcessed()) {
 172  0
                         getActionRequestService().activateRequest(adhocRequest);
 173  
                     } else {
 174  0
                         KEWServiceLocator.getActionRequestService().saveActionRequest(adhocRequest);
 175  
                     }
 176  
                 }
 177  0
         return "";
 178  
     }
 179  
 }