KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > AbstractField


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.core;
12
13 import org.mmbase.bridge.*;
14 import org.mmbase.core.util.Fields;
15 import org.mmbase.datatypes.DataType;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * @javadoc
20  *
21  * @author Pierre van Rooden
22  * @author Michiel Meeuwissen
23  * @since MMBase-1.8
24  * @version $Id: AbstractField.java,v 1.14 2006/07/18 12:40:39 michiel Exp $
25  */

26
27 abstract public class AbstractField extends AbstractDescriptor implements Field, Comparable JavaDoc {
28
29     private static final Logger log = Logging.getLoggerInstance(AbstractField.class);
30
31     protected DataType dataType = null;
32     protected int type = TYPE_UNKNOWN;
33     protected int state = STATE_UNKNOWN;
34     protected int listItemType = TYPE_UNKNOWN;
35     protected boolean readOnly = true;
36
37     /**
38      * Create a field object based on another field.
39      * The newly created field shared the datatype of it's parent
40      * (which means that any changes to the datatype affects both fields).
41      * @param name the name of the field
42      * @param field the parent field
43      */

44     protected AbstractField(String JavaDoc name, Field field) {
45          this(name, field, false);
46     }
47
48     /**
49      * Create a field object based on another field.
50      * @param name the name of the field
51      * @param field the parent field
52      * @param cloneDataForRewrite determines whether the datatype of the parent field is copied (which means it can be altered
53      * without affecting the original datatype)
54      */

55     protected AbstractField(String JavaDoc name, Field field, boolean cloneDataForRewrite) {
56         super(name, field, cloneDataForRewrite);
57         type = field.getType();
58         setState(field.getState());
59         readOnly = field.isReadOnly();
60         listItemType = field.getListItemType();
61         if (cloneDataForRewrite) {
62             setDataType((DataType)field.getDataType().clone());
63         } else {
64             setDataType(dataType = field.getDataType());
65         }
66     }
67
68     /**
69      * Create a field object
70      * @param name the name of the field
71      * @param dataType the data type of the field
72      */

73     protected AbstractField(String JavaDoc name, int type, int listItemType, int state, DataType dataType) {
74         super(name);
75         this.type = type;
76         this.listItemType = listItemType;
77         setState(state);
78         setDataType(dataType);
79     }
80
81     abstract public NodeManager getNodeManager();
82
83     public int compareTo(Object JavaDoc o) {
84         if (o instanceof Field) {
85             Field f = (Field) o;
86             int compared = getName().compareTo(f.getName());
87             if (compared == 0) compared = dataType.compareTo(f.getDataType());
88             return compared;
89         } else {
90             throw new ClassCastException JavaDoc("Object is not of type Field");
91         }
92     }
93
94     /**
95      * Whether data type equals to other data type. Only key and type are considered. DefaultValue and
96      * required propererties are only 'utilities'.
97      * @return true if o is a DataType of which key and type equal to this' key and type.
98      */

99     public boolean equals(Object JavaDoc o) {
100         if (o instanceof Field) {
101             Field f = (Field) o;
102             return getName().equals(f.getName()) && dataType.equals(f.getDataType());
103         }
104         return false;
105     }
106
107     public int hashCode() {
108         return getName().hashCode() * 13 + dataType.hashCode();
109     }
110
111     public int getState() {
112         return state;
113     }
114
115     protected void setState(int state) {
116         if (this.state == STATE_UNKNOWN) {
117           readOnly = state == STATE_SYSTEM || state == STATE_SYSTEM_VIRTUAL;
118         }
119         this.state = state;
120     }
121
122     public int getType() {
123         return type;
124     }
125
126     public int getListItemType() {
127         return listItemType;
128     }
129
130     public DataType getDataType() {
131         return dataType;
132     }
133
134     /**
135      * Sets the datatype of a field.
136      * It is possible that the datatype of a field is different from the actual field type.
137      * @see #getType
138      */

139     public void setDataType(DataType dataType) throws IllegalArgumentException JavaDoc {
140         int dataTypeType = dataType.getBaseType();
141         if (dataTypeType != type) {
142             log.debug("DataType (" + dataType.getBaseTypeIdentifier() + ") is different from db type (" + Fields.getTypeDescription(type) + ").");
143         }
144         this.dataType = dataType;
145     }
146
147     public boolean hasIndex() {
148         return (getType() == Field.TYPE_NODE) || getName().equals("number");
149     }
150
151     abstract public int getSearchPosition();
152
153     abstract public int getListPosition();
154
155     abstract public int getEditPosition();
156
157     abstract public int getStoragePosition();
158
159     /**
160      * Retrieve whether the field is a key and thus need be unique.
161      */

162     public boolean isUnique() {
163         return dataType.isUnique();
164     }
165
166     abstract public int getMaxLength();
167
168     /**
169      * @see org.mmbase.bridge.Field#isRequired()
170      */

171     public boolean isRequired() {
172         return dataType.isRequired();
173     }
174
175     /**
176      * @see org.mmbase.bridge.Field#isVirtual()
177      */

178     public boolean isVirtual() {
179        return getState() == STATE_VIRTUAL || getState() == STATE_SYSTEM_VIRTUAL;
180     }
181
182     /**
183      * Returns whether a field is a temporary field.
184      * Temporary fields hold data needed by a builder to facilitate certain functionality,
185      * such as holding node references in a transaction.
186      * Temporary fields are never persistent and should normally be ignored.
187      * @since MMBase-1.8
188      */

189     public boolean isTemporary() {
190         // this way of determining if a field is temporary is a bit odd,
191
// but it is how it worked in the past - should be changed in the future
192
// with possibly a setTemporary() method.
193
String JavaDoc fieldName = getName();
194         return fieldName != null ? fieldName.startsWith("_") : false;
195     }
196
197     /**
198      * @see org.mmbase.bridge.Field#isVirtual()
199      */

200     public boolean isReadOnly() {
201        return readOnly;
202     }
203
204     abstract public String JavaDoc getGUIType();
205
206     /**
207      * Returns a description for this field.
208      */

209     public String JavaDoc toString() {
210         return getName() + ":" +
211             Fields.getTypeDescription(getType()) + " / " +
212             Fields.getStateDescription(getState())+ "/" +
213             getDataType();
214     }
215
216     public Object JavaDoc clone() {
217         return clone (null, false);
218     }
219
220     public Object JavaDoc clone(String JavaDoc name, boolean copyDataTypeForRewrite) {
221         try {
222             AbstractField clone = (AbstractField)super.clone(name);
223             if (copyDataTypeForRewrite) {
224                 clone.dataType = (DataType) dataType.clone();
225             }
226             return clone;
227         } catch (CloneNotSupportedException JavaDoc cnse) {
228             // should not happen
229
throw new RuntimeException JavaDoc("Cannot clone this Field", cnse);
230         }
231     }
232
233 }
234
Popular Tags