View Javadoc

1   /**
2    * Copyright 2005-2012 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.kuali.rice.kew.actionrequest.ActionRequestValue;
19  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
20  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
21  import org.kuali.rice.kew.doctype.bo.DocumentType;
22  import org.kuali.rice.kew.api.exception.WorkflowException;
23  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
24  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
25  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
28  
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  
34  /**
35   * Super class for all super user action takens.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public abstract class SuperUserActionTakenEvent extends ActionTakenEvent {
40  
41      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionTakenEvent.class);
42  
43      protected String superUserAction;
44      //protected DocumentRouteStatusChange event;
45      private ActionRequestValue actionRequest;
46      public static String AUTHORIZATION = "general.routing.superuser.notAuthorized";
47  
48      public SuperUserActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
49          super(actionTakenCode, routeHeader, principal);
50      }
51  
52      public SuperUserActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor) {
53          super(actionTakenCode, routeHeader, principal, annotation, runPostProcessor);
54      }
55  
56      /* (non-Javadoc)
57       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
58       */
59      @Override
60      public String validateActionRules() {
61          DocumentType docType = getRouteHeader().getDocumentType();
62          if (!KEWServiceLocator.getDocumentTypePermissionService().canAdministerRouting(getPrincipal().getPrincipalId(), docType)) {
63              return "User not authorized for super user action";
64          }
65          return "";
66      }
67  
68      @Override
69      public String validateActionRules(List<ActionRequestValue> actionRequests) {
70      	return validateActionRules();
71      }
72  
73      public void recordAction() throws InvalidActionTakenException {
74  
75          String errorMessage = validateActionRules();
76          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
77              LOG.info("User not authorized");
78              List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
79              errors.add(new WorkflowServiceErrorImpl(errorMessage, AUTHORIZATION));
80              throw new WorkflowServiceErrorException(errorMessage, errors);
81          }
82  
83          processActionRequests();
84  
85          try {
86          	String oldStatus = getRouteHeader().getDocRouteStatus();
87          	//if the document is initiated then set it enroute so we can transition to any other status
88          	if (getRouteHeader().isStateInitiated()) {
89          		getRouteHeader().markDocumentEnroute();
90          		notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus);
91          	}
92              markDocument();
93              String newStatus = getRouteHeader().getDocRouteStatus();
94              notifyStatusChange(newStatus, oldStatus);
95          } catch (Exception ex) {
96              LOG.error("Caught Exception talking to post processor", ex);
97              throw new RuntimeException(ex.getMessage());
98          }
99  
100     }
101 
102     protected abstract void markDocument() throws WorkflowException;
103 
104     protected void processActionRequests() throws InvalidActionTakenException {
105         LOG.debug("Processing pending action requests");
106 
107         ActionTakenValue actionTaken = saveActionTaken();
108 
109         List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
110 
111         for (ActionRequestValue actionRequest : actionRequests)
112         {
113             getActionRequestService().deactivateRequest(actionTaken, actionRequest);
114         }
115 
116         notifyActionTaken(actionTaken);
117     }
118 
119     public ActionRequestValue getActionRequest() {
120         return actionRequest;
121     }
122 
123     public void setActionRequest(ActionRequestValue actionRequest) {
124         this.actionRequest = actionRequest;
125     }
126 
127 }