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