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