View Javadoc

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