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.List;
20  
21  import org.apache.log4j.MDC;
22  import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
23  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
24  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
25  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
26  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
27  import org.kuali.rice.kew.exception.InvalidActionTakenException;
28  import org.kuali.rice.kew.exception.WorkflowException;
29  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.util.KEWConstants;
32  import org.kuali.rice.kew.util.Utilities;
33  import org.kuali.rice.kim.bo.entity.KimPrincipal;
34  
35  
36  /**
37   * Saves a document.  Puts the document in the persons action list that saved the document.
38   * This can currently only be done by the initiator of the document.
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class SaveActionEvent extends ActionTakenEvent {
44  
45      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SaveActionEvent.class);
46  
47      private static final String RESPONSIBILITY_DESCRIPTION = "Initiator needs to complete document.";
48  
49      public SaveActionEvent(DocumentRouteHeaderValue routeHeader, KimPrincipal principal) {
50  	super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal);
51      }
52  
53      public SaveActionEvent(DocumentRouteHeaderValue routeHeader, KimPrincipal principal, String annotation) {
54  	super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal, annotation);
55      }
56  
57      /* (non-Javadoc)
58       * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
59       */
60      @Override
61      public String validateActionRules() {
62      	return validateActionRulesCustom(true);
63      }
64  
65      private String validateActionRulesCustom(boolean checkIfActionIsValid) {
66      	if (checkIfActionIsValid && (!getRouteHeader().isValidActionToTake(getActionPerformedCode()))) {
67      		return "Document is not in a state to be saved";
68      	}
69      	// check state before checking kim
70      	if (! KEWServiceLocator.getDocumentTypePermissionService().canSave(getPrincipal().getPrincipalId(), getRouteHeader().getRouteHeaderId().toString(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
71      		return "User is not authorized to Save document";
72      	}
73      	return "";
74      }
75      
76      /**
77       * This overridden method ...
78       * 
79       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules(java.util.List)
80       */
81      @Override
82      public String validateActionRules(List<ActionRequestValue> actionRequests) {
83      	return validateActionRules();
84      }
85  
86      public void recordAction() throws InvalidActionTakenException {
87  	MDC.put("docId", getRouteHeader().getRouteHeaderId());
88  	LOG.debug("Checking to see if the action is legal");
89  	/* Code below for variable 'checkIfActionIsValid' is used to identify when the 
90  	 * DocumentRouteHeaderValue 'legal actions' should be checked for the current
91  	 * document.  The 'legal actions' for a document that is in status ENROUTE or 
92  	 * EXCEPTION will currently say that a Save action is not valid to be performed
93  	 * however we still want to allow the Save action to occur if called for backward
94  	 * compatibility issues.
95  	 */
96  	boolean checkIfActionIsValid = true;
97  	if (getRouteHeader().isEnroute() || getRouteHeader().isInException()) {
98  	    // if document is enroute or exception... don't check if the action is valid... we will assume it is valid
99  	    checkIfActionIsValid = false;
100 	}
101 	String errorMessage = validateActionRulesCustom(checkIfActionIsValid);
102 	if (!Utilities.isEmpty(errorMessage)) {
103 	    throw new InvalidActionTakenException(errorMessage);
104 	}
105 
106 	updateSearchableAttributesIfPossible();
107 
108 	//    if (getRouteHeader().isValidActionToTake(getActionTakenCode())) {
109 	if (getRouteHeader().isStateInitiated()) {
110 	    LOG.debug("Record the save action");
111 	    ActionTakenValue actionTaken = saveActionTaken();
112 	    getRouteHeader().getActionRequests().add(generateSaveRequest());
113 	    notifyActionTaken(actionTaken);
114 	    LOG.debug("Marking document saved");
115 	    try {
116 		String oldStatus = getRouteHeader().getDocRouteStatus();
117 		getRouteHeader().markDocumentSaved();
118 		String newStatus = getRouteHeader().getDocRouteStatus();
119 		notifyStatusChange(newStatus, oldStatus);
120 		KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
121 	    } catch (WorkflowException ex) {
122 		LOG.warn(ex, ex);
123 		throw new InvalidActionTakenException(ex.getMessage());
124 	    }
125 	}
126     }
127 
128     protected ActionRequestValue generateSaveRequest() {
129         RouteNodeInstance initialNode = null;
130         List initialNodes = KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getRouteHeaderId());
131     	if (!initialNodes.isEmpty()) {
132     	    initialNode = (RouteNodeInstance)initialNodes.get(0);
133     	}
134         //RouteNodeInstance initialNode = (RouteNodeInstance) KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getRouteHeaderId()).get(0);
135     	ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), initialNode);
136     	ActionRequestValue saveRequest = arFactory.createActionRequest(KEWConstants.ACTION_REQUEST_COMPLETE_REQ,
137                 0, new KimPrincipalRecipient(getPrincipal()), RESPONSIBILITY_DESCRIPTION, KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID,
138     		Boolean.TRUE, annotation);
139     	//      this.getActionRequestService().saveActionRequest(saveRequest);
140     	this.getActionRequestService().activateRequest(saveRequest);
141     	return saveRequest;
142     }
143 
144 }