KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > FormObjectAccessor


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.action;
17
18 import org.springframework.validation.BindException;
19 import org.springframework.validation.Errors;
20 import org.springframework.webflow.execution.RequestContext;
21 import org.springframework.webflow.execution.ScopeType;
22
23 /**
24  * Convenience helper that encapsulates logic on how to retrieve and expose form
25  * objects and associated errors to and from a flow execution request context.
26  * <p>
27  * <b>Note</b>: The form object available under the well known attribute name
28  * {@link #CURRENT_FORM_OBJECT_ATTRIBUTE} will be the last ("current") form
29  * object set in the request context. The same is true for the associated errors
30  * object. This implies that special care should be taken when accessing the
31  * form object using this alias if there are multiple form objects available in
32  * the flow execution request context!
33  *
34  * @see org.springframework.webflow.execution.RequestContext
35  * @see org.springframework.validation.Errors
36  *
37  * @author Keith Donald
38  * @author Erwin Vervaet
39  */

40 public class FormObjectAccessor {
41
42     /**
43      * The form object instance is aliased under this attribute name in the flow
44      * context by the default form setup and bind and validate actions.
45      * <p>
46      * Note that if you would have multiple form objects in the request context,
47      * the last one that was used would be available using this alias!
48      * <p>
49      * We need to keep track of the 'current form object' using this attribute
50      * to be able to deal with the limitations of some clients that can only
51      * deal with a single form backing object, e.g. Struts when using the Struts
52      * FlowAction.
53      */

54     private static final String JavaDoc CURRENT_FORM_OBJECT_ATTRIBUTE = "currentFormObject";
55
56     /**
57      * The errors prefix.
58      */

59     //use deprecated API to remain compatible with Spring 1.2.x
60
private static final String JavaDoc ERRORS_PREFIX = BindException.ERROR_KEY_PREFIX;
61     
62     /**
63      * The wrapped request context.
64      */

65     private RequestContext context;
66
67     /**
68      * Creates a form object accessor that wraps the given context.
69      * @param context the flow execution request context
70      */

71     public FormObjectAccessor(RequestContext context) {
72         this.context = context;
73     }
74
75     /**
76      * Returns the current form object name.
77      * @return the current form object name
78      */

79     public static String JavaDoc getCurrentFormObjectName() {
80         return CURRENT_FORM_OBJECT_ATTRIBUTE;
81     }
82     
83     /**
84      * Returns the current form object errors attribute name.
85      * @return the current form object errors attribute name
86      */

87     public static String JavaDoc getCurrentFormErrorsName() {
88         return ERRORS_PREFIX + getCurrentFormObjectName();
89     }
90
91     /**
92      * Gets the form object from the context, using the well-known attribute
93      * name {@link #CURRENT_FORM_OBJECT_ATTRIBUTE}. Will try all scopes.
94      * @return the form object, or null if not found
95      */

96     public Object JavaDoc getCurrentFormObject() {
97         Object JavaDoc formObject = getCurrentFormObject(ScopeType.REQUEST);
98         if (formObject != null) {
99             return formObject;
100         }
101         formObject = getCurrentFormObject(ScopeType.FLASH);
102         if (formObject != null) {
103             return formObject;
104         }
105         formObject = getCurrentFormObject(ScopeType.FLOW);
106         if (formObject != null) {
107             return formObject;
108         }
109         return getCurrentFormObject(ScopeType.CONVERSATION);
110     }
111
112     /**
113      * Gets the form object from the context, using the well-known attribute
114      * name {@link #CURRENT_FORM_OBJECT_ATTRIBUTE}.
115      * @param scopeType the scope to obtain the form object from
116      * @return the form object, or null if not found
117      */

118     public Object JavaDoc getCurrentFormObject(ScopeType scopeType) {
119         return getFormObject(getCurrentFormObjectName(), scopeType);
120     }
121
122     /**
123      * Expose given form object using the well known alias
124      * {@link #CURRENT_FORM_OBJECT_ATTRIBUTE} in the specified scope.
125      * @param formObject the form object
126      * @param scopeType the scope in which to expose the form object
127      */

128     public void setCurrentFormObject(Object JavaDoc formObject, ScopeType scopeType) {
129         //don't call setFormObject since that would cause infinite recursion!
130
scopeType.getScope(context).put(getCurrentFormObjectName(), formObject);
131     }
132
133     /**
134      * Gets the form object from the context, using the specified name.
135      * @param formObjectName the name of the form object in the context
136      * @param scopeType the scope to obtain the form object from
137      * @return the form object, or null if not found
138      */

139     public Object JavaDoc getFormObject(String JavaDoc formObjectName, ScopeType scopeType) {
140         return scopeType.getScope(context).get(formObjectName);
141     }
142
143     /**
144      * Gets the form object from the context, using the specified name.
145      * @param formObjectName the name of the form in the context
146      * @param formObjectClass the class of the form object, which will be
147      * verified
148      * @param scopeType the scope to obtain the form object from
149      * @return the form object, or null if not found
150      */

151     public Object JavaDoc getFormObject(String JavaDoc formObjectName, Class JavaDoc formObjectClass, ScopeType scopeType) {
152         return scopeType.getScope(context).get(formObjectName, formObjectClass);
153     }
154
155     /**
156      * Expose given form object using given name in specified scope. Given
157      * object will become the <i>current</i> form object.
158      * @param formObject the form object
159      * @param formObjectName the name of the form object
160      * @param scopeType the scope in which to expose the form object
161      */

162     public void putFormObject(Object JavaDoc formObject, String JavaDoc formObjectName, ScopeType scopeType) {
163         scopeType.getScope(context).put(formObjectName, formObject);
164         setCurrentFormObject(formObject, scopeType);
165     }
166
167     /**
168      * Gets the form object <code>Errors</code> tracker from the context,
169      * using the form object name {@link #CURRENT_FORM_OBJECT_ATTRIBUTE}. This
170      * method will search all scopes.
171      * @return the form object Errors tracker, or null if not found
172      */

173     public Errors getCurrentFormErrors() {
174         Errors errors = getCurrentFormErrors(ScopeType.REQUEST);
175         if (errors != null) {
176             return errors;
177         }
178         errors = getCurrentFormErrors(ScopeType.FLASH);
179         if (errors != null) {
180             return errors;
181         }
182         errors = getCurrentFormErrors(ScopeType.FLOW);
183         if (errors != null) {
184             return errors;
185         }
186         return getCurrentFormErrors(ScopeType.CONVERSATION);
187     }
188
189     /**
190      * Gets the form object <code>Errors</code> tracker from the context,
191      * using the form object name {@link #CURRENT_FORM_OBJECT_ATTRIBUTE}.
192      * @param scopeType the scope to obtain the errors from
193      * @return the form object Errors tracker, or null if not found
194      */

195     public Errors getCurrentFormErrors(ScopeType scopeType) {
196         return getFormErrors(getCurrentFormObjectName(), scopeType);
197     }
198
199     /**
200      * Expose given errors instance using the well known alias
201      * {@link #CURRENT_FORM_OBJECT_ATTRIBUTE} in the specified scope.
202      * @param errors the errors instance
203      * @param scopeType the scope in which to expose the errors instance
204      */

205     public void setCurrentFormErrors(Errors errors, ScopeType scopeType) {
206         scopeType.getScope(context).put(getCurrentFormErrorsName(), errors);
207     }
208
209     /**
210      * Gets the form object <code>Errors</code> tracker from the context,
211      * using the specified form object name.
212      * @param formObjectName the name of the Errors object, which will be
213      * prefixed with {@link BindException#ERROR_KEY_PREFIX}
214      * @param scopeType the scope to obtain the errors from
215      * @return the form object errors instance, or null if not found
216      */

217     public Errors getFormErrors(String JavaDoc formObjectName, ScopeType scopeType) {
218         return (Errors)scopeType.getScope(context).get(ERRORS_PREFIX + formObjectName, Errors.class);
219     }
220
221     /**
222      * Expose given errors instance in the specified scope. Given errors
223      * instance will become the <i>current</i> form errors instance.
224      * @param errors the errors object
225      * @param scopeType the scope to expose the errors in
226      */

227     public void putFormErrors(Errors errors, ScopeType scopeType) {
228         scopeType.getScope(context).put(ERRORS_PREFIX + errors.getObjectName(), errors);
229         setCurrentFormErrors(errors, scopeType);
230     }
231 }
Popular Tags