Coverage Report - org.kuali.rice.kew.actions.RevokeAdHocAction
 
Classes in this File Line Coverage Branch Coverage Complexity
RevokeAdHocAction
0%
0/47
0%
0/36
4.571
 
 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 java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.collections.CollectionUtils;
 22  
 import org.apache.log4j.MDC;
 23  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 24  
 import org.kuali.rice.kew.actionrequest.Recipient;
 25  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 26  
 import org.kuali.rice.kew.api.action.AdHocRevoke;
 27  
 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
 28  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 29  
 import org.kuali.rice.kew.api.KewApiConstants;
 30  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 31  
 
 32  
 
 33  
 /**
 34  
  * The RevokeAdHocApprove revokes the specified AdHoc requests.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class RevokeAdHocAction extends ActionTakenEvent {
 39  
 
 40  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RevokeAdHocAction.class);
 41  
 
 42  
     private String actionRequestId;
 43  
     private AdHocRevoke revoke;
 44  
 
 45  
     public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
 46  0
         super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal);
 47  0
     }
 48  
 
 49  
     public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String actionRequestId, String annotation) {
 50  0
         super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation);
 51  0
         this.actionRequestId = actionRequestId;
 52  0
     }
 53  
     
 54  
     public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, AdHocRevoke revoke, String annotation) {
 55  0
         super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation);
 56  0
         this.revoke = revoke;
 57  0
     }
 58  
 
 59  
     /* (non-Javadoc)
 60  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 61  
      */
 62  
     @Override
 63  
     public String validateActionRules() {
 64  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 65  0
             return "Revoke adhoc request is not valid on this document";
 66  
         }
 67  0
         return "";
 68  
     }
 69  
     
 70  
     @Override
 71  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 72  0
             return validateActionRules();
 73  
     }
 74  
 
 75  
     /**
 76  
      * Records the approve action.
 77  
      * - Checks to make sure the document status allows the action.
 78  
      * - Checks that the user has not taken a previous action.
 79  
      * - Deactivates the pending requests for this user
 80  
      * - Records the action
 81  
      *
 82  
      * @throws InvalidActionTakenException
 83  
      */
 84  
     public void recordAction() throws InvalidActionTakenException {
 85  0
             MDC.put("docId", getRouteHeader().getDocumentId());
 86  0
         updateSearchableAttributesIfPossible();
 87  
 
 88  0
         String errorMessage = validateActionRules();
 89  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 90  0
             throw new InvalidActionTakenException(errorMessage);
 91  
         }
 92  
 
 93  0
         LOG.debug("Revoking adhoc request : " + annotation);
 94  
 
 95  0
         List<ActionRequestValue> requestsToRevoke = new ArrayList<ActionRequestValue>();
 96  0
         List<ActionRequestValue> actionRequests = getActionRequestService().findPendingRootRequestsByDocId(getDocumentId());
 97  0
         for (ActionRequestValue actionRequest : actionRequests)
 98  
         {
 99  0
             if (matchesActionRequest(revoke, actionRequest))
 100  
             {
 101  0
                 requestsToRevoke.add(actionRequest);
 102  
             }
 103  
         }
 104  0
         if (requestsToRevoke.isEmpty() && actionRequestId != null) {
 105  0
                 throw new InvalidActionTakenException("Failed to revoke action request with id " + actionRequestId +
 106  
                                 ".  ID does not represent a valid ad hoc request!");
 107  
         }
 108  
 
 109  0
         Recipient delegator = findDelegatorForActionRequests(actionRequests);
 110  0
         LOG.debug("Record the revoke action");
 111  0
         ActionTakenValue actionTaken = saveActionTaken(delegator);
 112  
 
 113  0
         LOG.debug("Revoke all matching action requests, number of matching requests: " + requestsToRevoke.size());
 114  0
         getActionRequestService().deactivateRequests(actionTaken, requestsToRevoke);
 115  0
         notifyActionTaken(actionTaken);
 116  
 
 117  0
     }
 118  
     
 119  
     /**
 120  
          * Determines if the given action request is an ad hoc request which matches this set of criteria.
 121  
          */
 122  
         protected boolean matchesActionRequest(AdHocRevoke adHocRevokeCommand, ActionRequestValue actionRequest) {
 123  0
                 if (!actionRequest.isAdHocRequest()) {
 124  0
                         return false;
 125  
                 }                
 126  0
                 if (actionRequestId != null) {
 127  0
                         return actionRequestId.equals(actionRequest.getActionRequestId());
 128  0
                 } else if (adHocRevokeCommand != null) {
 129  0
                         boolean principalOrGroupId = !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds()) || !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds());
 130  0
                         if (!CollectionUtils.isEmpty(adHocRevokeCommand.getNodeNames()) && !adHocRevokeCommand.getNodeNames().contains(actionRequest.getNodeInstance().getName())) {
 131  0
                                 return false;
 132  
                         }
 133  0
                         if (actionRequest.isUserRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds())) {
 134  0
                                 return adHocRevokeCommand.getPrincipalIds().contains(actionRequest.getPrincipalId());
 135  
                         }
 136  0
                         if (actionRequest.isGroupRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds())) {
 137  0
                                 return adHocRevokeCommand.getGroupIds().contains(actionRequest.getGroupId());
 138  
                         }
 139  0
                         return !principalOrGroupId;
 140  
                 }
 141  0
                 return true;
 142  
         }
 143  
 
 144  
 }