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