View Javadoc
1   /**
2    * Copyright 2005-2016 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      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          super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal);
47      }
48  
49      public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String actionRequestId, String annotation) {
50          super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation);
51          this.actionRequestId = actionRequestId;
52      }
53      
54      public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, AdHocRevoke revoke, String annotation) {
55          super(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation);
56          this.revoke = revoke;
57      }
58  
59      /* (non-Javadoc)
60       * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
61       */
62      @Override
63      public String validateActionRules() {
64          if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
65              return "Revoke adhoc request is not valid on this document";
66          }
67          return "";
68      }
69      
70      @Override
71      public String validateActionRules(List<ActionRequestValue> actionRequests) {
72      	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      	MDC.put("docId", getRouteHeader().getDocumentId());
86          updateSearchableAttributesIfPossible();
87  
88          String errorMessage = validateActionRules();
89          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
90              throw new InvalidActionTakenException(errorMessage);
91          }
92  
93          LOG.debug("Revoking adhoc request : " + annotation);
94  
95          List<ActionRequestValue> requestsToRevoke = new ArrayList<ActionRequestValue>();
96          List<ActionRequestValue> actionRequests = getActionRequestService().findPendingRootRequestsByDocId(getDocumentId());
97          for (ActionRequestValue actionRequest : actionRequests)
98          {
99              if (matchesActionRequest(revoke, actionRequest))
100             {
101                 requestsToRevoke.add(actionRequest);
102             }
103         }
104         if (requestsToRevoke.isEmpty() && actionRequestId != null) {
105         	throw new InvalidActionTakenException("Failed to revoke action request with id " + actionRequestId +
106         			".  ID does not represent a valid ad hoc request!");
107         }
108 
109         Recipient delegator = findDelegatorForActionRequests(actionRequests);
110         LOG.debug("Record the revoke action");
111         ActionTakenValue actionTaken = saveActionTaken(delegator);
112 
113         LOG.debug("Revoke all matching action requests, number of matching requests: " + requestsToRevoke.size());
114         getActionRequestService().deactivateRequests(actionTaken, requestsToRevoke);
115         notifyActionTaken(actionTaken);
116 
117     }
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 		if (!actionRequest.isAdHocRequest()) {
124 			return false;
125 		}		
126 		if (actionRequestId != null) {
127 			return actionRequestId.equals(actionRequest.getActionRequestId());
128 		} else if (adHocRevokeCommand != null) {
129 			boolean principalOrGroupId = !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds()) || !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds());
130 			if (!CollectionUtils.isEmpty(adHocRevokeCommand.getNodeNames()) && !adHocRevokeCommand.getNodeNames().contains(actionRequest.getNodeInstance().getName())) {
131 				return false;
132 			}
133 			if (actionRequest.isUserRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getPrincipalIds())) {
134 				return adHocRevokeCommand.getPrincipalIds().contains(actionRequest.getPrincipalId());
135 			}
136 			if (actionRequest.isGroupRequest() && !CollectionUtils.isEmpty(adHocRevokeCommand.getGroupIds())) {
137 				return adHocRevokeCommand.getGroupIds().contains(actionRequest.getGroupId());
138 			}
139 			return !principalOrGroupId;
140 		}
141 		return true;
142 	}
143 
144 }