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.engine;
17  
18  import java.io.Serializable;
19  import java.util.HashMap;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.kuali.rice.core.framework.util.ApplicationThreadLocal;
26  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
27  import org.kuali.rice.kew.api.WorkflowRuntimeException;
28  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
29  import org.kuali.rice.kew.routeheader.DocumentContent;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.routeheader.StandardDocumentContent;
32  
33  
34  /**
35   * Represents the current context of a Document being processed by the engine.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class RouteContext implements Serializable {
40  
41  	private static final long serialVersionUID = -7125137491367944594L;
42  
43      private String id;
44  
45  	private DocumentRouteHeaderValue routeHeader;
46  
47  	private DocumentContent documentContent;
48  
49  	private RouteNodeInstance nodeInstance;
50  
51  	private EngineState engineState;
52  
53  	private ActionRequestValue actionRequest;
54  
55  	private ActivationContext activationContext = new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION);
56  
57  	private boolean doNotSendApproveNotificationEmails = false;
58  
59  	private Map parameters = new HashMap();
60  	
61  	private boolean searchIndexingRequestedForContext = false;
62  
63  	public RouteContext() {
64          id = new String();
65  	}
66  
67  	private static ThreadLocal<List<RouteContext>> ROUTE_CONTEXT_STACK = new ApplicationThreadLocal<List<RouteContext>>() {
68  		protected List<RouteContext> initialValue() {
69  			List<RouteContext> contextStack = new LinkedList<RouteContext>();
70  			contextStack.add(0, new RouteContext());
71  			return contextStack;
72  		}
73  	};
74  
75  	public static RouteContext getCurrentRouteContext() {
76  		return ROUTE_CONTEXT_STACK.get().get(0);
77  	}
78  
79      public static void clearRouteContextByDocumentId(String documentId) {
80          if(StringUtils.isNotBlank(documentId)) {
81              // Pop top stack element with matching document id and replace with empty
82              for(int i = 0; i < ROUTE_CONTEXT_STACK.get().size(); i++) {
83                  if(ROUTE_CONTEXT_STACK.get().get(i).routeHeader.getDocumentId().equals(documentId)) {
84                      ROUTE_CONTEXT_STACK.get().remove(i);
85                      ROUTE_CONTEXT_STACK.get().add(i, new RouteContext());
86                  }
87              }
88          }
89  
90      }
91  
92  	public static void clearCurrentRouteContext() {
93          ROUTE_CONTEXT_STACK.get().remove(0);
94          ROUTE_CONTEXT_STACK.get().add(0, new RouteContext());
95  	}
96  
97  	public static RouteContext createNewRouteContext() {
98  		ROUTE_CONTEXT_STACK.get().add(0, new RouteContext());
99  		return getCurrentRouteContext();
100 	}
101 
102 	public static RouteContext releaseCurrentRouteContext() {
103 		return ROUTE_CONTEXT_STACK.get().remove(0);
104 	}
105 
106 	/**
107 	 * @deprecated use getDocument() instead
108 	 */
109 	public DocumentRouteHeaderValue getRouteHeader() {
110 		return routeHeader;
111 	}
112 
113 	/**
114 	 * @deprecated user setDocument() instead
115 	 */
116 	public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
117 		this.routeHeader = routeHeader;
118 	}
119 
120 	public DocumentRouteHeaderValue getDocument() {
121 		return routeHeader;
122 	}
123 
124 	public void setDocument(DocumentRouteHeaderValue routeHeader) {
125 		this.routeHeader = routeHeader;
126 		try {
127 			setDocumentContent(new StandardDocumentContent(routeHeader.getDocContent(), this));
128 		} catch (Exception e) {
129 			throw new WorkflowRuntimeException(e);
130 		}
131 	}
132 
133 	public DocumentContent getDocumentContent() {
134 		return documentContent;
135 	}
136 
137 	public void setDocumentContent(DocumentContent documentContent) {
138 		this.documentContent = documentContent;
139 	}
140 
141 	public RouteNodeInstance getNodeInstance() {
142 		return nodeInstance;
143 	}
144 
145 	public void setNodeInstance(RouteNodeInstance nodeInstance) {
146 		this.nodeInstance = nodeInstance;
147 	}
148 
149 	public EngineState getEngineState() {
150 		return engineState;
151 	}
152 
153 	public void setEngineState(EngineState engineState) {
154 		this.engineState = engineState;
155 	}
156 
157 	public ActionRequestValue getActionRequest() {
158 		return actionRequest;
159 	}
160 
161 	public void setActionRequest(ActionRequestValue actionRequest) {
162 		this.actionRequest = actionRequest;
163 	}
164 
165 	public boolean isSimulation() {
166 		if (activationContext == null) {
167 			return false;
168 		}
169 		return activationContext.isSimulation();
170 	}
171 
172 	public ActivationContext getActivationContext() {
173 		return activationContext;
174 	}
175 
176 	public void setActivationContext(ActivationContext activationContext) {
177 		this.activationContext = activationContext;
178 	}
179 
180 	public boolean isDoNotSendApproveNotificationEmails() {
181 		return doNotSendApproveNotificationEmails;
182 	}
183 
184 	public void setDoNotSendApproveNotificationEmails(boolean sendNotificationEmails) {
185 		this.doNotSendApproveNotificationEmails = sendNotificationEmails;
186 	}
187 
188 	public Map getParameters() {
189 		return parameters;
190 	}
191 
192 	public void setParameters(Map parameters) {
193 		this.parameters = parameters;
194 	}
195 
196 	/**
197 	 * Determines if search indexing has already been requested during this context
198 	 * @return the searchIndexingRequestedForContext: true if search indexing has been requested, false otherwise
199 	 */
200 	public boolean isSearchIndexingRequestedForContext() {
201 		return this.searchIndexingRequestedForContext;
202 	}
203 
204 	/**
205 	 * Sets the route context to let it know that search indexing has been requested
206 	 */
207 	public void requestSearchIndexingForContext() {
208 		this.searchIndexingRequestedForContext = true;
209 	}
210 	
211 }