KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > DBField


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj;
66
67 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
68 import com.jcorporate.expresso.core.db.DBException;
69 import org.apache.log4j.Logger;
70 import org.apache.oro.text.regex.MalformedPatternException;
71 import org.apache.oro.text.regex.Pattern;
72 import org.apache.oro.text.regex.PatternCompiler;
73 import org.apache.oro.text.regex.Perl5Compiler;
74
75 import java.io.IOException JavaDoc;
76 import java.io.ObjectInputStream JavaDoc;
77 import java.io.Serializable JavaDoc;
78 import java.util.HashMap JavaDoc;
79 import java.util.HashSet JavaDoc;
80 import java.util.Hashtable JavaDoc;
81 import java.util.Iterator JavaDoc;
82 import java.util.Set JavaDoc;
83
84 /**
85  * A DBField object represents a single field definition in a DBObjectDef. It does
86  * not store any actual data, only the meta-data associated with a field.
87  *
88  * @author Michael Nash
89  */

90 public class DBField
91         implements Serializable JavaDoc, DataFieldMetaData {
92
93
94     /**
95      * AUTOINC_TYPE is a special expresso type code for auto-increment field,
96      * a synonym for integer.
97      */

98     transient public static final String JavaDoc AUTOINC_TYPE = "auto-inc";
99
100     transient public static final String JavaDoc ARRAY_TYPE = "array";
101     transient public static final String JavaDoc BIGINT_TYPE = "bigint";
102     transient public static final String JavaDoc BINARY_TYPE = "binary";
103     transient public static final String JavaDoc BIT_TYPE = "bit";
104     /**
105      * boolean is synonym for bit
106      */

107     transient public static final String JavaDoc BOOLEAN_TYPE = "boolean";
108     transient public static final String JavaDoc BLOB_TYPE = "blob";
109     transient public static final String JavaDoc CHAR_TYPE = "char";
110     transient public static final String JavaDoc CLOB_TYPE = "clob";
111
112     transient public static final String JavaDoc DATE_TYPE = "date";
113     /**
114      * datetime is a synonym for timestamp
115      */

116     transient public static final String JavaDoc DATETIME_TYPE = "datetime";
117     transient public static final String JavaDoc DOUBLE_TYPE = "double";
118     transient public static final String JavaDoc DECIMAL_TYPE = "decimal";
119
120     transient public static final String JavaDoc FLOAT_TYPE = "float";
121     transient public static final String JavaDoc INTEGER_TYPE = "int";
122     /**
123      * int is synonym for integer
124      */

125     transient public static final String JavaDoc INT_TYPE = "integer";
126     transient public static final String JavaDoc JAVA_OBJECT = "java_object";
127
128     /**
129      * long is a synonym for BIGINT
130      */

131     transient public static final String JavaDoc LONG_TYPE = "long";
132     transient public static final String JavaDoc LONGVARCHAR_TYPE = "longvarchar";
133     transient public static final String JavaDoc LONGVARBINARY = "longvarbinary";
134     /**
135      * null isn't supported in expresso as of 5/02
136      */

137     transient public static final String JavaDoc NULL_TYPE = "null";
138     transient public static final String JavaDoc NUMERIC_TYPE = "numeric";
139     transient public static final String JavaDoc OTHER_TYPE = "other";
140
141     transient public static final String JavaDoc REAL_TYPE = "real";
142     transient public static final String JavaDoc REF_TYPE = "ref";
143     transient public static final String JavaDoc SMALLINT_TYPE = "smallint";
144     transient public static final String JavaDoc STRUCT_TYPE = "struct";
145
146     transient public static final String JavaDoc TIME_TYPE = "time";
147     transient public static final String JavaDoc TIMESTAMP_TYPE = "timestamp";
148     /**
149      * text is a synonym for longvarchar
150      */

151     transient public static final String JavaDoc TEXT_TYPE = "text";
152     transient public static final String JavaDoc TINYINT_TYPE = "tinyint";
153     transient public static final String JavaDoc VARBINARY_TYPE = "varbinary";
154     transient public static final String JavaDoc VARCHAR_TYPE = "varchar";
155
156     /**
157      * Boolean regular expression syntax for easy reference.
158      */

159     public static final String JavaDoc BOOLEAN_MASK = "^([01]|true|TRUE|false|FALSE|y|Y|n|N)$";
160
161     /**
162      * Date regular expression syntax for easy reference.
163      */

164     public static final String JavaDoc DATE_MASK = "^[\\d\\s-:/.+]+$";
165
166     /**
167      * Integer Regular Expression for easy reference
168      */

169     public static final String JavaDoc INT_MASK = "^[+-]?[0-9]+";
170
171     /**
172      * Floating point regular expression syntax for easy reference.
173      */

174     public static final String JavaDoc FLOAT_MASK = "^([+-]?)(?=\\d|\\.\\d)\\d*(\\.\\d*)?([Ee]([+-]?\\d+))?$";
175
176     /**
177      * Perl5 Regular Expression Compiler. The patterns generated by this compiler
178      * are mainly stored in the field mask table.
179      */

180     transient private static PatternCompiler patternCompiler =
181             new Perl5Compiler();
182
183     /**
184      * Attribute String for if there's an error with the field.
185      */

186     public static final String JavaDoc ATTRIBUTE_ERROR = "error";
187
188     /**
189      * Attribute String for what message to display if there's an error
190      * with thie field.
191      */

192     public static final String JavaDoc ATTRIBUTE_ERROR_MESSAGE = "error-message";
193
194
195     protected String JavaDoc fieldName;
196
197     /**
198      * The string "type" used to specify this type of field in Expresso
199      */

200     protected String JavaDoc expressoFieldTypeString;
201
202     //protected String fieldValue;
203

204     /**
205      * Is a null value allowed for this field.
206      */

207     protected boolean allowNull;
208
209     /**
210      * The user friendly description of this field.
211      */

212     protected String JavaDoc description;
213
214     /**
215      * The size of this field. ex: 80 for VARCHAR(80)
216      */

217     protected int fieldSize;
218
219     /**
220      * Is this a virtual field: Ie, does the system provide all values
221      * for this field.
222      */

223     protected boolean isVirtual = false;
224
225     /* regular expression "mask" for this field, if any */
226     protected Pattern mask = null;
227
228     /**
229      * User Defined Attributes for each field.
230      */

231     protected Hashtable JavaDoc attributes = null;
232
233     /**
234      * Is this field selected as a unique or distinct field?
235      */

236     /* protected boolean isDistinct = false; */
237     /**
238      * Is this a multivalued field
239      */

240     protected boolean isMultiValued = false;
241
242     /**
243      * Does this field not appear in standard DBMaint listings?
244      */

245     protected boolean isSecret = false;
246
247     /**
248      * Is this field readonly?
249      */

250     protected boolean isReadOnly = false;
251
252     /**
253      * is the field boolean?
254      */

255     private boolean isBoolean;
256
257
258     /**
259      * is the field numeric?
260      */

261     private boolean isNumeric;
262     /**
263      * is the field a date type?
264      */

265     private boolean isDate;
266
267     /**
268      * is the field a datetime or timestamp type?
269      * This necessary for handling correctly the Database datatime format
270      * when retrieve data from resultSet
271      * author Yves Henri AMAIZO
272      */

273     private boolean isDateTime;
274     /**
275      * is the field a time type?
276      * This necessary for handling correctly the Database datatime format
277      * when retrieve data from resultSet
278      * author Yves Henri AMAIZO
279      */

280     private boolean isTime;
281
282     /**
283      * is the field a date type?
284      * This necessary for handling correctly the Database datatime format
285      * when retrieve data from resultSet
286      * author Yves Henri AMAIZO
287      */

288     private boolean isDateOnly;
289
290
291     /**
292      * The default value for when a data object is first initialized.
293      */

294     private String JavaDoc defaultValue;
295
296     /**
297      * is the field a time type?
298      * This necessary for handling correctly the Database string format data
299      * when retrieve data from resultSet
300      * Oracle does not handlin correctly string more longer than 4000 chars
301      * author Yves Henri AMAIZO
302      */

303     private boolean isLongBinary;
304
305     private boolean isLongCharacter;
306
307     /**
308      * is the field a text type?
309      */

310     private boolean isText;
311
312     /**
313      * String for lookup object classname
314      */

315     protected String JavaDoc lookupObject;
316
317     /**
318      * String for the lookup field in the lookup object that maps to this
319      * field.
320      */

321     protected String JavaDoc lookupField;
322
323     /**
324      * Optional parameter that defines the definition of the lookup object if
325      * the lookup object implements the Defineable interface.
326      */

327     protected String JavaDoc lookupDefinition;
328
329     /**
330      * Is this a key field?
331      */

332     protected boolean isKey = false;
333
334     /**
335      * Is this object a LOB?
336      */

337     protected boolean isLongObject = false;
338
339     /**
340      * Is this object a CLOB?
341      */

342     protected boolean isCharacterLongObject = false;
343
344     /**
345      * What is the string filtering method applied with this filter
346      */

347     protected String JavaDoc filterMethod = "standardFilter";
348
349     /**
350      * The Log Category associated with DBFields
351      */

352     private transient static Logger logCat = Logger.getLogger(DBField.class);
353
354     /**
355      * set if the field is autoincremented
356      */

357     private boolean isAutoInc = false;
358
359     /**
360      * Set this value to true if you want a particular string field encrypted
361      * (Not implemented yet)
362      */

363     protected boolean encrypted = false;
364
365     /**
366      * Set this value to true if you want a particular string field hashed
367      * instead of stored in plaintext.
368      * (Not implamented yet)
369      */

370     protected boolean hashed = false;
371
372     /**
373      * Field precision, if applicable to this field
374      */

375     protected int precision;
376
377     protected boolean isFloatingPointType = false;
378
379     /**
380      * the preferred filtering class; null indicates default
381      */

382     private Class JavaDoc mFilterClass;
383
384     /**
385      * contains a list of all data types that should be quoted when building
386      * SQL strings.
387      */

388     transient private static Set JavaDoc quotedDataTypes = new HashSet JavaDoc(5);
389     transient private static Set JavaDoc numericDataTypes = new HashSet JavaDoc(10);
390     transient private static Set JavaDoc dateTypes = new HashSet JavaDoc(4);
391
392     /**
393      * Contains a list of all recognized expresso data types.
394      */

395     transient public static Set JavaDoc allDataTypes = new HashSet JavaDoc(40);
396
397
398     static {
399         quotedDataTypes.add(CHAR_TYPE);
400         quotedDataTypes.add(VARCHAR_TYPE);
401         quotedDataTypes.add(TEXT_TYPE);
402         quotedDataTypes.add(LONGVARCHAR_TYPE);
403
404         numericDataTypes.add(NUMERIC_TYPE);
405         numericDataTypes.add(INT_TYPE);
406         numericDataTypes.add(INTEGER_TYPE);
407         numericDataTypes.add(DOUBLE_TYPE);
408         numericDataTypes.add(FLOAT_TYPE);
409         numericDataTypes.add(REAL_TYPE);
410         numericDataTypes.add(DECIMAL_TYPE);
411         numericDataTypes.add(AUTOINC_TYPE);
412         numericDataTypes.add(BIGINT_TYPE);
413         numericDataTypes.add(LONG_TYPE);
414         numericDataTypes.add(SMALLINT_TYPE);
415         numericDataTypes.add(TINYINT_TYPE);
416
417
418         dateTypes.add(DATE_TYPE);
419         dateTypes.add(DATETIME_TYPE);
420         dateTypes.add(TIME_TYPE);
421         dateTypes.add(TIMESTAMP_TYPE);
422
423         allDataTypes.add(DBField.ARRAY_TYPE);
424         allDataTypes.add(DBField.BIGINT_TYPE);
425         allDataTypes.add(DBField.LONG_TYPE);
426         allDataTypes.add(DBField.BINARY_TYPE);
427         allDataTypes.add(DBField.BIT_TYPE);
428         allDataTypes.add(DBField.BOOLEAN_TYPE);
429         allDataTypes.add(DBField.BLOB_TYPE);
430         allDataTypes.add(DBField.CHAR_TYPE);
431         allDataTypes.add(DBField.CLOB_TYPE);
432         allDataTypes.add(DBField.DATE_TYPE);
433         allDataTypes.add(DBField.DECIMAL_TYPE);
434         allDataTypes.add(DBField.DOUBLE_TYPE);
435         allDataTypes.add(DBField.FLOAT_TYPE);
436         allDataTypes.add(DBField.INTEGER_TYPE);
437         allDataTypes.add(DBField.INT_TYPE);
438         allDataTypes.add(DBField.JAVA_OBJECT);
439         allDataTypes.add(DBField.LONGVARBINARY);
440         allDataTypes.add(DBField.LONGVARCHAR_TYPE);
441         allDataTypes.add(DBField.NUMERIC_TYPE);
442         allDataTypes.add(DBField.OTHER_TYPE);
443         allDataTypes.add(DBField.REAL_TYPE);
444         allDataTypes.add(DBField.REF_TYPE);
445         allDataTypes.add(DBField.SMALLINT_TYPE);
446         allDataTypes.add(DBField.STRUCT_TYPE);
447         allDataTypes.add(DBField.TIME_TYPE);
448         allDataTypes.add(DBField.TIMESTAMP_TYPE);
449         allDataTypes.add(DBField.DATETIME_TYPE);
450         allDataTypes.add(DBField.TINYINT_TYPE);
451         allDataTypes.add(DBField.VARBINARY_TYPE);
452         allDataTypes.add(DBField.VARCHAR_TYPE);
453
454         // TEXT_TYPE synonym for longvarchar
455
allDataTypes.add(DBField.TEXT_TYPE);
456
457         // auto-inc is unique to expresso
458
allDataTypes.add(DBField.AUTOINC_TYPE);
459
460     }
461
462
463     /**
464      * Constructor: A DBField is initialized knowing it's name, type, size,
465      * description and whether or not it can accept null or empty values
466      *
467      * @param myName Field name of this field
468      * @param myType Database type of the field - use string constant from DBField
469      * @param mySize Size of the field in characters
470      * @param newPrecision precision for this field
471      * @param myAllowNull True if null is allowed, false if not
472      * @param myDescrip Description (title) of the field
473      */

474     public DBField(String JavaDoc myName, String JavaDoc myType, int mySize, int newPrecision,
475                    boolean myAllowNull, String JavaDoc myDescrip)
476             throws DBException {
477         if (myName.length() > 18) {
478             logCat.warn("Field name '" + myName +
479                     "' is over 18 characters - may not be portable");
480         }
481
482         fieldName = myName;
483         expressoFieldTypeString = myType;
484         allowNull = myAllowNull;
485         description = myDescrip;
486         fieldSize = mySize;
487         precision = newPrecision;
488
489         isNumeric = numericDataTypes.contains(myType);
490         isBoolean = myType.equalsIgnoreCase(BOOLEAN_TYPE);
491         isDate = dateTypes.contains(myType);
492         isDateOnly = myType.equalsIgnoreCase(DATE_TYPE);
493         isTime = myType.equalsIgnoreCase(TIME_TYPE);
494         isDateTime = myType.equalsIgnoreCase(TIMESTAMP_TYPE) ||
495                 myType.equalsIgnoreCase(DATETIME_TYPE);
496         isText = quotedDataTypes.contains(myType);
497         isAutoInc = myType.equalsIgnoreCase(AUTOINC_TYPE);
498         isFloatingPointType = myType.equalsIgnoreCase(FLOAT_TYPE);
499
500         isCharacterLongObject = (myType.equalsIgnoreCase(TEXT_TYPE) ||
501                 myType.equalsIgnoreCase(LONGVARCHAR_TYPE) ||
502                 myType.equalsIgnoreCase(CLOB_TYPE));
503
504         isLongObject = (isCharacterLongObject || myType.equalsIgnoreCase(BINARY_TYPE) ||
505                 myType.equalsIgnoreCase(BLOB_TYPE) ||
506                 myType.equalsIgnoreCase(JAVA_OBJECT) ||
507                 myType.equalsIgnoreCase(LONGVARBINARY) ||
508                 myType.equalsIgnoreCase(VARBINARY_TYPE));
509
510         setDefaultMask();
511
512     } /* DBField(String, String, int, boolean, String) */
513
514     /**
515      * constructor for types with no size
516      *
517      * @param myName Field name of this field
518      * @param myType Database type of the field - use string constant from DBField
519      * @param newPrecision precision for this field
520      * @param myAllowNull True if null is allowed, false if not
521      * @param myDescrip Description (title) of the field
522      */

523     public DBField(String JavaDoc myName, String JavaDoc myType, int newPrecision,
524                    boolean myAllowNull, String JavaDoc myDescrip)
525             throws DBException {
526         this(myName, myType, 0, newPrecision, myAllowNull, myDescrip);
527     }
528
529     /**
530      * Does this field allow nulls?
531      *
532      * @return boolean True if the field allows null, else false if it does not
533      */

534     public boolean allowsNull() {
535         return allowNull;
536     } /* allowsNull() */
537
538     /**
539      * Return the description of this field
540      *
541      * @return String Description of the field
542      */

543     public String JavaDoc getDescription() {
544         return description;
545     } /* getDescription() */
546
547
548     /**
549      * Retrieve the default value for the field.
550      *
551      * @return String
552      */

553     public String JavaDoc getDefaultValue() {
554         return defaultValue;
555     }
556
557     /**
558      * Sets the default value of the field.
559      *
560      * @param newValue String
561      */

562     public void setDefaultValue(String JavaDoc newValue) {
563         this.defaultValue = newValue;
564     }
565
566     /**
567      * Return the filter method currently used for this field.
568      *
569      * @return The name of the filter method
570      */

571     public String JavaDoc getFilterMethod() {
572         return filterMethod;
573     } /* getFilterMethod() */
574
575     /**
576      * Return the length of the field in characters
577      *
578      * @return String A String containing the number of characters of
579      * this fields length
580      */

581     public String JavaDoc getLength() {
582         return String.valueOf(fieldSize);
583     } /* getLength() */
584
585     /**
586      * Return the length of this field as an integer
587      *
588      * @return int The length of this field in characters
589      */

590     public int getLengthInt() {
591         return fieldSize;
592     } /* getLengthInt() */
593
594     /**
595      * Return the value for the lookupObject for this field
596      *
597      * @return String for the lookup object for this field
598      */

599     public String JavaDoc getLookupObject() {
600         return lookupObject;
601     } /* getLookupObject() */
602
603     /**
604      * Future versions of Expresso will rely more on objects that are shown as
605      * unique by a combination of their classname and their definition names. All
606      * instances of Defineable must have a corresponding definition name.
607      *
608      * @return java.lang.String
609      */

610     public String JavaDoc getLookupDefinition() {
611         return lookupDefinition;
612     }
613
614     /**
615      * When you get a lookup object, to perform a complete mapping between the
616      * two, you need to know what field name in the remote object maps to this
617      * field.
618      *
619      * @return java.lang.String or null if there is no lookup field
620      * @throws IllegalArgumentException if the field name does not exist
621      */

622     public String JavaDoc getLookupField() {
623         return this.lookupField;
624     }
625
626     /**
627      * Return the name of the field
628      *
629      * @return String The name of this field
630      */

631     public String JavaDoc getName() {
632         return fieldName;
633     } /* getName() */
634
635     /**
636      * Return the precision of this field as an integer
637      *
638      * @return int The precision of this field
639      */

640     public int getPrecision() {
641         return precision;
642     } /* getPrecision() */
643
644     /**
645      * Return the database type of the field as specified with a
646      * string in the DBObject itself
647      *
648      * @return The type of this field
649      */

650     public String JavaDoc getTypeString() {
651         return expressoFieldTypeString;
652     } /* getTypeString() */
653
654     /**
655      * Is this field a key field?
656      *
657      * @return true if this field is a key field
658      */

659     public boolean isKey() {
660         return isKey;
661     } /* isKey() */
662
663     /**
664      * Is this field multi-valued?
665      *
666      * @return boolean True if the field is multi-valued, else false
667      */

668     public boolean isMultiValued() {
669         return isMultiValued;
670     } /* isMultiValued() */
671
672     /**
673      * Return the field's read-only
674      *
675      * @return True if the field is readonly, else false if it is not
676      */

677     public boolean isReadOnly() {
678         return isReadOnly || isAutoIncremented();
679     } /* isReadOnly() */
680
681     /**
682      * is the field a boolean?
683      * cache answer for efficiency, since field type is immutable
684      *
685      * @return True if the field is boolean
686      */

687     public boolean isBooleanType() {
688         return this.isBoolean;
689     }
690
691     /**
692      * Is this field fall into the classification of a long character
693      * object?
694      *
695      * @return boolean true if it does
696      */

697     public boolean isCharacterLongObjectType() {
698         return this.isCharacterLongObject;
699     }
700
701     /**
702      * Does this field fall into a class of BLOB object data types?
703      *
704      * @return boolean true if it does
705      */

706     public boolean isLongObjectType() {
707         return this.isLongObject;
708     }
709
710     /**
711      * Is it a LONGVARBINARY? This is calculated by if it is a long object
712      * and is NOT a character long object.
713      *
714      * @return boolean True if it is
715      */

716     public boolean isLongBinaryType() {
717         return this.isLongBinary;
718     }
719
720     /**
721      * Is it a LONGVARCHAR? This is calculated by if it is a long object
722      * and is NOT a character long object.
723      *
724      * @return boolean True if it is
725      */

726     public boolean isLongCharacterType() {
727         return this.isLongCharacter;
728     }
729
730     /**
731      * Is it a BLOB? This is calculated by if it is a long object
732      * and is NOT a character long object.
733      *
734      * @return boolean True if it is
735      */

736     public boolean isBinaryObjectType() {
737         return (isLongObject & !isCharacterLongObject & !isLongBinary);
738     }
739
740     /**
741      * is the field a quoted text field?
742      * cache answer for efficiency, since field type is immutable
743      *
744      * @return True if the field is text field
745      */

746     public boolean isQuotedTextType() {
747         return isText;
748     }
749
750     /**
751      * is the field a numeric field?
752      * cache answer for efficiency, since field type is immutable
753      *
754      * @return true if the field is numberic field
755      */

756     public boolean isNumericType() {
757         return isNumeric;
758     }
759
760     /**
761      * is the field a date or time field?
762      * cache answer for efficiency, since field type is immutable
763      *
764      * @return true if the field is date or time field
765      */

766     public boolean isDateType() {
767         return isDate;
768     }
769
770     /**
771      * is the field a date or time field?
772      * cache answer for efficiency, since field type is immutable
773      *
774      * @return true if the field is date field
775      * author Yves Henri AMAIZO
776      */

777     public boolean isDateOnlyType() {
778         return isDateOnly;
779     }
780
781     public boolean isFloatingPointType() {
782         return isFloatingPointType;
783     }
784
785     /**
786      * is the field a date or time field?
787      * cache answer for efficiency, since field type is immutable
788      *
789      * @return true if the field is time field
790      * author Yves Henri AMAIZO
791      */

792     public boolean isTimeType() {
793         return isTime;
794     }
795
796
797     /**
798      * is the field a date or time field?
799      * cache answer for efficiency, since field type is immutable
800      *
801      * @return true if the field is dateTime or Timestamp field
802      * author Yves Henri AMAIZO
803      */

804     public boolean isDateTimeType() {
805         return isDateTime;
806     }
807
808     /**
809      * Return the field's secret status
810      *
811      * @return True if the field is secret, else false if it is not
812      */

813     public boolean isSecret() {
814         return isSecret;
815     } /* isSecret() */
816
817     /**
818      * Return the field's hashed status
819      *
820      * @return True if the field is secret, else false if it is not
821      * @todo This is not completely implemented yet.
822      */

823     public boolean isHashed() {
824         return hashed;
825     } /* isHashed() */
826
827     /**
828      * Return the field's hashed status
829      *
830      * @return True if the field is secret, else false if it is not
831      * @todo This is not completely implemented yet.
832      */

833     public boolean isEncrypted() {
834         return encrypted;
835     } /* isEncrypted() */
836
837     /**
838      * Is this field a virtual field? E.g. not stored in the database
839      *
840      * @return boolean True if the field is virtual, else false
841      */

842     public boolean isVirtual() {
843         return isVirtual;
844     } /* isVirtual() */
845
846     /**
847      * Set's the name of the filter method to be used
848      *
849      * @param newMethod The name of the filter method to use.
850      * @return old filter just replaced
851      * @see com.jcorporate.expresso.core.security.filters.FilterManager
852      */

853     public String JavaDoc setFilterMethod(String JavaDoc newMethod)
854             throws DBException {
855
856         //Check for validity of new method
857
if (!(newMethod.equals("standardFilter") || newMethod.equals("rawFilter") ||
858                 newMethod.equals("stripFilter"))) {
859             throw new DBException("Unknown Filter Method Name: " + newMethod);
860         }
861
862         String JavaDoc oldmethod = filterMethod;
863         filterMethod = newMethod;
864         return oldmethod;
865     } /* setFilterMethod(String) */
866
867
868     /**
869      * Set this field as a key field (or not)
870      *
871      * @param newKey Is this field a key?
872      */

873     public void setKey(boolean newKey) {
874         isKey = newKey;
875     } /* setKey(boolean) */
876
877     /**
878      * Set the value for the "lookup object" for this field. This is
879      * a database object name which can be used to look up valid
880      * values for this field by the user. This is used by the standard
881      * maintenance forms to create a "Lookup" link alongside the
882      * field if this value is set
883      *
884      * @param objectName the classname of the lookup object
885      */

886     public synchronized void setLookupObject(String JavaDoc objectName) {
887         lookupObject = objectName;
888     } /* setLookupObject(String) */
889
890     /**
891      * Sets the value for the "lookup field". This is the field name in the
892      * lookup object that maps to this particular field.
893      *
894      * @param lookupFieldName the name of the field to retrieve.
895      */

896     public synchronized void setLookupField(String JavaDoc lookupFieldName) {
897         lookupField = lookupFieldName;
898     }
899
900     /**
901      * Sets the value for the "lookup definition" this is the name of the definition
902      * for objects implementing the defineable interface.
903      *
904      * @param definitionName java.lang.String. Depends on the class implementation
905      * of the Defineable implementation.
906      */

907     public synchronized void setLookupDefinition(String JavaDoc definitionName) {
908         lookupDefinition = definitionName;
909     }
910
911     /**
912      * Set this field to be "multi-valued". A multi-valued field has
913      * a specific set of valid values, often from another database
914      * object. Any multi-valued field may be used in a call to the
915      * getValues method, which will return a hashtable of the valid
916      * values for the field and descriptions for those values.
917      *
918      * @param newMulti True if the field is multi-valued, false if it is not
919      * @see com.jcorporate.expresso.core.dbobj.DBObject#isMultiValued
920      */

921     public synchronized void setMultiValued(boolean newMulti) {
922         isMultiValued = newMulti;
923     } /* setMultiValued(boolean) */
924
925     /**
926      * Set the field's hashed status. Only works if the field is a string
927      * data type (in the future CLOB should be ok too) [This is currently not
928      * yet implemented and tested]
929      *
930      * @param newValue true if you want this field hashed.
931      */

932     public synchronized void setHashed(boolean newValue) {
933         if (this.isQuotedTextType()) {
934             if (logCat.isDebugEnabled()) {
935                 logCat.debug("Setting field " + fieldName +
936                         " to hashed status");
937             }
938
939             hashed = newValue;
940         } else {
941             throw new java.lang.IllegalArgumentException JavaDoc(fieldName +
942                     " Field needs to be a string data type field");
943         }
944     } /* setHashed(boolean) */
945
946
947     /**
948      * Set the field's encrypted status
949      *
950      * @param newValue new value if you want an encrypted field
951      * @todo This is not completely implemented yet.
952      */

953     public synchronized void setEncrypted(boolean newValue) {
954         if (this.isQuotedTextType()) {
955             if (logCat.isDebugEnabled()) {
956                 logCat.debug("Setting field " + fieldName +
957                         " to encrypted status");
958             }
959
960             encrypted = newValue;
961         } else {
962             throw new IllegalArgumentException JavaDoc("Field needs to be a string data type field");
963         }
964     } /* setEncrypted(boolean) */
965
966
967     /**
968      * Set the field as a read-only field. Read only fields are still used
969      * against the database, but are not offered for updating when the
970      * automatic database maintenance servlet creates a form on the screen. Note
971      * this is different from the setAutoIncremented method below, which
972      * means this field will not participate in any add or update statement
973      * to the database.
974      */

975     public synchronized void setReadOnly() {
976         isReadOnly = true;
977     } /* setReadOnly() */
978
979     /**
980      * Is this field an auto-incremented field?
981      *
982      * @return true if this field is autoincremented
983      */

984     public boolean isAutoIncremented() {
985         return isAutoInc;
986     } /* isAutoIncremented() */
987
988     /**
989      * Set the field as a 'secret' field. Secret fields are not shown
990      * in listings of data from this database object, and are only available
991      * to users with update, add or delete permissions
992      */

993     public synchronized void setSecret() {
994         isSecret = true;
995     } /* setSecret() */
996
997     /**
998      * Set this field as a virtual field. A virtual field is part of the object
999      * but not stored in the database table.
1000     *
1001     * @param newVirtual True to make this object virtual, false if it is not
1002     */

1003    public synchronized void setVirtual(boolean newVirtual) {
1004        isVirtual = newVirtual;
1005    } /* setVirtual(boolean) */
1006
1007    /**
1008     * Set a regular expression "mask" for this field that specifies it's
1009     * valid values. The mask should already be compiled by the regular
1010     * expression compiler
1011     *
1012     * @param newMask The compiled regular expression mask
1013     */

1014    public void setMask(Pattern newMask) {
1015        mask = newMask;
1016    }
1017
1018    /**
1019     * Set the field's default mask according to it's type
1020     *
1021     * @throws DBException on error
1022     */

1023    public void setDefaultMask()
1024            throws DBException {
1025        /* Still have not defined default masks for the following types. Either they are text types, which will be quoted
1026            and do not need default masks, or do not fit into the mold (like BLOB)
1027                        DBField.ARRAY_TYPE
1028                        DBField.AUTOINC_TYPE
1029                        DBField.BINARY_TYPE
1030                        DBField.BLOB_TYPE
1031                        DBField.CHAR_TYPE
1032                        DBField.CLOB_TYPE
1033                        DBField.JAVA_OBJECT
1034                        DBField.LONGVARBINARY
1035                        DBField.LONGVARCHAR_TYPE
1036                        DBField.NULL_TYPE
1037                        DBField.OTHER_TYPE
1038                        DBField.REF_TYPE
1039                        DBField.STRUCT_TYPE
1040                        DBField.TEXT_TYPE
1041                        DBField.VARBINARY_TYPE
1042                        DBField.VARCHAR_TYPE
1043        */

1044
1045        // integer types
1046
if (expressoFieldTypeString.equalsIgnoreCase(DBField.BIGINT_TYPE) ||
1047                expressoFieldTypeString.equalsIgnoreCase(DBField.INTEGER_TYPE) ||
1048                expressoFieldTypeString.equalsIgnoreCase(DBField.INT_TYPE) ||
1049                expressoFieldTypeString.equalsIgnoreCase(DBField.LONG_TYPE) ||
1050                expressoFieldTypeString.equalsIgnoreCase(DBField.SMALLINT_TYPE) ||
1051                expressoFieldTypeString.equalsIgnoreCase(DBField.TINYINT_TYPE)) {
1052            Pattern p;
1053            try {
1054                p = patternCompiler.compile(INT_MASK, Perl5Compiler.READ_ONLY_MASK);
1055            } catch (MalformedPatternException mpe) {
1056                throw new DBException(mpe);
1057            }
1058            setMask(p);
1059            // TODO not i18n
1060
setAttribute(ATTRIBUTE_ERROR_MESSAGE,
1061                    "You must enter a valid integer for field '" +
1062                    description +
1063                    "'");
1064        }
1065        // decimal types
1066
else if (expressoFieldTypeString.equalsIgnoreCase(DBField.DECIMAL_TYPE) ||
1067                expressoFieldTypeString.equalsIgnoreCase(DBField.DOUBLE_TYPE) ||
1068                expressoFieldTypeString.equalsIgnoreCase(DBField.FLOAT_TYPE) ||
1069                expressoFieldTypeString.equalsIgnoreCase(DBField.NUMERIC_TYPE) ||
1070                expressoFieldTypeString.equalsIgnoreCase(DBField.REAL_TYPE)) {
1071
1072            Pattern p;
1073            try {
1074                p = patternCompiler.compile(FLOAT_MASK, Perl5Compiler.READ_ONLY_MASK);
1075            } catch (MalformedPatternException mpe) {
1076                throw new DBException(mpe);
1077            }
1078            setMask(p);
1079            //TODO not i18n
1080
setAttribute(ATTRIBUTE_ERROR_MESSAGE,
1081                    "You must enter a valid decimal for field '" +
1082                    description +
1083                    "'");
1084        }
1085        // boolean types
1086
else if (expressoFieldTypeString.equalsIgnoreCase(DBField.BIT_TYPE) ||
1087                expressoFieldTypeString.equalsIgnoreCase(DBField.BOOLEAN_TYPE)) {
1088
1089            Pattern p;
1090            try {
1091                p = patternCompiler.compile(BOOLEAN_MASK, Perl5Compiler.READ_ONLY_MASK);
1092            } catch (MalformedPatternException mpe) {
1093                throw new DBException(mpe);
1094            }
1095            setMask(p);
1096            // TODO not i18n
1097
setAttribute(ATTRIBUTE_ERROR_MESSAGE,
1098                    "You must enter a valid boolean for field '" +
1099                    description +
1100                    "'");
1101        }
1102        // date types
1103
else if (expressoFieldTypeString.equalsIgnoreCase(DBField.DATETIME_TYPE) ||
1104                expressoFieldTypeString.equalsIgnoreCase(DBField.DATE_TYPE) ||
1105                expressoFieldTypeString.equalsIgnoreCase(DBField.TIMESTAMP_TYPE) ||
1106                expressoFieldTypeString.equalsIgnoreCase(DBField.TIME_TYPE)) {
1107
1108            Pattern p;
1109            try {
1110                p = patternCompiler.compile(DATE_MASK, Perl5Compiler.READ_ONLY_MASK);
1111            } catch (MalformedPatternException mpe) {
1112                throw new DBException(mpe);
1113            }
1114            setMask(p);
1115            // TODO not i18n
1116
setAttribute(ATTRIBUTE_ERROR_MESSAGE,
1117                    "You must enter a valid date for field '" +
1118                    description +
1119                    "'");
1120        }
1121    }
1122
1123    /**
1124     * Get the compiled regular expression for this field.
1125     *
1126     * @return the precompiled regular expression mask
1127     */

1128    public Pattern getMask() {
1129        return mask;
1130    }
1131
1132    /**
1133     * Removes an attribute from this Field object.
1134     *
1135     * @param attribName The name of the attribute to remove from this field.
1136     */

1137    public synchronized void removeAttribute(String JavaDoc attribName) {
1138        if (attributes == null) {
1139            return;
1140        }
1141
1142        attributes.remove(attribName);
1143    }
1144
1145    /**
1146     * Sets an attribute for this particular field.
1147     *
1148     * @param attribName the name of the attribute
1149     * @param attribValue the value of the attribute by this name
1150     */

1151    public synchronized void setAttribute(String JavaDoc attribName,
1152                                          Object JavaDoc attribValue) {
1153        if (attributes == null) {
1154            attributes = new Hashtable JavaDoc();
1155        }
1156
1157        attributes.put(attribName, attribValue);
1158    }
1159
1160    /**
1161     * Returns an attribute keyed by name. An attribute is an arbitrary object
1162     * you can associate with a particular db field.
1163     *
1164     * @param attribName the name of the attribute to get
1165     * @return java.lang.Object the object associated with this attribute name
1166     */

1167    public Object JavaDoc getAttribute(String JavaDoc attribName) {
1168        if (attributes == null) {
1169            return null;
1170        }
1171
1172        return attributes.get(attribName);
1173    }
1174
1175    /**
1176     * Returns a copy of all attributes associated with this field.
1177     *
1178     * @return a valid Iterator to a HashMap OR null if no attributes exist for
1179     * this field
1180     */

1181    public Iterator JavaDoc getAttributesIterator() {
1182        if (attributes == null) {
1183            return null;
1184        }
1185
1186        return new HashMap JavaDoc(attributes).keySet().iterator();
1187    }
1188
1189
1190    /**
1191     * Retrieves all attribute names for this particular data field's metadata
1192     *
1193     * @return java.util.Set
1194     */

1195    public java.util.Set JavaDoc getAllAttributes() {
1196        if (attributes == null) {
1197            return null;
1198        }
1199
1200        return new HashMap JavaDoc(attributes).keySet();
1201    }
1202
1203
1204    /**
1205     * Reads the Field object from a serialization stream.
1206     *
1207     * @param stream The object input stream from which to create this object
1208     */

1209    private void readObject(ObjectInputStream JavaDoc stream)
1210            throws IOException JavaDoc, ClassNotFoundException JavaDoc {
1211        if (logCat == null) {
1212            logCat = Logger.getLogger(DBField.class);
1213        }
1214
1215        stream.defaultReadObject();
1216    }
1217
1218    /**
1219     * getter for Filter Class
1220     * used in conjuction with get/setFilterMethod
1221     *
1222     * @return class for current filter; NULL if not explicitly set; NULL implies using standard filter
1223     * @see #getFilterMethod
1224     */

1225    public Class JavaDoc getFilterClass() {
1226        return mFilterClass;
1227    }
1228
1229    /**
1230     * Set's the name of the filter class to be used
1231     * used in conjuction with get/setFilterMethod
1232     *
1233     * @param filterClass The class of the filter method to use.
1234     * @see #setFilterMethod
1235     * @see com.jcorporate.expresso.core.security.filters.FilterManager
1236     */

1237    public void setFilterClass(Class JavaDoc filterClass)
1238            throws DBException {
1239
1240        mFilterClass = filterClass;
1241    }
1242
1243    /**
1244     * Return boolean if the field has a mask set
1245     *
1246     * @return True if the field mask is set, else false if it is not
1247     */

1248    public boolean isMasked() {
1249        return mask != null;
1250    } /* isMasked() */
1251
1252
1253} /* DBField */
1254
Popular Tags