KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.chain.Command;
21 import org.apache.commons.chain.Context;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.struts.action.ActionErrors;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.config.ActionConfig;
27
28
29 /**
30  * <p>Validate the properties of the form bean for this request. If there are
31  * any validation errors, execute the specified command; otherwise,
32  * proceed normally.</p>
33  *
34  * @author Craig R. McClanahan
35  * @version $Rev: 55324 $ $Date: 2004-10-22 19:55:27 +0100 (Fri, 22 Oct 2004) $
36  */

37
38 public abstract class AbstractValidateActionForm implements Command {
39
40
41     // ------------------------------------------------------ Instance Variables
42

43
44     private String JavaDoc actionConfigKey = Constants.ACTION_CONFIG_KEY;
45     private String JavaDoc actionFormKey = Constants.ACTION_FORM_KEY;
46     private String JavaDoc cancelKey = Constants.CANCEL_KEY;
47     private String JavaDoc validKey = Constants.VALID_KEY;
48
49     private static final Log log =
50         LogFactory.getLog(AbstractValidateActionForm.class);
51
52
53     // -------------------------------------------------------------- Properties
54

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

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

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

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

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

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

125     public void setCancelKey(String JavaDoc cancelKey) {
126
127         this.cancelKey = cancelKey;
128
129     }
130
131
132     /**
133      * <p>Return the context attribute key under which the
134      * validity flag for this request is stored.</p>
135      */

136     public String JavaDoc getValidKey() {
137
138         return (this.validKey);
139
140     }
141
142
143     /**
144      * <p>Set the context attribute key under which the
145      * validity flag for this request is stored.</p>
146      *
147      * @param validKey The new context attribute key
148      */

149     public void setValidKey(String JavaDoc validKey) {
150
151         this.validKey = validKey;
152
153     }
154
155
156     // ---------------------------------------------------------- Public Methods
157

158
159     /**
160      * <p>Validate the properties of the form bean for this request. If
161      * there are any validation errors, execute the child commands in our
162      * chain; otherwise, proceed normally.</p>
163      *
164      * @param context The <code>Context</code> for the current request
165      *
166      * @return <code>false</code> so that processing continues, if there are
167      * no validation errors; otherwise <code>true</code>
168      */

169     public boolean execute(Context context) throws Exception JavaDoc {
170
171         // Is there a form bean for this request?
172
ActionForm actionForm = (ActionForm)
173             context.get(getActionFormKey());
174         if (actionForm == null) {
175             context.put(getValidKey(), Boolean.TRUE);
176             return (false);
177         }
178
179         // Was this request cancelled?
180
Boolean JavaDoc cancel = (Boolean JavaDoc)
181             context.get(getCancelKey());
182         if ((cancel != null) && cancel.booleanValue()) {
183             context.put(getValidKey(), Boolean.TRUE);
184             return (false);
185         }
186
187         // Is validation disabled on this request?
188
ActionConfig actionConfig = (ActionConfig)
189             context.get(getActionConfigKey());
190         if (!actionConfig.getValidate()) {
191             context.put(getValidKey(), Boolean.TRUE);
192             return (false);
193         }
194
195         // Call the validate() method of this form bean
196
ActionErrors errors = validate(context, actionConfig, actionForm);
197
198         // If there were no errors, proceed normally
199
if ((errors == null) || (errors.isEmpty())) {
200             context.put(getValidKey(), Boolean.TRUE);
201             return (false);
202         }
203
204         // Flag the validation failure and proceed
205
context.put(getValidKey(), Boolean.FALSE);
206         return (false);
207
208     }
209
210
211     // ------------------------------------------------------- Protected Methods
212

213
214     /**
215      * <p>Call the <code>validate()</code> method of the specified form bean,
216      * and return the resulting <code>ActionErrors</code> object.</p>
217      *
218      * @param context The context for this request
219      * @param actionConfig The <code>ActionConfig</code> for this request
220      * @param actionForm The form bean for this request
221      */

222     protected abstract ActionErrors validate(Context context,
223                                              ActionConfig actionConfig,
224                                              ActionForm actionForm);
225
226
227 }
228
Popular Tags