Coverage Report - org.kuali.rice.kew.actions.AcknowledgeAction
 
Classes in this File Line Coverage Branch Coverage Complexity
AcknowledgeAction
0%
0/47
0%
0/26
4.167
 
 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.log4j.MDC;
 20  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 21  
 import org.kuali.rice.kew.actionrequest.Recipient;
 22  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 23  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 24  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 25  
 import org.kuali.rice.kew.exception.ResourceUnavailableException;
 26  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 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  
  * <p>
 36  
  * AcknowledegeAction records the users acknowledgement of a document
 37  
  * </p>
 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  
  * </p>
 43  
  *
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  
 public class AcknowledgeAction extends ActionTakenEvent {
 47  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AcknowledgeAction.class);
 48  
 
 49  
     /**
 50  
      * @param rh
 51  
      *            RouteHeader for the document upon which the action is taken.
 52  
      * @param principal
 53  
      *            User taking the action.
 54  
      */
 55  
     public AcknowledgeAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
 56  0
         super(KEWConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, rh, principal);
 57  0
     }
 58  
 
 59  
     /**
 60  
      * @param rh
 61  
      *            RouteHeader for the document upon which the action is taken.
 62  
      * @param principal
 63  
      *            User taking the action.
 64  
      * @param annotation
 65  
      *            User comment on the action taken
 66  
      */
 67  
     public AcknowledgeAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
 68  0
         super(KEWConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, rh, principal, annotation);
 69  0
     }
 70  
     
 71  
     /**
 72  
      * Method to check if the Action is currently valid on the given document
 73  
      * @return  returns an error message to give system better identifier for problem
 74  
      */
 75  
     public String validateActionRules() {
 76  0
         return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
 77  
     }
 78  
 
 79  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 80  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 81  0
             return "Document is not in a state to be acknowledged";
 82  
         }
 83  0
         List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
 84  0
         if (!isActionCompatibleRequest(filteredActionRequests)) {
 85  0
             return "No request for the user is compatible " + "with the ACKNOWLEDGE action";
 86  
         }
 87  0
         return "";
 88  
     }
 89  
 
 90  
     /* (non-Javadoc)
 91  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List, java.lang.String)
 92  
      */
 93  
     @Override
 94  
     public boolean isActionCompatibleRequest(List requests) {
 95  
 
 96  
         // we allow pre-approval
 97  0
         if (requests.isEmpty()) {
 98  0
             return true;
 99  
         }
 100  
 
 101  
         // can always cancel saved or initiated document
 102  0
         if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
 103  0
             return true;
 104  
         }
 105  
 
 106  0
         boolean actionCompatible = false;
 107  0
         Iterator ars = requests.iterator();
 108  0
         ActionRequestValue actionRequest = null;
 109  
 
 110  0
         while (ars.hasNext()) {
 111  0
             actionRequest = (ActionRequestValue) ars.next();
 112  0
             String request = actionRequest.getActionRequested();
 113  
 
 114  
             // Acknowledge Taken Code matches Fyi and Ack
 115  0
             if ( (KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(request)) || (KEWConstants.ACTION_REQUEST_FYI_REQ.equals(request)) ) {
 116  0
                 actionCompatible = true;
 117  0
                 break;
 118  
             }
 119  0
         }
 120  
 
 121  0
         return actionCompatible;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Records the Acknowldege 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 InvalidActionTakenException {
 131  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 132  0
         updateSearchableAttributesIfPossible();
 133  
 
 134  0
         LOG.debug("Acknowledging document : " + annotation);
 135  
 
 136  0
         LOG.debug("Checking to see if the action is legal");
 137  0
         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), routeHeader.getDocumentId(), KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
 138  0
         if (actionRequests == null || actionRequests.isEmpty()) {
 139  0
             DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy();
 140  0
             if (allowUnrequested != null) {
 141  0
                     if (!allowUnrequested.getPolicyValue()) {
 142  0
                             throw new InvalidActionTakenException("No request for the user is compatible " + "with the ACKNOWLEDGE action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document.");
 143  
                     }
 144  
             }
 145  
         }
 146  
 
 147  0
         String errorMessage = validateActionRules(actionRequests);
 148  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 149  0
             throw new InvalidActionTakenException(errorMessage);
 150  
         }
 151  
 
 152  0
         LOG.debug("Record the acknowledge action");
 153  0
         Recipient delegator = findDelegatorForActionRequests(actionRequests);
 154  0
         ActionTakenValue actionTaken = saveActionTaken(delegator);
 155  0
         LOG.debug("Deactivate all pending action requests");
 156  0
         getActionRequestService().deactivateRequests(actionTaken, actionRequests);
 157  0
         notifyActionTaken(actionTaken);
 158  0
     }
 159  
 }