View Javadoc

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