001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.engine;
017
018import java.io.Serializable;
019import java.util.HashMap;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Map;
023
024import org.kuali.rice.core.framework.util.ApplicationThreadLocal;
025import org.kuali.rice.kew.actionrequest.ActionRequestValue;
026import org.kuali.rice.kew.api.WorkflowRuntimeException;
027import org.kuali.rice.kew.engine.node.RouteNodeInstance;
028import org.kuali.rice.kew.routeheader.DocumentContent;
029import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
030import org.kuali.rice.kew.routeheader.StandardDocumentContent;
031
032
033/**
034 * Represents the current context of a Document being processed by the engine.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class RouteContext implements Serializable {
039
040        private static final long serialVersionUID = -7125137491367944594L;
041
042        private DocumentRouteHeaderValue routeHeader;
043
044        private DocumentContent documentContent;
045
046        private RouteNodeInstance nodeInstance;
047
048        private EngineState engineState;
049
050        private ActionRequestValue actionRequest;
051
052        private ActivationContext activationContext = new ActivationContext(!ActivationContext.CONTEXT_IS_SIMULATION);
053
054        private boolean doNotSendApproveNotificationEmails = false;
055
056        private Map parameters = new HashMap();
057        
058        private boolean searchIndexingRequestedForContext = false;
059
060        public RouteContext() {
061        }
062
063        private static ThreadLocal<List<RouteContext>> ROUTE_CONTEXT_STACK = new ApplicationThreadLocal<List<RouteContext>>() {
064                protected List<RouteContext> initialValue() {
065                        List<RouteContext> contextStack = new LinkedList<RouteContext>();
066                        contextStack.add(0, new RouteContext());
067                        return contextStack;
068                }
069        };
070
071        public static RouteContext getCurrentRouteContext() {
072                return ROUTE_CONTEXT_STACK.get().get(0);
073        }
074
075        public static void clearCurrentRouteContext() {
076                ROUTE_CONTEXT_STACK.get().remove(0);
077                ROUTE_CONTEXT_STACK.get().add(0, new RouteContext());
078        }
079
080        public static RouteContext createNewRouteContext() {
081                ROUTE_CONTEXT_STACK.get().add(0, new RouteContext());
082                return getCurrentRouteContext();
083        }
084
085        public static RouteContext releaseCurrentRouteContext() {
086                return ROUTE_CONTEXT_STACK.get().remove(0);
087        }
088
089        /**
090         * @deprecated use getDocument() instead
091         */
092        public DocumentRouteHeaderValue getRouteHeader() {
093                return routeHeader;
094        }
095
096        /**
097         * @deprecated user setDocument() instead
098         */
099        public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
100                this.routeHeader = routeHeader;
101        }
102
103        public DocumentRouteHeaderValue getDocument() {
104                return routeHeader;
105        }
106
107        public void setDocument(DocumentRouteHeaderValue routeHeader) {
108                this.routeHeader = routeHeader;
109                try {
110                        setDocumentContent(new StandardDocumentContent(routeHeader.getDocContent(), this));
111                } catch (Exception e) {
112                        throw new WorkflowRuntimeException(e);
113                }
114        }
115
116        public DocumentContent getDocumentContent() {
117                return documentContent;
118        }
119
120        public void setDocumentContent(DocumentContent documentContent) {
121                this.documentContent = documentContent;
122        }
123
124        public RouteNodeInstance getNodeInstance() {
125                return nodeInstance;
126        }
127
128        public void setNodeInstance(RouteNodeInstance nodeInstance) {
129                this.nodeInstance = nodeInstance;
130        }
131
132        public EngineState getEngineState() {
133                return engineState;
134        }
135
136        public void setEngineState(EngineState engineState) {
137                this.engineState = engineState;
138        }
139
140        public ActionRequestValue getActionRequest() {
141                return actionRequest;
142        }
143
144        public void setActionRequest(ActionRequestValue actionRequest) {
145                this.actionRequest = actionRequest;
146        }
147
148        public boolean isSimulation() {
149                if (activationContext == null) {
150                        return false;
151                }
152                return activationContext.isSimulation();
153        }
154
155        public ActivationContext getActivationContext() {
156                return activationContext;
157        }
158
159        public void setActivationContext(ActivationContext activationContext) {
160                this.activationContext = activationContext;
161        }
162
163        public boolean isDoNotSendApproveNotificationEmails() {
164                return doNotSendApproveNotificationEmails;
165        }
166
167        public void setDoNotSendApproveNotificationEmails(boolean sendNotificationEmails) {
168                this.doNotSendApproveNotificationEmails = sendNotificationEmails;
169        }
170
171        public Map getParameters() {
172                return parameters;
173        }
174
175        public void setParameters(Map parameters) {
176                this.parameters = parameters;
177        }
178
179        /**
180         * Determines if search indexing has already been requested during this context
181         * @return the searchIndexingRequestedForContext: true if search indexing has been requested, false otherwise
182         */
183        public boolean isSearchIndexingRequestedForContext() {
184                return this.searchIndexingRequestedForContext;
185        }
186
187        /**
188         * Sets the route context to let it know that search indexing has been requested
189         */
190        public void requestSearchIndexingForContext() {
191                this.searchIndexingRequestedForContext = true;
192        }
193        
194}