KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.mmbase.core;
11
12 import org.mmbase.bridge.Field;
13 import org.mmbase.bridge.NodeManager;
14 import org.mmbase.datatypes.*;
15 import org.mmbase.core.*;
16 import org.mmbase.core.util.Fields;
17 import org.mmbase.module.core.MMObjectBuilder;
18 import org.mmbase.module.core.MMBase;
19 import org.mmbase.storage.*;
20 import org.mmbase.util.*;
21 import java.util.Collection JavaDoc;
22
23 import org.mmbase.util.logging.*;
24
25 /**
26  * @since MMBase-1.8
27  */

28 public class CoreField extends AbstractField implements Field, Storable, Cloneable JavaDoc {
29
30     private static final Logger log = Logging.getLoggerInstance(CoreField.class);
31
32     private static final int NO_POSITION = -1;
33
34     private int storageType; // JDBC type of this field
35
private int searchPosition = NO_POSITION;
36     private int listPosition = NO_POSITION;
37     private int editPosition = NO_POSITION;
38
39     private int maxLength = -1;
40
41     private MMObjectBuilder parent = null;
42     private int storagePosition = -1;
43     private Object JavaDoc storageIdentifier = null;
44
45     private boolean notNull = false;
46
47     private int savedHashcode = -1;
48     private boolean hashcodeChanged = true;
49
50
51     /**
52      * Creates a CoreField object based on a more generic 'Field'.
53      * @since MMBase-1.8.1
54      */

55     public CoreField(Field field) {
56         this(field.getName(), field.getType(), field.getListItemType(), field.getState(), field.getDataType());
57     }
58     
59     /**
60      * Create a core object
61      * @param name the name of the data type
62      * @param dataType the data type for this field
63      */

64     protected CoreField(String JavaDoc name, int type, int listItemType, int state, DataType dataType) {
65         super(name, type, listItemType, state, dataType);
66         hashcodeChanged = true;
67         // obtain maxlength from datatype where applicable
68
if (dataType instanceof LengthDataType) {
69             // maxlength is an int, but LengthDataType stores longs.
70
// this ispart of the bridge, so the conflict may be hard to solve
71
// without breaking backward compatibility in the bridge
72
long length = ((LengthDataType)dataType).getMaxLength();
73             if (length > Integer.MAX_VALUE) {
74                 maxLength = Integer.MAX_VALUE;
75             } else {
76                 maxLength = (int)length;
77             }
78         }
79     }
80
81     /**
82      * Copy constructor.
83      * @param name the name of the data type
84      * @param coreField
85      */

86     protected CoreField(String JavaDoc name, CoreField coreField) {
87         super(name, coreField, true);
88         setSearchPosition(coreField.getSearchPosition());
89         setEditPosition(coreField.getEditPosition());
90         setListPosition(coreField.getListPosition());
91         setStoragePosition(coreField.getStoragePosition());
92         setParent(coreField.getParent());
93         setMaxLength(coreField.getMaxLength());
94         setUnique(coreField.isUnique());
95         hashcodeChanged = true;
96     }
97
98     public NodeManager getNodeManager() {
99         throw new UnsupportedOperationException JavaDoc("Core fields currently do not support calls to getNodeManager.");
100     }
101
102     public Object JavaDoc clone() {
103         return clone (null);
104     }
105
106     public Object JavaDoc clone(String JavaDoc name) {
107         hashcodeChanged = true;
108         return super.clone(name, true);
109     }
110
111     public void setReadOnly(boolean readOnly) {
112         hashcodeChanged = true;
113         this.readOnly = readOnly;
114     }
115
116     public void setNotNull(boolean nl) {
117         hashcodeChanged = true;
118         notNull = nl;
119     }
120
121     public boolean isNotNull() {
122         return notNull;
123     }
124
125     /**
126      * Retrieve the position of the field when searching.
127      * A value of -1 indicates the field is unavailable during search.
128      */

129     public int getSearchPosition() {
130         return searchPosition;
131     }
132
133     /**
134      * Set the position of the field when searching.
135      * @see #getSearchPosition
136      */

137     public void setSearchPosition(int i) {
138         hashcodeChanged = true;
139         searchPosition = i;
140     }
141
142     /**
143      * Retrieve the position of the field when listing.
144      * A value of -1 indicates the field is unavailable in a list.
145      */

146     public int getListPosition() {
147         return listPosition;
148     }
149
150     /**
151      * Set the position of the field when listing.
152      * @see #getListPosition
153      */

154     public void setListPosition(int i) {
155         hashcodeChanged = true;
156         listPosition = i;
157     }
158
159     /**
160      * Retrieve the position of the field when editing.
161      * A value of -1 indicates the field cannot be edited.
162      */

163     public int getEditPosition() {
164         return editPosition;
165     }
166
167     /**
168      * Set the position of the field when editing.
169      * @see #getEditPosition
170      */

171     public void setEditPosition(int i) {
172         editPosition = i;
173         hashcodeChanged = true;
174     }
175
176     /**
177      * Retrieve the position of the field in the database table.
178      */

179     public int getStoragePosition() {
180         return storagePosition;
181     }
182
183     /**
184      * Set the position of the field in the database table.
185      */

186     public void setStoragePosition(int i) {
187         storagePosition = i;
188         hashcodeChanged = true;
189     }
190
191     /**
192      * Retrieves the parent builder for this field
193      */

194     public MMObjectBuilder getParent() {
195         return parent;
196     }
197
198     /**
199      * Set the parent builder for this field
200      * @param parent the parent builder
201      */

202     public void setParent(MMObjectBuilder parent) {
203         this.parent = parent;
204         hashcodeChanged = true;
205     }
206
207     public void setState(int state) {
208         super.setState(state);
209         hashcodeChanged = true;
210     }
211
212     public void setType(int type) {
213         this.type = type;
214         hashcodeChanged = true;
215     }
216
217     public void setListItemType(int listItemType) {
218         this.listItemType = listItemType;
219         hashcodeChanged = true;
220     }
221
222     public Collection JavaDoc validate(Object JavaDoc value) {
223         Collection JavaDoc errors = getDataType().validate(value, null, this);
224         return LocalizedString.toStrings(errors, parent.getMMBase().getLocale());
225     }
226
227     /**
228      * Whether this CoreField is equal to another for storage purposes (so, ignoring gui and documentation fields)
229      * @since MMBase-1.7
230      */

231     public boolean storageEquals(CoreField f) {
232         return
233             getName().equals(f.getName())
234             && readOnly == f.isReadOnly()
235             && state == f.getState()
236             && getDataType().isRequired() == f.getDataType().isRequired()
237             && getDataType().isUnique() == f.getDataType().isUnique()
238             && maxLength == f.getMaxLength()
239             && (parent == null ? f.getParent() == null : parent.equals(f.getParent()))
240             && getStorageIdentifier().equals(f.getStorageIdentifier())
241             && getStorageType() == f.getStorageType() // implies equal MMBase types
242
&& storagePosition == f.getStoragePosition();
243     }
244
245     /**
246      * @see java.lang.Object#equals(java.lang.Object)
247      * @since MMBase-1.7
248      */

249     public boolean equals(Object JavaDoc o) {
250         if (o instanceof CoreField) {
251             CoreField f = (CoreField) o;
252             return
253                 storageEquals(f)
254                 && getLocalizedDescription().equals(f.getLocalizedDescription())
255                 && getLocalizedGUIName().equals(f.getLocalizedGUIName())
256                 && searchPosition == f.searchPosition
257                 && listPosition == f.listPosition
258                 && editPosition == f.editPosition
259                 ;
260         } else {
261             return false;
262         }
263     }
264
265     /**
266      * @see java.lang.Object#hashCode()
267      */

268     public int hashCode() {
269         if (hashcodeChanged) {
270           int result = 0;
271           result = HashCodeUtil.hashCode(result, getName());
272           result = HashCodeUtil.hashCode(result, getType());
273           result = HashCodeUtil.hashCode(result, getState());
274           result = HashCodeUtil.hashCode(result, getDataType().isRequired());
275           result = HashCodeUtil.hashCode(result, getDataType().isUnique());
276           result = HashCodeUtil.hashCode(result, parent);
277           result = HashCodeUtil.hashCode(result, storagePosition);
278           savedHashcode = result;
279           hashcodeChanged = false;
280         }
281         return savedHashcode;
282     }
283
284     /**
285      * Compare this object to the supplied one (should be a CoreField)
286      * @param o the object to compare to
287      * @return -1,1, or 0 according to wether this object is smaller, greater, or equal
288      * to the supplied one.
289      */

290     public int compareTo(Object JavaDoc o) {
291         int pos1 = getStoragePosition();
292         int pos2 = ((CoreField)o).getStoragePosition();
293         if (pos1 < pos2) {
294             return -1;
295         } else if (pos1 > pos2) {
296             return 1;
297         } else {
298             return 0;
299         }
300     }
301
302     public void finish() {
303         dataType.finish(this);
304     }
305
306     public void rewrite() {
307         dataType.rewrite(this);
308     }
309
310     /**
311      * Returns the (maximum) size of this field, as determined by the storage layer.
312      * For example if a field contains characters the size indicates the
313      * maximum number of characters it can contain.
314      * If the field is a numeric field (such as an integer), the result is -1.
315      *
316      * @return the maximum size of data this field can contain
317      */

318     public int getMaxLength() {
319         return maxLength;
320     }
321
322     public void setMaxLength(int size) {
323         this.maxLength = size;
324         if (size > 0 && (dataType instanceof LengthDataType) && size < ((LengthDataType)dataType).getMaxLength()) {
325             if (dataType.isFinished()) {
326                 dataType = (DataType) dataType.clone();
327             }
328             ((LengthDataType)dataType).setMaxLength(size);
329         }
330         hashcodeChanged = true;
331     }
332
333     public void setDataType(DataType dataType) throws IllegalArgumentException JavaDoc {
334         super.setDataType(dataType);
335         // datatype can be influenced by size
336
setMaxLength(maxLength);
337         hashcodeChanged = true;
338     }
339
340     public void setUnique(boolean unique) {
341         dataType.setUnique(unique);
342         hashcodeChanged = true;
343     }
344
345     // Storable interface
346
/**
347      * {@inheritDoc}
348      * @since MMBase 1.7
349      */

350     public Object JavaDoc getStorageIdentifier() throws StorageException {
351         // determine the storage identifier from the name
352
if (storageIdentifier == null) {
353             storageIdentifier = MMBase.getMMBase().getStorageManagerFactory().getStorageIdentifier(this);
354         }
355         return storageIdentifier;
356     }
357
358     public int getStorageType() {
359         return storageType;
360     }
361
362     public void setStorageType(int type) {
363         storageType = type;
364         hashcodeChanged = true;
365     }
366
367     public boolean inStorage() {
368         return !isVirtual();
369     }
370
371     // deprecated methods
372
/**
373      * Retrieve the GUI type of the field.
374      */

375     public String JavaDoc getGUIType() {
376         return dataType.getName();
377     }
378
379     public String JavaDoc toString() {
380         return super.toString() + (parent != null ? " of " + parent.getTableName() : " without parent");
381     }
382
383
384
385 }
386
Popular Tags