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.ActionRequestFactory;
21  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
22  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
23  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
24  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
25  import org.kuali.rice.kew.exception.InvalidActionTakenException;
26  import org.kuali.rice.kew.exception.WorkflowException;
27  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.util.KEWConstants;
30  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
31  
32  
33  import java.util.List;
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, PrincipalContract principal) {
50  	super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal);
51      }
52  
53      public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract 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().getDocumentId(), 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().getDocumentId());
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 (!org.apache.commons.lang.StringUtils.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 	    this.getActionRequestService().saveActionRequest(generateSaveRequest());
114 	    notifyActionTaken(actionTaken);
115 	    LOG.debug("Marking document saved");
116 	    try {
117 		String oldStatus = getRouteHeader().getDocRouteStatus();
118 		getRouteHeader().markDocumentSaved();
119 		String newStatus = getRouteHeader().getDocRouteStatus();
120 		notifyStatusChange(newStatus, oldStatus);
121 		KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
122 	    } catch (WorkflowException ex) {
123 		LOG.warn(ex, ex);
124 		throw new InvalidActionTakenException(ex.getMessage());
125 	    }
126 	}
127     }
128 
129     protected ActionRequestValue generateSaveRequest() {
130         RouteNodeInstance initialNode = null;
131         List initialNodes = KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId());
132     	if (!initialNodes.isEmpty()) {
133     	    initialNode = (RouteNodeInstance)initialNodes.get(0);
134     	}
135         //RouteNodeInstance initialNode = (RouteNodeInstance) KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId()).get(0);
136     	ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), initialNode);
137     	ActionRequestValue saveRequest = arFactory.createActionRequest(KEWConstants.ACTION_REQUEST_COMPLETE_REQ,
138                 0, new KimPrincipalRecipient(getPrincipal()), RESPONSIBILITY_DESCRIPTION, KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID,
139     		Boolean.TRUE, annotation);
140     	//      this.getActionRequestService().saveActionRequest(saveRequest);
141     	this.getActionRequestService().activateRequest(saveRequest);
142     	return saveRequest;
143     }
144 
145 }