KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > executor > ResponseInstruction


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.springframework.webflow.executor;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.core.style.ToStringCreator;
21 import org.springframework.util.Assert;
22 import org.springframework.webflow.execution.FlowExecutionContext;
23 import org.springframework.webflow.execution.ViewSelection;
24 import org.springframework.webflow.execution.support.ApplicationView;
25 import org.springframework.webflow.execution.support.ExternalRedirect;
26 import org.springframework.webflow.execution.support.FlowDefinitionRedirect;
27 import org.springframework.webflow.execution.support.FlowExecutionRedirect;
28
29 /**
30  * Immutable value object that provides clients with information about a
31  * response to issue.
32  * <p>
33  * There are five different <i>types</i> of response instruction:
34  * <ul>
35  * <li>An {@link #isApplicationView() application view}.</li>
36  * <li>A {@link #isFlowExecutionRedirect() flow execution redirect}, showing
37  * an application view via a redirect that refreshes an ongoing flow
38  * execution.</li>
39  * <li>A {@link #isFlowDefinitionRedirect() flow definition redirect},
40  * launching an entirely new flow execution.</li>
41  * <li>An {@link #isExternalRedirect() external redirect}, redirecting
42  * to an external URL.</li>
43  * <li>A {@link #isNull() null view}, not showing a response at all.</li>
44  * </ul>
45  *
46  * @author Keith Donald
47  * @author Erwin Vervaet
48  */

49 public class ResponseInstruction implements Serializable JavaDoc {
50
51     /**
52      * The persistent identifier of the flow execution that
53      * resulted in this response instruction.
54      */

55     private String JavaDoc flowExecutionKey;
56
57     /**
58      * Basic state info on the flow execution.
59      */

60     private transient FlowExecutionContext flowExecutionContext;
61
62     /**
63      * The view selection that was made.
64      */

65     private ViewSelection viewSelection;
66
67     /**
68      * Create a new response instruction for a paused flow execution.
69      * @param flowExecutionKey the persistent identifier of the flow execution
70      * @param flowExecutionContext the current flow execution context
71      * @param viewSelection the selected view
72      */

73     public ResponseInstruction(String JavaDoc flowExecutionKey, FlowExecutionContext flowExecutionContext,
74             ViewSelection viewSelection) {
75         Assert.notNull(flowExecutionKey, "The flow execution key is required");
76         this.flowExecutionKey = flowExecutionKey;
77         init(flowExecutionContext, viewSelection);
78     }
79
80     /**
81      * Create a new response instruction for an ended flow execution. No
82      * flow execution key needs to be provided since the flow execution no longer
83      * exists and cannot be referenced any longer.
84      * @param flowExecutionContext the current flow execution context (inactive)
85      * @param viewSelection the selected view
86      */

87     public ResponseInstruction(FlowExecutionContext flowExecutionContext, ViewSelection viewSelection) {
88         init(flowExecutionContext, viewSelection);
89     }
90
91     /**
92      * Helper to initialize the flow execution context and view selection.
93      */

94     private void init(FlowExecutionContext flowExecutionContext, ViewSelection viewSelection) {
95         Assert.notNull(flowExecutionContext, "The flow execution context is required");
96         Assert.notNull(viewSelection, "The view selection is required");
97         this.flowExecutionContext = flowExecutionContext;
98         this.viewSelection = viewSelection;
99     }
100
101     /**
102      * Returns the persistent identifier of the flow execution.
103      */

104     public String JavaDoc getFlowExecutionKey() {
105         return flowExecutionKey;
106     }
107
108     /**
109      * Returns the flow execution context representing the current state of the
110      * execution. It could be that the returned flow execution is
111      * {@link FlowExecutionContext#isActive() inactive}.
112      */

113     public FlowExecutionContext getFlowExecutionContext() {
114         return flowExecutionContext;
115     }
116
117     /**
118      * Returns the view selection selected by the flow execution.
119      */

120     public ViewSelection getViewSelection() {
121         return viewSelection;
122     }
123
124     /**
125      * Returns true if this is an instruction to render an application view for
126      * an "active" (in progress) flow execution.
127      */

128     public boolean isActiveView() {
129         return isApplicationView() && flowExecutionContext.isActive();
130     }
131
132     /**
133      * Returns true if this is an instruction to render an application view for
134      * an "ended" (inactive) flow execution from an end state.
135      */

136     public boolean isEndingView() {
137         return isApplicationView() && !flowExecutionContext.isActive();
138     }
139     
140     // response types
141

142     /**
143      * Returns true if this is an "application view" (forward) response
144      * instruction.
145      */

146     public boolean isApplicationView() {
147         return viewSelection instanceof ApplicationView;
148     }
149
150     /**
151      * Returns true if this is an instruction to perform a redirect to the
152      * current flow execution to render an application view.
153      */

154     public boolean isFlowExecutionRedirect() {
155         return viewSelection instanceof FlowExecutionRedirect;
156     }
157
158     /**
159      * Returns true if this is an instruction to launch an entirely new
160      * (independent) flow execution.
161      */

162     public boolean isFlowDefinitionRedirect() {
163         return viewSelection instanceof FlowDefinitionRedirect;
164     }
165
166     /**
167      * Returns true if this an instruction to perform a redirect to an external
168      * URL.
169      */

170     public boolean isExternalRedirect() {
171         return viewSelection instanceof ExternalRedirect;
172     }
173
174     /**
175      * Returns true if this is a "null" response instruction, e.g.
176      * no response needs to be rendered.
177      */

178     public boolean isNull() {
179         return viewSelection == ViewSelection.NULL_VIEW;
180     }
181
182     public boolean equals(Object JavaDoc o) {
183         if (!(o instanceof ResponseInstruction)) {
184             return false;
185         }
186         ResponseInstruction other = (ResponseInstruction)o;
187         if (getFlowExecutionKey() != null) {
188             return getFlowExecutionKey().equals(other.getFlowExecutionKey())
189                     && viewSelection.equals(other.viewSelection);
190         }
191         else {
192             return other.getFlowExecutionKey() == null && viewSelection.equals(other.viewSelection);
193         }
194     }
195
196     public int hashCode() {
197         int hashCode = viewSelection.hashCode();
198         if (getFlowExecutionKey() != null) {
199             hashCode += getFlowExecutionKey().hashCode();
200         }
201         return hashCode;
202     }
203
204     public String JavaDoc toString() {
205         return new ToStringCreator(this).append("flowExecutionKey", flowExecutionKey)
206             .append("viewSelection", viewSelection).append("flowExecutionContext", flowExecutionContext).toString();
207     }
208 }
Popular Tags