KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > DefaultFormElement


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultFormElement.java,v 1.21 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.enhydra.barracuda.plankton.data.DefaultStateMap;
27 import org.enhydra.barracuda.plankton.data.StateMap;
28 import org.apache.log4j.*;
29 import java.math.BigDecimal JavaDoc;
30
31 /**
32  * <p>A FormElement defines how an element in a FormMap should
33  * be mapped to a first class java object. There are several
34  * key pieces of information required:
35  *
36  * <ul>
37  * <li>key - used to retrieve the target value from the incoming
38  * data source (either a ServletRequest or a StateMap)</li>
39  * <li>name - a displayable name for this element (defaults to key)</li>
40  * <li>type - the type of data object the incoming value should be
41  * mapped to, as defined by the FormType class (String, Boolean,
42  * Integer, Date, Long, Short, Double, Float)</li>
43  * <li>default val - the default val to be used if the key value is
44  * null in the incoming data source (may be null)</li>
45  * <li>validator - any validators associated with this particular
46  * form elmenent (may be null)</li>
47  * <li>allow multiples - the servlet interface allows multiple params
48  * with the same key. This value indicates whether or not that is
49  * acceptable for a key. If so, then when multiple values are
50  * encountered they element will actually contain a List of values
51  * of the proper type</li>
52  * </ul>
53  *
54  * <p>Once we have this information, we basically have enough data to map
55  * a String value in the data source to a first class Java object in the
56  * element.
57  *
58  * <p>Note that the element keeps track of the original value as well, so
59  * that if for some reason you ever need to get to it that information is
60  * available.
61  *
62  * <p>Also not that we provide convenience methods which automatically cast
63  * for you (Note however: this type of thing is not type safe, meaning that
64  * you can get a ClassCastException if you do a getXXXVal() when the
65  * underlying object is not of type XXX.
66  */

67 public class DefaultFormElement implements FormElement {
68
69     //public constants
70
protected static final Logger logger = Logger.getLogger(DefaultFormElement.class.getName());
71
72     //non-public vars
73
protected String JavaDoc key = null;
74     protected String JavaDoc name = null;
75     protected FormType type = null;
76     protected Object JavaDoc defaultVal = null;
77     protected ParseException pe = null;
78     protected FormValidator validator = null;
79     protected boolean allowMultiples = false;
80     protected Object JavaDoc origVal = null;
81     protected Object JavaDoc val = null;
82
83     /**
84      * Public noargs constructor. Form key defaults to null.
85      */

86     public DefaultFormElement() {
87         this(null);
88     }
89     
90     /**
91      * Public constructor. Form type defaults to FormType.STRING
92      *
93      * @param ikey the key name in the data source
94      */

95     public DefaultFormElement(String JavaDoc ikey) {
96         this(ikey, FormType.STRING);
97     }
98
99     /**
100      * Public constructor. Default value defaults to Null.
101      *
102      * @param ikey the key name in the data source
103      * @param itype the FormType for the element
104      */

105     public DefaultFormElement(String JavaDoc ikey, FormType itype) {
106         this(ikey, itype, null);
107     }
108
109     /**
110      * Public constructor. Validator defaults to null.
111      *
112      * @param ikey the key name in the data source
113      * @param itype the FormType for the element
114      * @param idefaultVal the default value to be used if the key is
115      * not found in the data source or the value for the key is null
116      */

117     public DefaultFormElement(String JavaDoc ikey, FormType itype, Object JavaDoc idefaultVal) {
118         this(ikey, itype, idefaultVal, null);
119     }
120
121     /**
122      * Public constructor. AllowMultiples defaults to false.
123      *
124      * @param ikey the key name in the data source
125      * @param itype the FormType for the element
126      * @param idefaultVal the default value to be used if the key is
127      * not found in the data source or the value for the key is null
128      * @param ivalidator the FormValidator associated with this element
129      */

130     public DefaultFormElement(String JavaDoc ikey, FormType itype, Object JavaDoc idefaultVal, FormValidator ivalidator) {
131         this(ikey, itype, idefaultVal, ivalidator, false);
132     }
133     
134     /**
135      * Public constructor, Name defaults to Key
136      *
137      * @param ikey the key name in the data source
138      * @param itype the FormType for the element
139      * @param idefaultVal the default value to be used if the key is
140      * not found in the data source or the value for the key is null
141      * @param ivalidator the FormValidator associated with this element
142      * @param iallowMultiples true if there may be multiple values in the datasource
143      * for this particular key name
144      */

145     public DefaultFormElement(String JavaDoc ikey, FormType itype, Object JavaDoc idefaultVal, FormValidator ivalidator, boolean iallowMultiples) {
146 //csc_100901.3 this(ikey, ikey, itype, idefaultVal, ivalidator, false);
147
this(ikey, ikey, itype, idefaultVal, ivalidator, iallowMultiples);
148     }
149
150     /**
151      * Public constructor
152      *
153      * @param ikey the key name in the data source
154      * @param iname the name for the element
155      * @param itype the FormType for the element
156      * @param idefaultVal the default value to be used if the key is
157      * not found in the data source or the value for the key is null
158      * @param ivalidator the FormValidator associated with this element
159      * @param iallowMultiples true if there may be multiple values in the datasource
160      * for this particular key name
161      */

162     public DefaultFormElement(String JavaDoc ikey, String JavaDoc iname, FormType itype, Object JavaDoc idefaultVal, FormValidator ivalidator, boolean iallowMultiples) {
163         setKey(ikey);
164         setName(iname);
165         setType(itype);
166         setDefaultVal(idefaultVal);
167         setValidator(ivalidator);
168         setAllowMultiples(iallowMultiples);
169     }
170
171     /**
172      * Set the key value for this form element
173      *
174      * @param ikey the key value for this form element
175      */

176     public void setKey(String JavaDoc ikey) {
177         key = ikey;
178     }
179     
180     /**
181      * Get the key value for this form element
182      *
183      * @return the key for this form element
184      */

185     public String JavaDoc getKey() {
186         return key;
187     }
188
189     /**
190      * Set the name of this form element
191      *
192      * @param iname the name of this form element
193      */

194     public void setName(String JavaDoc iname) {
195         name = iname;
196     }
197     
198     /**
199      * Get the name of this form element
200      *
201      * @return the name of this form element
202      */

203     public String JavaDoc getName() {
204         return name;
205     }
206
207     /**
208      * Set the FormType for this form element
209      *
210      * @param itype the FormType for this form element
211      */

212     public void setType(FormType itype) {
213         if (itype==null) itype = FormType.STRING;
214         type = itype;
215     }
216     
217     /**
218      * Get the FormType for this for element
219      *
220      * @return the FormType for this form element
221      */

222     public FormType getType() {
223         return type;
224     }
225
226     /**
227      * Set the default value for this form element
228      *
229      * @param idefaultVal the FormType for this form element
230      */

231     public void setDefaultVal(Object JavaDoc idefaultVal) {
232         defaultVal = idefaultVal;
233     }
234     
235     /**
236      * Get the default value for this form element
237      *
238      * @return the default value for this form element (may be null)
239      */

240     public Object JavaDoc getDefaultVal() {
241         return defaultVal;
242     }
243
244     /**
245      * Set whether or not this element allows multiple values
246      *
247      * @param val true if the element allows multiples
248      */

249     public void setAllowMultiples(boolean val) {
250         allowMultiples = val;
251     }
252     
253     /**
254      * Does this element allow multiple values
255      *
256      * @return true if this element allows multiple values
257      */

258     public boolean allowMultiples() {
259         return allowMultiples;
260     }
261
262     /**
263      * Set the FormValidator for this form element
264      *
265      * @param ivalidator the FormValidator for this form element
266      */

267     public void setValidator(FormValidator ivalidator) {
268         validator = ivalidator;
269     }
270     
271     /**
272      * Get the default FormValidator for this form element
273      *
274      * @return the validator for this form element (may be null)
275      */

276     public FormValidator getValidator() {
277         return validator;
278     }
279
280     /**
281      * Set the original value for this element
282      *
283      * @param iorigVal the original value
284      */

285     public void setOrigVal(Object JavaDoc iorigVal) {
286         origVal = iorigVal;
287     }
288
289     /**
290      * Get the original value for this element
291      *
292      * @return the original value for this form element (may be null)
293      */

294     public Object JavaDoc getOrigVal() {
295         return origVal;
296     }
297
298     /**
299      * Set the value for this element
300      *
301      * @param ival the value for this element
302      */

303     public void setVal(Object JavaDoc ival) {
304         val = ival;
305     }
306
307     /**
308      * Get the value for this element. If the underlying object
309      * is actually an array (ie. allowMultiples = true), then
310      * you should really be calling getVals() to get the whole
311      * object array; if you call this method, you will just get
312      * the first element of the array.
313      *
314      * @return the value for this form element (may be null)
315      */

316     public Object JavaDoc getVal() {
317 // return val;
318
if (val!=null && val instanceof Object JavaDoc[]) {
319             return ((Object JavaDoc[]) val)[0];
320         }
321         return val;
322     }
323     
324     /**
325      * Get all the values for this element. There will always be an array returned.
326      * if the value for this FormElement is null, an empty array is returned. This
327      * method only really makes sense if allowMultiples = true
328      *
329      * @return the array of values for this form element
330      */

331     public Object JavaDoc[] getVals() {
332         Object JavaDoc valCopy = val;
333         if (valCopy==null) {
334             if (type!=null) {
335                 return type.getTypeArray(0);
336             }
337             return new Object JavaDoc [] {};
338         }
339         if (valCopy instanceof ArrayList) {
340             valCopy = ((ArrayList)valCopy).toArray();
341         } else {
342             valCopy = new Object JavaDoc [] {valCopy};
343         }
344         Object JavaDoc [] vals = (Object JavaDoc [])valCopy;
345         Object JavaDoc [] typedVals = null;
346         if (type!=null) {
347             typedVals = type.getTypeArray(vals.length);
348         }
349         if (typedVals==null) {
350             typedVals = new Object JavaDoc [vals.length];
351         }
352         if (logger.isDebugEnabled()) {
353             for(int i = 0; i < vals.length; i++) {
354                 logger.debug("Val: " + vals[i]);
355                 if (vals[i]!=null) {
356                     logger.debug("ValType: " + vals[i].getClass());
357                 }
358             }
359         }
360         System.arraycopy(vals, 0, typedVals, 0, vals.length);
361         return typedVals;
362     }
363
364     /**
365      * Get the value for this element as a String
366      */

367     public String JavaDoc getStringVal() {
368         return (String JavaDoc) val;
369     }
370
371     /**
372      * Get the value for this element as a String, defaulting
373      * accordingly if the value is null
374      */

375     public String JavaDoc getStringVal(String JavaDoc dflt) {
376         return (val!=null ? (String JavaDoc) val : dflt);
377     }
378
379     /**
380      * Get the value for this element as a Boolean
381      */

382     public Boolean JavaDoc getBooleanVal() {
383         return (Boolean JavaDoc) val;
384     }
385
386     /**
387      * Get a Boolean value from the map, defaulting accordingly if
388      * the value is null
389      */

390     public Boolean JavaDoc getBooleanVal(Boolean JavaDoc dflt) {
391         return (val!=null ? (Boolean JavaDoc) val : dflt);
392     }
393
394     /**
395      * Get the value for this element as a Integer
396      */

397     public Integer JavaDoc getIntegerVal() {
398         return (Integer JavaDoc) val;
399     }
400
401     /**
402      * Get a Integer value from the map, defaulting accordingly if
403      * the value is null
404      */

405     public Integer JavaDoc getIntegerVal(Integer JavaDoc dflt) {
406         return (val!=null ? (Integer JavaDoc) val : dflt);
407     }
408
409     /**
410      * Get the value for this element as a Date
411      */

412     public Date getDateVal() {
413         return (Date) val;
414     }
415
416     /**
417      * Get a Date value from the map, defaulting accordingly if
418      * the value is null
419      */

420     public Date getDateVal(Date dflt) {
421         return (val!=null ? (Date) val : dflt);
422     }
423
424     /**
425      * Get the value for this element as a Long
426      */

427     public Long JavaDoc getLongVal() {
428         return (Long JavaDoc) val;
429     }
430
431     /**
432      * Get a Long value from the map, defaulting accordingly if
433      * the value is null
434      */

435     public Long JavaDoc getLongVal(Long JavaDoc dflt) {
436         return (val!=null ? (Long JavaDoc) val : dflt);
437     }
438
439     /**
440      * Get the value for this element as a Short
441      */

442     public Short JavaDoc getShortVal() {
443         return (Short JavaDoc) val;
444     }
445
446     /**
447      * Get a Short value from the map, defaulting accordingly if
448      * the value is null
449      */

450     public Short JavaDoc getShortVal(Short JavaDoc dflt) {
451         return (val!=null ? (Short JavaDoc) val : dflt);
452     }
453
454     /**
455      * Get the value for this element as a Double
456      */

457     public Double JavaDoc getDoubleVal() {
458         return (Double JavaDoc) val;
459     }
460
461     /**
462      * Get a Double value from the map, defaulting accordingly if
463      * the value is null
464      */

465     public Double JavaDoc getDoubleVal(Double JavaDoc dflt) {
466         return (val!=null ? (Double JavaDoc) val : dflt);
467     }
468
469     /**
470      * Get the value for this element as a Float
471      */

472     public Float JavaDoc getFloatVal() {
473         return (Float JavaDoc) val;
474     }
475
476     /**
477      * Get a Float value from the map, defaulting accordingly if
478      * the value is null
479      */

480     public Float JavaDoc getFloatVal(Float JavaDoc dflt) {
481         return (val!=null ? (Float JavaDoc) val : dflt);
482     }
483
484     /**
485      * Set the parse exception associated with this element
486      *
487      * @return any parse exceptions associated with the element
488      */

489     public void setParseException(ParseException ipe) {
490         pe = ipe;
491     }
492
493     /**
494      * Get any parse exceptions associated with the element (ie. that
495      * might have occurred when the element was mapped)
496      *
497      * @return any parse exceptions associated with the element
498      */

499     public ParseException getParseException() {
500         return pe;
501     }
502     
503     /**
504      * Get a string representation of this element
505      *
506      * @return the String describing this element
507      */

508     public String JavaDoc toString() {
509         return "Key:"+key+" Name:"+name+" Val:"+val+" Type:"+(type!=null ? type.getFormClass().getName() : "null")+" Orig:"+origVal+" Dflt:"+defaultVal+" Mult:"+allowMultiples+" Validator:"+(validator!=null ? "@"+Integer.toHexString(validator.hashCode()) : "null");
510     }
511
512 }
513
Popular Tags