Coverage Report - org.kuali.rice.kew.actions.CompleteAction
 
Classes in this File Line Coverage Branch Coverage Complexity
CompleteAction
0%
0/55
0%
0/34
4.833
 
 1  
 /**
 2  
  * Copyright 2005-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  
 package org.kuali.rice.kew.actions;
 17  
 
 18  
 import org.apache.log4j.MDC;
 19  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 20  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 21  
 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
 22  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 23  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 24  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 25  
 import org.kuali.rice.kew.api.KewApiConstants;
 26  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 27  
 
 28  
 
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 
 33  
 /**
 34  
  * CompleteAction records and process a complete action
 35  
  *
 36  
  * The routeheader is first checked to make sure the action is valid for the document.
 37  
  * Next the user is checked to make sure he/she has not taken a previous action on this
 38  
  * document at the actions responsibility or below. The action is recorded. Any requests
 39  
  * related to this user are deactivated.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class CompleteAction extends ActionTakenEvent {
 44  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CompleteAction.class);
 45  
 
 46  
     /**
 47  
      * @param rh
 48  
      *            RouteHeader for the document upon which the action is taken.
 49  
      * @param principal
 50  
      *            User taking the action.
 51  
      */
 52  
     public CompleteAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
 53  0
         super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
 54  0
     }
 55  
 
 56  
     /**
 57  
      * @param rh
 58  
      *            RouteHeader for the document upon which the action is taken.
 59  
      * @param principal
 60  
      *            User taking the action.
 61  
      * @param annotation
 62  
      *            User comment on the action taken
 63  
      */
 64  
     public CompleteAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
 65  0
         super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
 66  0
     }
 67  
 
 68  
     /* (non-Javadoc)
 69  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 70  
      */
 71  
     @Override
 72  
     public String validateActionRules() {
 73  0
         return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
 74  
     }
 75  
 
 76  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 77  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 78  0
             return "Document is not in a state to be completed";
 79  
         }
 80  0
         List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
 81  0
         if (!isActionCompatibleRequest(filteredActionRequests)) {
 82  0
             return "No request for the user is compatible " + "with the COMPLETE action";
 83  
         }
 84  0
         return "";
 85  
     }
 86  
 
 87  
     /* (non-Javadoc)
 88  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 89  
      */
 90  
     @Override
 91  
     public boolean isActionCompatibleRequest(List requests) {
 92  
         // we allow pre-approval
 93  0
         if (requests.isEmpty()) {
 94  0
             return true;
 95  
         }
 96  
 
 97  
         // can always cancel saved or initiated document
 98  0
         if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
 99  0
             return true;
 100  
         }
 101  
 
 102  0
         boolean actionCompatible = false;
 103  0
         Iterator ars = requests.iterator();
 104  0
         ActionRequestValue actionRequest = null;
 105  
 
 106  0
         while (ars.hasNext()) {
 107  0
             actionRequest = (ActionRequestValue) ars.next();
 108  0
             String request = actionRequest.getActionRequested();
 109  
 
 110  
             // Complete action matches Complete, Approve, FYI, and ACK requests
 111  0
             if ( (KewApiConstants.ACTION_REQUEST_FYI_REQ.equals(request)) ||
 112  
                     (KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(request)) ||
 113  
                     (KewApiConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||
 114  
                     (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) {
 115  0
                 actionCompatible = true;
 116  0
                 break;
 117  
             }
 118  0
         }
 119  0
         return actionCompatible;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Records the complete action. - Checks to make sure the document status allows the action. - Checks that the user has not taken a previous action. - Deactivates the pending requests for this user - Records the action
 124  
      *
 125  
      * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException
 126  
      * @throws org.kuali.rice.kew.api.exception.ResourceUnavailableException
 127  
      */
 128  
     public void recordAction() throws InvalidActionTakenException {
 129  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 130  0
         updateSearchableAttributesIfPossible();
 131  0
         LOG.debug("Completing document : " + annotation);
 132  
 
 133  0
         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
 134  0
         if (actionRequests == null || actionRequests.isEmpty()) {
 135  0
             DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy();
 136  0
             if (allowUnrequested != null) {
 137  0
                     if (!allowUnrequested.getPolicyValue()) {
 138  0
                             throw new InvalidActionTakenException("No request for the user is compatible " + "with the COMPLETE action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document.");
 139  
                     }
 140  
             }
 141  
         }
 142  0
         LOG.debug("Checking to see if the action is legal");
 143  0
         String errorMessage = validateActionRules(actionRequests);
 144  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 145  0
             throw new InvalidActionTakenException(errorMessage);
 146  
         }
 147  
 
 148  0
         LOG.debug("Record the complete action");
 149  0
         ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(actionRequests));
 150  
 
 151  0
         LOG.debug("Deactivate all pending action requests");
 152  0
         getActionRequestService().deactivateRequests(actionTaken, actionRequests);
 153  0
         notifyActionTaken(actionTaken);
 154  
 
 155  0
         boolean isException = getRouteHeader().isInException();
 156  0
         boolean isSaved = getRouteHeader().isStateSaved();
 157  0
         if (isException || isSaved) {
 158  0
             String oldStatus = getRouteHeader().getDocRouteStatus();
 159  0
             LOG.debug("Moving document back to Enroute from "+KewApiConstants.DOCUMENT_STATUSES.get(oldStatus));
 160  0
             getRouteHeader().markDocumentEnroute();
 161  0
             String newStatus = getRouteHeader().getDocRouteStatus();
 162  0
             notifyStatusChange(newStatus, oldStatus);
 163  0
             KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
 164  
         }
 165  0
     }
 166  
 
 167  
 }