KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > util > Fields


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.util;
11
12 import org.mmbase.bridge.*;
13 import org.mmbase.datatypes.*;
14 import org.mmbase.core.CoreField;
15 import java.util.*;
16
17 /**
18
19  * @since MMBase-1.8
20  */

21 public class Fields {
22     public final static int STATE_MINVALUE = 0;
23     public final static int STATE_MAXVALUE = 3;
24     private final static String JavaDoc[] STATES = {
25         "unknown", "virtual", "unknown", "persistent", "system", "systemvirtual"
26     };
27
28     public final static int TYPE_MINVALUE = 1;
29     public final static int TYPE_MAXVALUE = 12;
30     private final static String JavaDoc[] TYPES = {
31         "UNKNOWN", "STRING", "INTEGER", "UNKNOWN", "BINARY" /* BYTE */, "FLOAT", "DOUBLE", "LONG", "XML", "NODE", "DATETIME", "BOOLEAN", "LIST"
32     };
33
34     /**
35      * Returns an instance of a CoreField based on the type, with state 'SYSTEM', and a basic datatype assigned.
36      * @param name The name of the field
37      * @param type the MMBase basic field type, one of the {@link Field} TYPE constants. Specifying {@link Field#TYPE_LIST},
38      * may give unpredictable results.
39      */

40     public static CoreField createSystemField(String JavaDoc name, int type) {
41         return createField(name, type, Field.TYPE_UNKNOWN, Field.STATE_SYSTEM, null);
42     }
43
44     /**
45      * Returns an instance of a CoreField based on the type and state.
46      * @param name The name of the field
47      * @param type the MMBase basic field type, one of the {@link Field} TYPE constants.
48      * @param listItemType the MMBase type for items of a list (if type is {@link Field#TYPE_LIST}).
49      * @param state the MMBase field state, one of the {@link Field} STATE constants.
50      * @param dataType the dataType to use for validating the field data. If <code>null</code>, a default datatype is assigned
51      */

52     public static CoreField createField(String JavaDoc name, int type, int listItemType, int state, DataType dataType) {
53         if (dataType == null) {
54             if (type == Field.TYPE_LIST) {
55                 dataType = (DataType)DataTypes.getListDataType(listItemType).clone();
56             } else {
57                 dataType = (DataType)DataTypes.getDataType(type).clone();
58             }
59         }
60         return new org.mmbase.module.corebuilders.FieldDefs(name, type, listItemType, state, dataType);
61     }
62
63     /**
64      * Provide a description for the specified type.
65      * Useful for debugging, errors or presenting GUI info.
66      * @param type the type to get the description of
67      * @return the description of the type.
68      */

69     public static String JavaDoc getTypeDescription(int type) {
70        if (type < TYPE_MINVALUE || type > TYPE_MAXVALUE) {
71             return TYPES[0];
72        }
73
74        return TYPES[type - TYPE_MINVALUE + 1];
75     }
76
77     /**
78      * Provide a description for the specified state.
79      * Useful for debugging, errors or presenting GUI info.
80      * @param state the state to get the description of
81      * @return the description of the state.
82      */

83     public static String JavaDoc getStateDescription(int state) {
84        if (state < STATE_MINVALUE || state > STATE_MAXVALUE) {
85             return STATES[0];
86        }
87        return STATES[state - STATE_MINVALUE + 1];
88     }
89
90
91
92     /**
93      * Provide an id for the specified mmbase state description.
94      * @param state the state description to get the id of
95      * @return the id of the state.
96      */

97     public static int getState(String JavaDoc state) {
98         if (state == null) return Field.STATE_UNKNOWN;
99         state = state.toLowerCase();
100         if (state.equals("persistent")) return Field.STATE_PERSISTENT;
101         if (state.equals("virtual")) return Field.STATE_VIRTUAL;
102         if (state.equals("systemvirtual")) return Field.STATE_SYSTEM_VIRTUAL;
103         if (state.equals("system")) return Field.STATE_SYSTEM;
104         return Field.STATE_UNKNOWN;
105     }
106
107     /**
108      * Provide an id for the specified mmbase type description
109      * @param type the type description to get the id of
110      * @return the id of the type.
111      */

112     public static int getType(String JavaDoc type) {
113         if (type == null) return Field.TYPE_UNKNOWN;
114         // XXX: deprecated VARCHAR
115
type = type.toUpperCase();
116         if (type.equals("VARCHAR")) return Field.TYPE_STRING;
117         if (type.equals("STRING")) return Field.TYPE_STRING;
118         if (type.equals("XML")) return Field.TYPE_XML;
119         if (type.equals("INTEGER")) return Field.TYPE_INTEGER;
120         if (type.equals("BYTE")) return Field.TYPE_BINARY;
121         if (type.equals("BINARY")) return Field.TYPE_BINARY;
122         if (type.equals("FLOAT")) return Field.TYPE_FLOAT;
123         if (type.equals("DOUBLE")) return Field.TYPE_DOUBLE;
124         if (type.equals("LONG")) return Field.TYPE_LONG;
125         if (type.equals("NODE")) return Field.TYPE_NODE;
126         if (type.equals("DATETIME"))return Field.TYPE_DATETIME;
127         if (type.equals("BOOLEAN")) return Field.TYPE_BOOLEAN;
128         if (type.startsWith("LIST")) return Field.TYPE_LIST;
129         return Field.TYPE_UNKNOWN;
130     }
131
132     /**
133      * Determines the MMBase type of a specified class. The MMBase base type is sue by the storage layer to
134      * determine how to store a field.
135      * If the base type cannot be determined from the class, the value returned is {@link Field#TYPE_UNKNOWN}.
136      * @param classType
137      * @return an MMBase base type constant
138      */

139     public static int classToType(Class JavaDoc classType) {
140         if (classType == null) {
141             return Field.TYPE_UNKNOWN;
142         } else if (classType.isArray() && classType.getComponentType() == Byte.TYPE) {
143             return Field.TYPE_BINARY;
144         } else if (classType == Integer JavaDoc.class || classType == Integer.TYPE) {
145             return Field.TYPE_INTEGER;
146         } else if (classType == Long JavaDoc.class || classType == Long.TYPE) {
147             return Field.TYPE_LONG;
148         } else if (classType == Double JavaDoc.class || classType == Double.TYPE) {
149             return Field.TYPE_DOUBLE;
150         } else if (classType == Float JavaDoc.class || classType == Float.TYPE) {
151             return Field.TYPE_FLOAT;
152         } else if (classType == Boolean JavaDoc.class || classType == Boolean.TYPE) {
153             return Field.TYPE_BOOLEAN;
154         } else if (classType == String JavaDoc.class) {
155             return Field.TYPE_STRING;
156         } else if (org.w3c.dom.Node JavaDoc.class.isAssignableFrom(classType)) {
157             return Field.TYPE_XML;
158         } else if (Node.class.isAssignableFrom(classType)) {
159             return Field.TYPE_NODE;
160         } else if (Date.class.isAssignableFrom(classType)) {
161             return Field.TYPE_DATETIME;
162         } else if (List.class.isAssignableFrom(classType)) {
163             return Field.TYPE_LIST;
164         } else {
165             return Field.TYPE_UNKNOWN;
166         }
167     }
168
169     /**
170      * Determines the class for a specified MMBase base type.
171      * If the value is {@link Field#TYPE_UNKNOWN}), the method returns <code>null</code>.
172      * @param type
173      * @return an MMBase base type constant
174      */

175     public static Class JavaDoc typeToClass(int type) {
176         switch (type) {
177         case Field.TYPE_STRING : return String JavaDoc.class;
178         case Field.TYPE_INTEGER : return Integer JavaDoc.class;
179         case Field.TYPE_BINARY: return byte[].class;
180         case Field.TYPE_FLOAT: return Float JavaDoc.class;
181         case Field.TYPE_DOUBLE: return Double JavaDoc.class;
182         case Field.TYPE_LONG: return Long JavaDoc.class;
183         case Field.TYPE_XML: return org.w3c.dom.Document JavaDoc.class;
184         case Field.TYPE_NODE: return Node.class;
185         case Field.TYPE_DATETIME: return java.util.Date JavaDoc.class;
186         case Field.TYPE_BOOLEAN: return Boolean JavaDoc.class;
187         case Field.TYPE_LIST: return List.class;
188         default: return null;
189         }
190     }
191
192
193     public static void sort(List fields, int order) {
194         Collections.sort(fields, new FieldComparator(order));
195     }
196
197
198     /**
199      * Comparator to sort CoreFields by creation order, or by position
200      * specified in one of the GUIPos fields.
201      */

202     private static class FieldComparator implements Comparator {
203
204         private int order = NodeManager.ORDER_CREATE;
205
206         /**
207          * Constrcuts a comparator to sort fields on teh specifie dorder
208          * @param order one of NodeManager.ORDER_CREATE, NodeManager.ORDER_EDIT, NodeManager.ORDER_LIST, NodeManager.ORDER_SEARCH
209          */

210         FieldComparator (int order) {
211             this.order = order;
212         }
213
214         /**
215          * Retrieve the postion of a CoreField object according to the order to sort on
216          */

217         private int getPos(CoreField o) {
218             switch (order) {
219             case NodeManager.ORDER_EDIT: {
220                 return o.getEditPosition();
221             }
222             case NodeManager.ORDER_LIST: {
223                 return o.getListPosition();
224             }
225             case NodeManager.ORDER_SEARCH: {
226                 return o.getSearchPosition();
227             }
228             default : {
229                 return o.getStoragePosition();
230             }
231             }
232         }
233
234         /**
235          * Compare two objects (should be CoreFields)
236          */

237         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
238             int pos1 = getPos((CoreField)o1);
239             int pos2 = getPos((CoreField)o2);
240
241             if (pos1 < pos2) {
242                 return -1;
243             } else if (pos1 > pos2) {
244                 return 1;
245             } else {
246                 return 0;
247             }
248         }
249     }
250
251
252
253 }
254
Popular Tags