KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > config > Field


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/Field.java,v 1.34 2004/12/20 08:24:56 hkollmann Exp $
3  * $Revision: 1.34 $
4  * $Date: 2004/12/20 08:24:56 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.config;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.util.IEscaper;
30 import org.dbforms.util.MessageResourcesInternal;
31 import org.dbforms.util.ReflectionUtil;
32 import org.dbforms.util.StringUtil;
33 import org.dbforms.util.Util;
34
35 import java.text.DateFormat JavaDoc;
36 import java.text.Format JavaDoc;
37
38 import java.util.Locale JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import java.io.Serializable JavaDoc;
42
43 /**
44  * This class represents a field tag in dbforms-config.xml.
45  *
46  * @author foxat
47  */

48 public class Field implements Serializable JavaDoc {
49     private static Log logCat = LogFactory.getLog(Field.class.getName());
50
51     private IEscaper escaper = null;
52
53     /**
54      * used only for DISKBLOB: holds the directory uploaded files should be
55      * stored to
56      */

57     private String JavaDoc directory = null;
58
59     private String JavaDoc escaperClass = null;
60
61     private String JavaDoc expression = null;
62
63     private String JavaDoc fieldType = null;
64
65     /** stores if the field is a KEY */
66     private String JavaDoc key = "false";
67
68     /** the field-name, as provided in xml-config file */
69     private String JavaDoc name;
70
71     private Table table = null;
72
73     /**
74      * used only for DISKBLOB: if "true" -> then files will be renamed to ensure
75      * that every file is unique and no file overwrites another. default is
76      * "false" (keep original names)
77      */

78     private boolean encoded = false;
79
80     /** stores if the field is AUTOINCremental */
81     private boolean isAutoInc = false;
82
83     /** stores if the field is sortable */
84     private boolean isSortable = false;
85
86     /** the id of this field (for dbforms-internal use) */
87     private int id;
88
89     /** the field-size */
90     private int size = -1;
91
92     /** integer representation of the "fieldType"-value */
93     private int type;
94
95     /**
96      * Sets the autoInc attribute of the Field object
97      *
98      * @param autoInc
99      * The new autoInc value
100      */

101     public void setAutoInc(String JavaDoc autoInc) {
102         this.isAutoInc = Util.getTrue(autoInc);
103     }
104
105     /**
106      * Sets the directory attribute of the Field object
107      *
108      * @param directory
109      * The new directory value
110      */

111     public void setDirectory(String JavaDoc directory) {
112         this.directory = directory;
113     }
114
115     /**
116      * Gets the directory attribute of the Field object
117      *
118      * @return The directory value
119      */

120     public String JavaDoc getDirectory() {
121         return directory;
122     }
123
124     /**
125      * Sets the encoding attribute of the Field object
126      *
127      * @param encoding
128      * The new encoding value
129      */

130     public void setEncoding(String JavaDoc encoding) {
131         this.encoded = Util.getTrue(encoding);
132     }
133
134     /**
135      * DOCUMENT ME!
136      *
137      * @return DOCUMENT ME!
138      */

139     public IEscaper getEscaper() {
140         if (escaper == null) {
141             String JavaDoc s = getEscaperClass();
142
143             if (!Util.isNull(s)) {
144                 try {
145                     escaper = (IEscaper) ReflectionUtil.newInstance(s);
146                 } catch (Exception JavaDoc e) {
147                     logCat
148                             .error("cannot create the new escaper [" + s + "]",
149                                     e);
150                 }
151             }
152
153             if (escaper == null) {
154                 if (getTable() == null) {
155                     try {
156                         escaper = DbFormsConfigRegistry.instance().lookup()
157                                 .getEscaper();
158                     } catch (Exception JavaDoc e) {
159                         logCat
160                                 .error("cannot create the new default escaper",
161                                         e);
162                     }
163                 } else {
164                     escaper = getTable().getEscaper();
165                 }
166             }
167         }
168
169         return escaper;
170     }
171
172     /**
173      * DOCUMENT ME!
174      *
175      * @param string
176      */

177     public void setEscaperClass(String JavaDoc string) {
178         escaperClass = string;
179     }
180
181     /**
182      * Sets the expression attribute of the Field object
183      *
184      * @param expression
185      * The new expression value
186      */

187     public void setExpression(String JavaDoc expression) {
188         this.expression = expression;
189     }
190
191     /**
192      * Gets the expression attribute of the Field object
193      *
194      * @return The expression value
195      */

196     public String JavaDoc getExpression() {
197         return expression;
198     }
199
200     /**
201      * DOCUMENT ME!
202      *
203      * @param core
204      * DOCUMENT ME!
205      *
206      * @return DOCUMENT ME!
207      */

208     public String JavaDoc getFieldName(String JavaDoc core) {
209         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(core);
210         sb.append(getTable().getId());
211         sb.append("_");
212         sb.append(getId());
213
214         return sb.toString();
215     }
216
217     /**
218      * Maps the field type description to internal value. <br>
219      * We need this information in oder to call the appropriate
220      * PreparedStatement.setXxx(..) methods <br>
221      * this method is called by the digester framework to set the fieldType!
222      *
223      * @param aFieldType
224      * the type string value (example: "int", "char", "numeric", etc)
225      */

226     public void setFieldType(String JavaDoc aFieldType) {
227         this.fieldType = aFieldType.toLowerCase();
228
229         if (fieldType.startsWith("char") || fieldType.startsWith("varchar")
230                 || fieldType.startsWith("nvarchar")
231                 || fieldType.startsWith("longchar")
232                 || fieldType.startsWith("long varchar")
233                 || fieldType.startsWith("text")) {
234             type = FieldTypes.CHAR;
235         } else if (fieldType.startsWith("int")
236                 || fieldType.startsWith("smallint")
237                 || fieldType.startsWith("long")
238                 || fieldType.startsWith("tinyint")) {
239             type = FieldTypes.INTEGER;
240         } else if (fieldType.startsWith("numeric")
241                 || fieldType.startsWith("number")
242                 || fieldType.startsWith("decimal")) {
243             type = FieldTypes.NUMERIC;
244         } else if (fieldType.startsWith("date")) {
245             type = FieldTypes.DATE;
246         } else if (fieldType.startsWith("timestamp")) {
247             type = FieldTypes.TIMESTAMP;
248         } else if (fieldType.startsWith("time")) {
249             type = FieldTypes.TIME;
250         } else if (fieldType.startsWith("double")
251                 || fieldType.startsWith("float")) {
252             type = FieldTypes.DOUBLE;
253         } else if (fieldType.startsWith("real")) {
254             type = FieldTypes.FLOAT;
255         } else if (fieldType.startsWith("blob")
256                 || fieldType.startsWith("image")) {
257             type = FieldTypes.BLOB;
258         } else if (fieldType.startsWith("diskblob")) {
259             type = FieldTypes.DISKBLOB;
260         } else if (fieldType.startsWith("bool")) {
261             type = FieldTypes.BOOLEAN;
262         }
263     }
264
265     /**
266      * DOCUMENT ME!
267      *
268      * @return
269      */

270     public String JavaDoc getFieldType() {
271         return fieldType;
272     }
273
274     /**
275      * DOCUMENT ME!
276      *
277      * @param pattern
278      * DOCUMENT ME!
279      * @param locale
280      * DOCUMENT ME!
281      *
282      * @return DOCUMENT ME!
283      */

284     public Format JavaDoc getFormat(String JavaDoc pattern, Locale JavaDoc locale) {
285         switch (getType()) {
286         case FieldTypes.INTEGER:
287         case FieldTypes.NUMERIC:
288         case FieldTypes.DOUBLE:
289         case FieldTypes.FLOAT:
290         case FieldTypes.DATE:
291         case FieldTypes.TIME:
292         case FieldTypes.TIMESTAMP:
293             break;
294
295         default:
296             return null;
297         }
298
299         Format JavaDoc res = null;
300         int dateStyle = Constants.DATE_STYLE_DEFAULT;
301         int timeStyle = Constants.TIME_STYLE_DEFAULT;
302
303         if (Util.isNull(pattern)) {
304             pattern = MessageResourcesInternal.getMessage("dbforms.pattern."
305                     + getFieldType(), locale);
306         }
307
308         if (!Util.isNull(pattern)) {
309             if ("short".startsWith(pattern.toLowerCase())) {
310                 dateStyle = DateFormat.SHORT;
311                 pattern = MessageResourcesInternal.getMessage(
312                         "dbforms.pattern." + getFieldType() + "." + pattern,
313                         locale);
314             } else if ("medium".startsWith(pattern.toLowerCase())) {
315                 dateStyle = DateFormat.MEDIUM;
316                 pattern = MessageResourcesInternal.getMessage(
317                         "dbforms.pattern." + getFieldType() + "." + pattern,
318                         locale);
319             } else if ("long".startsWith(pattern.toLowerCase())) {
320                 dateStyle = DateFormat.LONG;
321                 pattern = MessageResourcesInternal.getMessage(
322                         "dbforms.pattern." + getFieldType() + "." + pattern,
323                         locale);
324             } else if ("full".startsWith(pattern.toLowerCase())) {
325                 dateStyle = DateFormat.FULL;
326                 pattern = MessageResourcesInternal.getMessage(
327                         "dbforms.pattern." + getFieldType() + "." + pattern,
328                         locale);
329             }
330         }
331
332         switch (getType()) {
333         case FieldTypes.INTEGER:
334
335             try {
336                 res = java.text.NumberFormat.getIntegerInstance(locale);
337             } catch (NoSuchMethodError JavaDoc nsme) {
338                 res = java.text.NumberFormat.getNumberInstance(locale);
339             }
340
341             ((java.text.DecimalFormat JavaDoc) res).setParseIntegerOnly(true);
342
343             if (!Util.isNull(pattern)) {
344                 try {
345                     ((java.text.DecimalFormat JavaDoc) res).applyPattern(pattern);
346                 } catch (Exception JavaDoc e) {
347                     logCat.error(pattern, e);
348                 }
349             }
350
351             break;
352
353         case FieldTypes.NUMERIC:
354         case FieldTypes.DOUBLE:
355         case FieldTypes.FLOAT:
356             res = java.text.NumberFormat.getNumberInstance(locale);
357             if (!Util.isNull(pattern)) {
358                 try {
359                     ((java.text.DecimalFormat JavaDoc) res).applyPattern(pattern);
360                 } catch (Exception JavaDoc e) {
361                     logCat.error(pattern, e);
362                 }
363             }
364             break;
365
366         case FieldTypes.DATE:
367             res = java.text.DateFormat.getDateInstance(dateStyle, locale);
368             if (!Util.isNull(pattern)) {
369                 try {
370                     ((java.text.SimpleDateFormat JavaDoc) res).applyPattern(pattern);
371                 } catch (Exception JavaDoc e) {
372                     logCat.error(pattern, e);
373                 }
374             }
375
376             break;
377
378         case FieldTypes.TIME:
379             res = java.text.DateFormat.getTimeInstance(timeStyle, locale);
380
381             if (!Util.isNull(pattern)) {
382                 try {
383                     ((java.text.SimpleDateFormat JavaDoc) res).applyPattern(pattern);
384                 } catch (Exception JavaDoc e) {
385                     logCat.error(pattern, e);
386                 }
387             }
388
389             break;
390
391         case FieldTypes.TIMESTAMP:
392             res = java.text.DateFormat.getDateTimeInstance(dateStyle,
393                     timeStyle, locale);
394
395             if (!Util.isNull(pattern)) {
396                 try {
397                     ((java.text.SimpleDateFormat JavaDoc) res).applyPattern(pattern);
398                 } catch (Exception JavaDoc e) {
399                     logCat.error(pattern, e);
400                 }
401             }
402
403             break;
404
405         default:
406             break;
407         }
408
409         return res;
410     }
411
412     /**
413      * sets the id of this field-object (this method is called by Table on
414      * init).
415      *
416      * @param id
417      * The new id value
418      */

419     public void setId(int id) {
420         this.id = id;
421     }
422
423     /**
424      * Gets the id attribute of the Field object
425      *
426      * @return The id value
427      */

428     public int getId() {
429         return id;
430     }
431
432     /**
433      * Sets the isKey attribute of the Field object
434      *
435      * @param value
436      * The new isKey value
437      */

438     public void setIsKey(String JavaDoc value) {
439         this.key = value;
440     }
441
442     /**
443      * Sets the name attribute of the Field object
444      *
445      * @param name
446      * The new name value
447      */

448     public void setName(String JavaDoc name) {
449         this.name = name;
450     }
451
452     /**
453      * Gets the name attribute of the Field object
454      *
455      * @return The name value
456      */

457     public String JavaDoc getName() {
458         return name;
459     }
460
461     /**
462      * Sets the sortable attribute of the Field object
463      *
464      * @param sortable
465      * The new sortable value
466      */

467     public void setSortable(String JavaDoc sortable) {
468         this.isSortable = Util.getTrue(sortable);
469     }
470
471     /**
472      * Gets the type attribute of the Field object as numeric value. <br>
473      * It's read only because the field type is set by the digester during
474      * initialize!
475      *
476      * @return The type value
477      */

478     public int getType() {
479         return type;
480     }
481
482     /**
483      * DOCUMENT ME!
484      *
485      * @param obj
486      * DOCUMENT ME!
487      */

488     public void setTypeByObject(Object JavaDoc obj) {
489         if (obj == null) {
490             return;
491         }
492
493         Class JavaDoc clazz = obj.getClass();
494         Vector JavaDoc v = StringUtil.splitString(clazz.getName().toLowerCase(), ".");
495         fieldType = (String JavaDoc) v.lastElement();
496
497         if (clazz.isAssignableFrom(java.lang.Integer JavaDoc.class)) {
498             type = FieldTypes.INTEGER;
499         } else if (clazz.isAssignableFrom(java.lang.Long JavaDoc.class)) {
500             type = FieldTypes.INTEGER;
501         } else if (clazz.isAssignableFrom(java.lang.String JavaDoc.class)) {
502             type = FieldTypes.CHAR;
503         } else if (clazz.isAssignableFrom(java.math.BigDecimal JavaDoc.class)) {
504             type = FieldTypes.NUMERIC;
505         } else if (clazz.isAssignableFrom(java.sql.Date JavaDoc.class)) {
506             type = FieldTypes.DATE;
507         } else if (clazz.isAssignableFrom(java.sql.Time JavaDoc.class)) {
508             type = FieldTypes.TIME;
509         } else if (clazz.isAssignableFrom(java.sql.Timestamp JavaDoc.class)) {
510             type = FieldTypes.TIMESTAMP;
511         } else if (clazz.isAssignableFrom(java.lang.Double JavaDoc.class)) {
512             type = FieldTypes.DOUBLE;
513         } else if (clazz.isAssignableFrom(java.lang.Float JavaDoc.class)) {
514             type = FieldTypes.FLOAT;
515         } else if (clazz.isAssignableFrom(java.lang.Boolean JavaDoc.class)) {
516             type = FieldTypes.BOOLEAN;
517         }
518     }
519
520     /**
521      * Dump the fieldValue objects contained into the input FieldValue array.
522      *
523      * @param fv
524      * the FieldValue array to dump
525      *
526      * @return the String object containing the dumped data, or null if the
527      * input array is null
528      */

529     public static final String JavaDoc dumpFieldValueArray(FieldValue[] fv) {
530         String JavaDoc s = null;
531
532         if (fv != null) {
533             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
534
535             for (int i = 0; i < fv.length; i++) {
536                 FieldValue f = fv[i];
537                 sb.append(" fv[").append(i).append("] = {").append(
538                         f.toString()).append("}\n");
539             }
540
541             s = sb.toString();
542         }
543
544         return s;
545     }
546
547     /**
548      * DOCUMENT ME!
549      *
550      * @return
551      */

552     public String JavaDoc getEscaperClass() {
553         return escaperClass;
554     }
555
556     /**
557      * DOCUMENT ME!
558      *
559      * @return DOCUMENT ME!
560      */

561     public String JavaDoc getSearchAlgoName() {
562         return getFieldName(Constants.FIELDNAME_SEARCHALGO);
563     }
564
565     /**
566      * DOCUMENT ME!
567      *
568      * @return DOCUMENT ME!
569      */

570     public String JavaDoc getSearchFieldName() {
571         return getFieldName(Constants.FIELDNAME_SEARCH);
572     }
573
574     /**
575      * DOCUMENT ME!
576      *
577      * @return DOCUMENT ME!
578      */

579     public String JavaDoc getSearchModeName() {
580         return getFieldName(Constants.FIELDNAME_SEARCHMODE);
581     }
582
583     /**
584      * Sets the size attribute of the Field object
585      *
586      * @param size
587      * DOCUMENT ME!
588      */

589     public void setSize(int size) {
590         this.size = size;
591     }
592
593     /**
594      * Gets the size attribute of the Field object
595      *
596      * @return DOCUMENT ME!
597      */

598     public int getSize() {
599         return size;
600     }
601
602     /**
603      * DOCUMENT ME!
604      *
605      * @return DOCUMENT ME!
606      */

607     public String JavaDoc getSortFieldName() {
608         return getFieldName(Constants.FIELDNAME_SORT);
609     }
610
611     /**
612      * DOCUMENT ME!
613      *
614      * @param table
615      */

616     public void setTable(Table table) {
617         this.table = table;
618     }
619
620     /**
621      * DOCUMENT ME!
622      *
623      * @return
624      */

625     public Table getTable() {
626         return table;
627     }
628
629     /**
630      * Gets the isAutoInc attribute of the Field object
631      *
632      * @return The isAutoInc value
633      */

634     public boolean hasAutoIncSet() {
635         return isAutoInc;
636     }
637
638     /**
639      * Gets the encoding attribute of the Field object
640      *
641      * @return The encoding value
642      */

643     public boolean hasEncodedSet() {
644         return encoded;
645     }
646
647     /**
648      * Gets the key attribute of the Field object
649      *
650      * @return The key value
651      */

652     public boolean hasIsKeySet() {
653         return Util.getTrue(key);
654     }
655
656     /**
657      * Gets the fieldSortable attribute of the Field object
658      *
659      * @return The fieldSortable value
660      */

661     public boolean hasSortableSet() {
662         return isSortable;
663     }
664
665     /**
666      * Get the String representation of this Field object.
667      *
668      * @return the String representation of this Field object
669      */

670     public String JavaDoc toString() {
671         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
672
673         buf.append("name=");
674         buf.append(name);
675         buf.append(" type=");
676         buf.append(type);
677         buf.append(" key=");
678         buf.append(key);
679         buf.append(" isAutoinc=");
680         buf.append(isAutoInc);
681         buf.append(" issortable=");
682         buf.append(isSortable);
683         buf.append(" directory=");
684         buf.append(directory);
685         buf.append(" expression=");
686         buf.append(expression);
687
688         return buf.toString();
689     }
690 }
691
Popular Tags