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.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.exception.InvalidActionTakenException;
27  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
28  import org.kuali.rice.kew.util.KEWConstants;
29  import org.kuali.rice.kew.util.Utilities;
30  import org.kuali.rice.kim.bo.entity.KimPrincipal;
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 AdHocRevoke revoke;
43  
44      public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal) {
45          super(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal);
46      }
47  
48      public RevokeAdHocAction(DocumentRouteHeaderValue routeHeader, KimPrincipal principal, AdHocRevoke revoke, String annotation) {
49          super(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, routeHeader, principal, annotation);
50          this.revoke = revoke;
51      }
52  
53      /* (non-Javadoc)
54       * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
55       */
56      @Override
57      public String validateActionRules() {
58          if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
59              return "Revoke adhoc request is not valid on this document";
60          }
61          return "";
62      }
63      
64      @Override
65      public String validateActionRules(List<ActionRequestValue> actionRequests) {
66      	return validateActionRules();
67      }
68  
69      /**
70       * Records the approve action.
71       * - Checks to make sure the document status allows the action.
72       * - Checks that the user has not taken a previous action.
73       * - Deactivates the pending requests for this user
74       * - Records the action
75       *
76       * @throws InvalidActionTakenException
77       */
78      public void recordAction() throws InvalidActionTakenException {
79      	MDC.put("docId", getRouteHeader().getRouteHeaderId());
80          updateSearchableAttributesIfPossible();
81  
82          String errorMessage = validateActionRules();
83          if (!Utilities.isEmpty(errorMessage)) {
84              throw new InvalidActionTakenException(errorMessage);
85          }
86  
87          LOG.debug("Revoking adhoc request : " + annotation);
88  
89          List<ActionRequestValue> requestsToRevoke = new ArrayList<ActionRequestValue>();
90          List<ActionRequestValue> actionRequests = getActionRequestService().findPendingRootRequestsByDocId(getRouteHeaderId());
91          for (ActionRequestValue actionRequest : actionRequests)
92          {
93              if (revoke.matchesActionRequest(actionRequest))
94              {
95                  requestsToRevoke.add(actionRequest);
96              }
97          }
98          if (requestsToRevoke.isEmpty() && revoke.getActionRequestId() != null) {
99          	throw new InvalidActionTakenException("Failed to revoke action request with id " + revoke.getActionRequestId() +
100         			".  ID does not represent a valid ad hoc request!");
101         }
102 
103         Recipient delegator = findDelegatorForActionRequests(actionRequests);
104         LOG.debug("Record the revoke action");
105         ActionTakenValue actionTaken = saveActionTaken(delegator);
106 
107         LOG.debug("Revoke all matching action requests, number of matching requests: " + requestsToRevoke.size());
108         getActionRequestService().deactivateRequests(actionTaken, requestsToRevoke);
109         notifyActionTaken(actionTaken);
110 
111     }
112 
113 }