KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > AbstractPopulateActionForm


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
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
17 package org.apache.struts.chain;
18
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.apache.commons.chain.Command;
25 import org.apache.commons.chain.Context;
26 import org.apache.commons.chain.web.WebContext;
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.config.ActionConfig;
30
31
32 /**
33  * <p>Populate the form bean (if any) for this request.</p>
34  *
35  * @author Craig R. McClanahan
36  * @version $Rev: 55324 $ $Date: 2004-10-22 19:55:27 +0100 (Fri, 22 Oct 2004) $
37  */

38
39 public abstract class AbstractPopulateActionForm implements Command {
40
41
42     // ------------------------------------------------------ Instance Variables
43

44
45     private String JavaDoc actionConfigKey = Constants.ACTION_CONFIG_KEY;
46     private String JavaDoc actionFormKey = Constants.ACTION_FORM_KEY;
47     private String JavaDoc cancelKey = Constants.CANCEL_KEY;
48
49
50     // -------------------------------------------------------------- Properties
51

52
53     /**
54      * <p>Return the context attribute key under which the
55      * <code>ActionConfig</code> for the currently selected application
56      * action is stored.</p>
57      */

58     public String JavaDoc getActionConfigKey() {
59
60         return (this.actionConfigKey);
61
62     }
63
64
65     /**
66      * <p>Set the context attribute key under which the
67      * <code>ActionConfig</code> for the currently selected application
68      * action is stored.</p>
69      *
70      * @param actionConfigKey The new context attribute key
71      */

72     public void setActionConfigKey(String JavaDoc actionConfigKey) {
73
74         this.actionConfigKey = actionConfigKey;
75
76     }
77
78
79     /**
80      * <p>Return the context attribute key under which the
81      * <code>ActionForm</code> for the currently selected application
82      * action is stored.</p>
83      */

84     public String JavaDoc getActionFormKey() {
85
86         return (this.actionFormKey);
87
88     }
89
90
91     /**
92      * <p>Set the context attribute key under which the
93      * <code>ActionForm</code> for the currently selected application
94      * action is stored.</p>
95      *
96      * @param actionFormKey The new context attribute key
97      */

98     public void setActionFormKey(String JavaDoc actionFormKey) {
99
100         this.actionFormKey = actionFormKey;
101
102     }
103
104
105     /**
106      * <p>Return the context attribute key under which the
107      * cancellation flag for this request is stored.</p>
108      */

109     public String JavaDoc getCancelKey() {
110
111         return (this.cancelKey);
112
113     }
114
115
116     /**
117      * <p>Set the context attribute key under which the
118      * cancellation flag for this request is stored.</p>
119      *
120      * @param cancelKey The new context attribute key
121      */

122     public void setCancelKey(String JavaDoc cancelKey) {
123
124         this.cancelKey = cancelKey;
125
126     }
127
128
129     // ---------------------------------------------------------- Public Methods
130

131
132     /**
133      * <p>Populate the form bean (if any) for this request.</p>
134      *
135      * @param context The <code>Context</code> for the current request
136      *
137      * @return <code>false</code> so that processing continues
138      */

139     public boolean execute(Context context) throws Exception JavaDoc {
140
141         // Is there a form bean for this request?
142
ActionForm actionForm = (ActionForm)
143             context.get(getActionFormKey());
144         if (actionForm == null) {
145             return (false);
146         }
147
148         // Reset the form bean property values
149
ActionConfig actionConfig = (ActionConfig)
150             context.get(getActionConfigKey());
151
152
153         reset(context, actionConfig, actionForm);
154
155         populate(context, actionConfig, actionForm);
156
157         handleCancel(context, actionConfig, actionForm);
158
159         return (false);
160
161     }
162
163
164     // ------------------------------------------------------- Protected Methods
165

166     /**
167      * <p>Call the <code>reset()</code> method on the specified form bean.</p>
168      *
169      * @param context The context for this reqest
170      * @param actionConfig The actionConfig for this request
171      * @param actionForm The form bean for this request
172      */

173     protected abstract void reset(Context context,
174                                   ActionConfig actionConfig,
175                                   ActionForm actionForm);
176
177
178     /**
179      * <p>Base implementation assumes that the <code>Context</code>
180      * can be cast to <code>WebContext</code> and copies the parameter
181      * values from the context to the <code>ActionForm</code>.</p>
182      *
183      * <p>Note that this implementation does not handle "file uploads"
184      * because as far as I know there is no API for handling that without
185      * committing to servlets -- in a servlet environment, use
186      * <code>org.apache.struts.chain.servlet.PopulateActionForm</code>.</p>
187      *
188      * @param context
189      * @param actionConfig
190      * @param actionForm
191      * @throws Exception
192      */

193     protected void populate(Context context,
194                             ActionConfig actionConfig,
195                             ActionForm actionForm) throws Exception JavaDoc
196     {
197         WebContext wcontext = (WebContext) context;
198         Map JavaDoc paramValues = wcontext.getParamValues();
199         Map JavaDoc parameters = new HashMap JavaDoc();
200
201         String JavaDoc prefix = actionConfig.getPrefix();
202         String JavaDoc suffix = actionConfig.getSuffix();
203
204         Iterator JavaDoc keys = paramValues.keySet().iterator();
205         while (keys.hasNext()) {
206             String JavaDoc name = (String JavaDoc) keys.next();
207             String JavaDoc stripped = name;
208             if (prefix != null) {
209                 if (!stripped.startsWith(prefix)) {
210                     continue;
211                 }
212                 stripped = stripped.substring(prefix.length());
213             }
214             if (suffix != null) {
215                 if (!stripped.endsWith(suffix)) {
216                     continue;
217                 }
218                 stripped =
219                         stripped.substring(0, stripped.length() - suffix.length());
220             }
221             parameters.put(stripped, paramValues.get(name));
222         }
223         BeanUtils.populate(actionForm, parameters);
224     }
225
226
227     protected void handleCancel(Context context,
228                                 ActionConfig actionConfig,
229                                 ActionForm actionForm) throws Exception JavaDoc
230     {
231         WebContext wcontext = (WebContext) context;
232         Map JavaDoc paramValues = wcontext.getParamValues();
233
234         // Set the cancellation attribute if appropriate
235
if ((paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY) != null) ||
236             (paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY_X) != null)) {
237             context.put(getCancelKey(), Boolean.TRUE);
238             wcontext.getRequestScope().put(Globals.CANCEL_KEY, Boolean.TRUE);
239         } else {
240             context.put(getCancelKey(), Boolean.FALSE);
241         }
242     }
243 }
244
Popular Tags