View Javadoc
1   /**
2    * Copyright 2005-2014 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 org.apache.log4j.MDC;
19  import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
20  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
22  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.api.action.ActionType;
25  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
26  import org.kuali.rice.kew.api.exception.WorkflowException;
27  import org.kuali.rice.kew.doctype.bo.DocumentType;
28  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
29  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  
38  /**
39   * Super user Approves a single action request.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class SuperUserActionRequestApproveEvent extends SuperUserActionTakenEvent {
44      /**
45       * This is the only action which is polymorphic...the action taken code is dynamically determined
46       * based on action requested.  All other actions' action taken code is immutable, so the field could otherwise
47       * be set to final and initialized in the constructor...however it would not be advisable to perform in the
48       * constructor the work required by this class to determine the action taken.  So for now the class initializes
49       * the action taken to null (this would be the behavior anyway if the constructor did not enforce an action taken code
50       * to be supplied).  An alternative would be to do away with the stored superclass field and simply delegate to a subclass
51       * getActionTakenCode implementation when necessary.  It is also not clear that this would be a good choice as it may be
52       * called multiple times in arbitrary contexts.
53       */
54      private static final String UNDEFINED_ACTION_TAKEN_CODE = null;
55  
56      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionRequestApproveEvent.class);
57      private String actionRequestId;
58  
59      public SuperUserActionRequestApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
60          super(UNDEFINED_ACTION_TAKEN_CODE, KewApiConstants.SUPER_USER_ACTION_REQUEST_APPROVE, routeHeader, principal);
61      }
62  
63      public SuperUserActionRequestApproveEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String actionRequestId, String annotation, boolean runPostProcessor) {
64          super(UNDEFINED_ACTION_TAKEN_CODE, KewApiConstants.SUPER_USER_ACTION_REQUEST_APPROVE, routeHeader, principal, annotation, runPostProcessor);
65          this.actionRequestId = actionRequestId;
66      }
67  
68      public void setActionTaken() {
69          String actionRequestCode = "";
70  
71          ActionRequestValue actionRequest = getActionRequestService().findByActionRequestId(actionRequestId);
72  
73          setActionRequest(actionRequest);
74  
75          actionRequestCode = actionRequest.getActionRequested();
76  
77          ActionType suActionType = ActionType.toSuperUserActionType(ActionType.fromCode(actionRequestCode, true));
78          if (suActionType == null) {
79              //TODO this should be checked
80              LOG.error("Invalid SU delegation action request code: " + actionRequestCode);
81              throw new RuntimeException("Invalid SU delegation action request code: " + actionRequestCode);
82          } else {
83              this.setActionTakenCode(suActionType.getCode());
84          }
85      }
86  
87      protected ActionTakenValue processActionRequests() throws InvalidActionTakenException {
88          //this method has been written to process all of the actions though only approvals are currently processed
89  
90          DocumentType docType = getRouteHeader().getDocumentType();
91  //        boolean userAuthorized = getDocumentTypeService().verifySUAuthority(docType, getUser());
92  
93          String errorMessage = super.validateActionRules();
94          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
95              LOG.info("User not authorized");
96              List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
97              errors.add(new WorkflowServiceErrorImpl(errorMessage, SuperUserActionTakenEvent.AUTHORIZATION));
98              throw new WorkflowServiceErrorException(errorMessage, errors);
99          }
100 //        if (!docType.isSuperUser(getUser())) {
101 //            List errors = new ArrayList();
102 //            errors.add(new WorkflowServiceErrorImpl("User not authorized for super user action", SuperUserActionTakenEvent.AUTHORIZATION));
103 //            throw new WorkflowServiceErrorException("Super User Authorization Error", errors);
104 //        }
105 
106         this.setActionTaken();
107 
108         MDC.put("docId", getRouteHeader().getDocumentId());
109 
110         LOG.debug("Super User Delegation Action on action request: " + annotation);
111         KimPrincipalRecipient superUserRecipient = null;
112         if (getActionRequest().getPrincipal() != null) {
113         	superUserRecipient = new KimPrincipalRecipient(getActionRequest().getPrincipal());
114         }
115         
116         ActionTakenValue actionTaken = this.saveActionTaken(superUserRecipient);
117 
118         LOG.debug("Deactivate this action request");
119 
120         ActionRequestValue request = getActionRequest();
121         request = getActionRequestService().deactivateRequest(actionTaken, request);
122         if (docType.getSuperUserApproveNotificationPolicy().getPolicyValue() && request.isApproveOrCompleteRequest()) {
123         	KEWServiceLocator.getActionRequestService().activateRequest(
124         	new ActionRequestFactory(this.getRouteHeader()).createNotificationRequest(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, request.getPrincipal(), this.getActionTakenCode(), getPrincipal(), null));
125         }
126         notifyActionTaken(actionTaken);
127 
128         if (!(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD.equals(this.getActionTakenCode()) && KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD.equals(this.getActionTakenCode()))) {
129             if (getRouteHeader().isInException()) {
130                 LOG.debug("Moving document back to Enroute from Exception");
131 
132                 String oldStatus = getRouteHeader().getDocRouteStatus();
133                 this.getRouteHeader().markDocumentEnroute();
134 
135                 String newStatus = getRouteHeader().getDocRouteStatus();
136                 this.notifyStatusChange(newStatus, oldStatus);
137                 DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
138                         saveRouteHeader(getRouteHeader());
139                 setRouteHeader(routeHeaderValue);
140             }
141             else if (getRouteHeader().isStateSaved()) {
142         	if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(request.getResponsibilityId())) {
143                     LOG.debug("Moving document to Enroute from Saved because action request was request generated by save action");
144             	
145                     String oldStatus = getRouteHeader().getDocRouteStatus();
146                     this.getRouteHeader().markDocumentEnroute();
147                     String newStatus = getRouteHeader().getDocRouteStatus();
148                     this.notifyStatusChange(newStatus, oldStatus);
149                 DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
150                         saveRouteHeader(getRouteHeader());
151                 setRouteHeader(routeHeaderValue);
152         	}
153             }
154         }
155 
156         return actionTaken;
157     }
158 
159     public void recordAction() throws InvalidActionTakenException {
160         this.processActionRequests();
161         this.queueDocumentProcessing();
162     }
163 
164     protected void markDocument() throws WorkflowException {
165     }
166 }