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 org.apache.log4j.MDC;
20  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
22  import org.kuali.rice.kew.doctype.DocumentTypePolicy;
23  import org.kuali.rice.kew.exception.InvalidActionTakenException;
24  import org.kuali.rice.kew.exception.ResourceUnavailableException;
25  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
26  import org.kuali.rice.kew.util.KEWConstants;
27  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
28  
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  
34  /**
35   *
36   * ClearFYIAction deactivates the user requests.
37   *
38   * The routeheader is first checked to make sure the action is valid for the document.
39   * Next the user is checked to make sure he/she has not taken a previous action on this
40   * document at the actions responsibility or below. Any requests related to this user
41   * are deactivated.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ClearFYIAction extends ActionTakenEvent {
46      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClearFYIAction.class);
47  
48      /**
49       * @param rh
50       *            RouteHeader for the document upon which the action is taken.
51       * @param principal
52       *            User taking the action.
53       */
54      public ClearFYIAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
55          super(KEWConstants.ACTION_TAKEN_FYI_CD, rh, principal);
56      }
57  
58      /**
59       * @param rh
60       *            RouteHeader for the document upon which the action is taken.
61       * @param principal
62       *            User taking the action.
63       * @param annotation
64       *            User comment on the action taken
65       */
66      public ClearFYIAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
67          super(KEWConstants.ACTION_TAKEN_FYI_CD, rh, principal, annotation);
68      }
69      
70      /**
71       * Method to check if the Action is currently valid on the given document
72       * @return  returns an error message to give system better identifier for problem
73       */
74      public String validateActionRules() {
75          return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
76      }
77  
78      public String validateActionRules(List<ActionRequestValue> actionRequests) {
79          if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
80              return "Document is not in a state to have FYI processed";
81          }
82          List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_FYI_REQ);
83          if (!isActionCompatibleRequest(filteredActionRequests)) {
84              return "No request for the user is compatible " + "with the ClearFYI action";
85          }
86          return "";
87      }
88  
89      public boolean isActionCompatibleRequest(List requests) {
90  
91          // can always cancel saved or initiated document
92          if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
93              return true;
94          }
95  
96          boolean actionCompatible = false;
97          Iterator ars = requests.iterator();
98          ActionRequestValue actionRequest = null;
99  
100         while (ars.hasNext()) {
101             actionRequest = (ActionRequestValue) ars.next();
102 
103             //FYI request matches all but deny and cancel
104             if (KEWConstants.ACTION_REQUEST_FYI_REQ.equals(actionRequest.getActionRequested())) {
105                 actionCompatible = true;
106                 break;
107             }
108         }
109 
110         return actionCompatible;
111     }
112 
113     /**
114      * Processes the clear FYI action. - Checks to make sure the document status allows the action. - Checks that the user has not taken a previous action. - Deactivates the pending requests for this user
115      *
116      * @throws InvalidActionTakenException
117      * @throws ResourceUnavailableException
118      */
119     public void recordAction() throws InvalidActionTakenException {
120         MDC.put("docId", getRouteHeader().getDocumentId());
121         updateSearchableAttributesIfPossible();
122 
123         LOG.debug("Clear FYI for document : " + annotation);
124         LOG.debug("Checking to see if the action is legal");
125 
126         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_FYI_REQ);
127         if (actionRequests == null || actionRequests.isEmpty()) {
128         	DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy();
129         	if (allowUnrequested != null) {
130         		if (!allowUnrequested.getPolicyValue()) {
131         			throw new InvalidActionTakenException("No request for the user is compatible " + "with the ClearFYI action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document.");
132         		}
133         	}
134         }
135         
136         String errorMessage = validateActionRules(actionRequests);
137         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
138             throw new InvalidActionTakenException(errorMessage);
139         }
140 
141         ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(actionRequests));
142 
143         LOG.debug("Deactivate all pending action requests");
144         getActionRequestService().deactivateRequests(actionTaken, actionRequests);
145         notifyActionTaken(actionTaken);
146     }
147 }