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 org.apache.cxf.common.util.StringUtils;
 20  
 import org.kuali.rice.kew.actionrequest.*;
 21  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 22  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 23  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 24  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 25  
 import org.kuali.rice.kew.util.KEWConstants;
 26  
 import org.kuali.rice.kim.api.entity.principal.PrincipalContract;
 27  
 import org.kuali.rice.kim.api.group.Group;
 28  
 
 29  
 
 30  
 import java.util.List;
 31  
 
 32  
 
 33  
 /**
 34  
  * Responsible for creating adhoc requests that are requested from the client.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class AdHocAction extends ActionTakenEvent {
 39  
     /**
 40  
      * AdHoc actions don't actually result in an action being taken...it's a special case that generates other action requests
 41  
      */
 42  0
     private static final String NO_ACTION_TAKEN_CODE = null;
 43  
 
 44  
         private String actionRequested;
 45  
         private String nodeName;
 46  
         private String responsibilityDesc;
 47  
         private Boolean forceAction;
 48  
         private Recipient recipient;
 49  
         private String annotation;
 50  
         private String requestLabel;
 51  
 
 52  
     public AdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
 53  0
         super(NO_ACTION_TAKEN_CODE, routeHeader, principal);
 54  0
     }
 55  
 
 56  
         public AdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String actionRequested, String nodeName, Recipient recipient, String responsibilityDesc, Boolean forceAction, String requestLabel) {
 57  0
                 super(NO_ACTION_TAKEN_CODE, routeHeader, principal, annotation);
 58  0
                 this.actionRequested = actionRequested;
 59  0
                 this.nodeName = nodeName;
 60  0
                 this.responsibilityDesc = responsibilityDesc;
 61  0
                 this.forceAction = forceAction;
 62  0
                 this.recipient = recipient;
 63  0
                 this.annotation = annotation;
 64  0
                 this.requestLabel = requestLabel;
 65  0
         }
 66  
 
 67  
         public void recordAction() throws InvalidActionTakenException {
 68  0
                 String errorMessage = validateActionRules();
 69  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 70  0
             throw new InvalidActionTakenException(errorMessage);
 71  
         }
 72  0
                 List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId());
 73  0
         String error = adhocRouteAction(targetNodes, false);
 74  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(error)) {
 75  0
             throw new InvalidActionTakenException(error);
 76  
         }
 77  0
     }
 78  
 
 79  
     /* (non-Javadoc)
 80  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
 81  
      */
 82  
     @Override
 83  
     public String validateActionRules() {
 84  0
         List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId());
 85  0
         return validateActionRulesInternal(targetNodes);
 86  
     }
 87  
     
 88  
     @Override
 89  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 90  0
             return validateActionRules();
 91  
     }
 92  
 
 93  
     private String validateActionRulesInternal(List<RouteNodeInstance> targetNodes) {
 94  
             // recipient will be null when this is invoked from ActionRegistry.getValidActions
 95  0
             if (recipient != null) {
 96  0
                     if (recipient instanceof KimPrincipalRecipient) {
 97  0
                             KimPrincipalRecipient principalRecipient = (KimPrincipalRecipient)recipient;
 98  0
                             if (!KEWServiceLocator.getDocumentTypePermissionService().canReceiveAdHocRequest(principalRecipient.getPrincipalId(), getRouteHeader().getDocumentType(), actionRequested)) {
 99  0
                                     return "The principal '" + principalRecipient.getPrincipal().getPrincipalName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
 100  
                             }
 101  0
                     } else if (recipient instanceof KimGroupRecipient) {
 102  0
                             Group group = ((KimGroupRecipient)recipient).getGroup();
 103  0
                             if (!KEWServiceLocator.getDocumentTypePermissionService().canGroupReceiveAdHocRequest("" + group.getId(), getRouteHeader().getDocumentType(), actionRequested)) {
 104  0
                                     return "The group '" + group.getName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
 105  
                             }
 106  0
                     } else {
 107  0
                             return "Invalid Recipient type encountered: " + recipient.getClass();
 108  
                     }
 109  
             }
 110  0
         return adhocRouteAction(targetNodes, true);
 111  
     }
 112  
 
 113  
     private String adhocRouteAction(List<RouteNodeInstance> targetNodes, boolean forValidationOnly) {
 114  
         //if (targetNodes.isEmpty()) {
 115  
         //    return "Could not locate an node instance on the document with the name '" + nodeName + "'";
 116  
         //}
 117  0
         boolean requestCreated = false;
 118  0
         for (RouteNodeInstance routeNode : targetNodes)
 119  
         {
 120  
             // if the node name is null, then adhoc it to the first available node
 121  0
             if (nodeName == null || routeNode.getName().equals(nodeName))
 122  
             {
 123  0
                 String message = createAdHocRequest(routeNode, forValidationOnly);
 124  0
                 if (!StringUtils.isEmpty(message))
 125  
                 {
 126  0
                     return message;
 127  
                 }
 128  0
                 requestCreated = true;
 129  0
                 if (nodeName == null)
 130  
                 {
 131  0
                     break;
 132  
                 }
 133  0
             }
 134  
         }
 135  
         
 136  0
         if (!requestCreated && targetNodes.isEmpty()) {
 137  0
             String message = createAdHocRequest(null, forValidationOnly);
 138  0
             if (!StringUtils.isEmpty(message)) {
 139  0
                 return message;
 140  
             }
 141  0
             requestCreated = true;
 142  
         }
 143  
         
 144  0
         if (!requestCreated) {
 145  0
             return "Didn't create request.  The node name " + nodeName + " given is probably invalid ";
 146  
         }
 147  0
         return "";
 148  
     }
 149  
     
 150  
     private String createAdHocRequest(RouteNodeInstance routeNode, boolean forValidationOnly) {
 151  0
                 ActionRequestValue adhocRequest = new ActionRequestValue();
 152  0
                 if (!forValidationOnly) {
 153  0
                     ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader, routeNode);
 154  0
                     adhocRequest = arFactory.createActionRequest(actionRequested, recipient, responsibilityDesc, forceAction, annotation);
 155  0
                     adhocRequest.setResponsibilityId(KEWConstants.ADHOC_REQUEST_RESPONSIBILITY_ID);
 156  0
                     adhocRequest.setRequestLabel(requestLabel);
 157  0
                 } else {
 158  0
                     adhocRequest.setActionRequested(actionRequested);
 159  
                 }
 160  0
                 if (adhocRequest.isApproveOrCompleteRequest() && ! (routeHeader.isEnroute() || routeHeader.isStateInitiated() ||
 161  
                         routeHeader.isStateSaved())) {
 162  0
             return "Cannot AdHoc a Complete or Approve request when document is in state '" + routeHeader.getDocRouteStatusLabel() + "'.";
 163  
                 }
 164  0
                 if (!forValidationOnly) {
 165  
      
 166  0
                         if (routeHeader.isDisaproved() || routeHeader.isCanceled() || routeHeader.isFinal() || routeHeader.isProcessed()) {
 167  0
                         getActionRequestService().activateRequest(adhocRequest);
 168  
                     } else {
 169  0
                         KEWServiceLocator.getActionRequestService().saveActionRequest(adhocRequest);
 170  
                     }
 171  
                 }
 172  0
         return "";
 173  
     }
 174  
 }