KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > DataTypeSpec


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.util.*;
59 import java.lang.reflect.*;
60 import java.math.*;
61 import java.text.*;
62 import org.apache.commons.lang.math.NumberUtils;
63 import org.apache.commons.lang.*;
64 import org.apache.commons.lang.time.*;
65
66 public class DataTypeSpec {
67   protected Map dataTypeClassMap = new HashMap();
68   protected Format dateFormat = new SimpleDateFormat(
69       "yyyy-MM-dd",
70       Locale.US);
71   protected Format dateTimeFormat = new SimpleDateFormat(
72       "yyyy-MM-dd'T'HH:mm:ss",
73       Locale.US);
74
75   public DataTypeSpec() {
76     dataTypeClassMap.put(DataTypeEnum.BOOLEAN_TYPE, Boolean JavaDoc.class);
77     dataTypeClassMap.put(DataTypeEnum.DATE_TYPE, Date.class);
78     dataTypeClassMap.put(DataTypeEnum.DATETIME_TYPE, Date.class);
79     dataTypeClassMap.put(DataTypeEnum.DOUBLE_TYPE, Double JavaDoc.class);
80     dataTypeClassMap.put(DataTypeEnum.INTEGER_TYPE, Long JavaDoc.class);
81     dataTypeClassMap.put(DataTypeEnum.MONEY_TYPE, Double JavaDoc.class);
82     dataTypeClassMap.put(DataTypeEnum.PERCENT_TYPE, Double JavaDoc.class);
83     dataTypeClassMap.put(DataTypeEnum.STRING_TYPE, String JavaDoc.class);
84     dataTypeClassMap.put(DataTypeEnum.OBJECT_TYPE, Object JavaDoc.class);
85     dataTypeClassMap.put(DataTypeEnum.UNKNOWN_TYPE, null);
86   }
87
88   public DataTypeEnum getDataType(String JavaDoc dataType) {
89     return DataTypeEnum.getEnum(dataType);
90   }
91
92   public DataTypeEnum getDataType(int dataType) {
93     return DataTypeEnum.getEnum(dataType);
94   }
95
96   public Class JavaDoc getJavaClass(DataTypeEnum dataType) {
97     return (Class JavaDoc)dataTypeClassMap.get(dataType);
98   }
99
100   public Object JavaDoc create(DataTypeEnum dataType) {
101     Class JavaDoc clazz = getJavaClass(dataType);
102     if (clazz != null) {
103       try {
104         Object JavaDoc value = clazz.newInstance();
105         if (DataTypeEnum.DATE_TYPE.equals(dataType))
106           value = DateUtils.truncate(value, Calendar.DATE);
107         return value;
108       }
109       catch (InstantiationException JavaDoc ex) {
110         return null;
111       }catch (IllegalAccessException JavaDoc ex) {
112         return null;
113       }
114     }
115     return null;
116   }
117
118   public Object JavaDoc create(DataTypeEnum dataType, String JavaDoc argument) {
119     Class JavaDoc clazz = getJavaClass(dataType);
120     if (clazz != null) {
121       try {
122         if (DataTypeEnum.DATE_TYPE.equals(dataType)) {
123           return dateFormat.parseObject(argument);
124         } else if (DataTypeEnum.DATETIME_TYPE.equals(dataType)) {
125           return dateTimeFormat.parseObject(argument);
126         }
127         Constructor strConstructor = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
128         return strConstructor.newInstance(new Object JavaDoc[] {argument});
129       }
130       catch (ParseException ex) {
131         return null;
132       }
133       catch (NoSuchMethodException JavaDoc ex) {
134         return null;
135       }
136       catch (InvocationTargetException ex) {
137         return null;
138       }
139       catch (InstantiationException JavaDoc ex) {
140         return null;
141       }catch (IllegalAccessException JavaDoc ex) {
142         return null;
143       }
144     }
145     return null;
146   }
147
148   public Object JavaDoc create(DataTypeEnum dataType, Object JavaDoc[] arguments) {
149     if (arguments == null || arguments.length == 0)
150       return create(dataType);
151
152     Class JavaDoc clazz = getJavaClass(dataType);
153     if (clazz != null) {
154       try {
155         Class JavaDoc[] argTypes = new Class JavaDoc[arguments.length];
156         for (int i = 0; i < arguments.length; i++) {
157           argTypes[i] = arguments[i].getClass();
158         }
159
160         Constructor strConstructor = clazz.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
161         return strConstructor.newInstance(arguments);
162       }
163       catch (NoSuchMethodException JavaDoc ex) {
164         return null;
165       }
166       catch (InvocationTargetException ex) {
167         return null;
168       }
169       catch (InstantiationException JavaDoc ex) {
170         return null;
171       }catch (IllegalAccessException JavaDoc ex) {
172         return null;
173       }
174     }
175     return null;
176   }
177
178   public Object JavaDoc toDataType(DataTypeEnum dataType, Object JavaDoc untypedValue) {
179     Class JavaDoc dataTypeClass = getJavaClass(dataType);
180     if (dataTypeClass == null ||
181         untypedValue == null ||
182         ClassUtils.isAssignable(untypedValue.getClass(), dataTypeClass)) {
183       if (DataTypeEnum.DATE_TYPE.equals(dataType) &&
184           Date.class.equals(dataTypeClass)) {
185         return DateUtils.truncate(untypedValue, Calendar.DATE);
186       }
187       return untypedValue;
188     }
189
190     Object JavaDoc v = null;
191     String JavaDoc strUntypedValue = null;
192     boolean isStringUntypedValue = untypedValue instanceof String JavaDoc;
193     Number JavaDoc numUntypedValue = null;
194     boolean isNumberUntypedValue = untypedValue instanceof Number JavaDoc;
195     if (isStringUntypedValue)
196       strUntypedValue = (String JavaDoc)untypedValue;
197     if (isNumberUntypedValue)
198       numUntypedValue = (Number JavaDoc)untypedValue;
199
200     switch (dataType.getValue()) {
201       case DataTypeEnum.BOOLEAN_TYPE_VALUE:
202         if (isNumberUntypedValue)
203           v = BooleanUtils.toBooleanObject(numUntypedValue.intValue());
204         else if (isStringUntypedValue)
205           v = BooleanUtils.toBooleanObject(strUntypedValue);
206         break;
207       case DataTypeEnum.INTEGER_TYPE_VALUE:
208         if (isNumberUntypedValue)
209           v = new Integer JavaDoc(numUntypedValue.intValue());
210         else if (isStringUntypedValue)
211           v = NumberUtils.createInteger(strUntypedValue);
212         break;
213       case DataTypeEnum.DOUBLE_TYPE_VALUE:
214         if (isNumberUntypedValue)
215           v = new Double JavaDoc(numUntypedValue.doubleValue());
216         else if (isStringUntypedValue)
217           v = NumberUtils.createDouble(strUntypedValue);
218         break;
219       case DataTypeEnum.STRING_TYPE_VALUE:
220         v = ObjectUtils.toString(untypedValue);
221         break;
222       case DataTypeEnum.DATE_TYPE_VALUE:
223         if (isNumberUntypedValue)
224           v = DateUtils.truncate(new Date(numUntypedValue.longValue()), Calendar.DATE);
225         break;
226       case DataTypeEnum.DATETIME_TYPE_VALUE:
227         if (isNumberUntypedValue)
228           v = new Date(numUntypedValue.longValue());
229         break;
230       case DataTypeEnum.MONEY_TYPE_VALUE:
231         if (isNumberUntypedValue)
232           v = new Double JavaDoc(numUntypedValue.doubleValue());
233         else if (isStringUntypedValue)
234           v = NumberUtils.createDouble(strUntypedValue);
235         break;
236       case DataTypeEnum.PERCENT_TYPE_VALUE:
237         if (isNumberUntypedValue)
238           v = new Double JavaDoc(numUntypedValue.doubleValue());
239         else if (isStringUntypedValue)
240           v = NumberUtils.createDouble(strUntypedValue);
241         break;
242     }
243     return v;
244   }
245
246   public Object JavaDoc fromDataType(
247       Class JavaDoc untypedValueClass,
248       DataTypeEnum dataType,
249       Object JavaDoc typedValue) {
250     if (typedValue == null)
251       return null;
252     Class JavaDoc dataTypeClass = getJavaClass(dataType);
253 // Validate.isTrue(typedValue.getClass().equals(dataTypeClass));
254

255     if (untypedValueClass == null)
256       return typedValue;
257
258     if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass))
259       return typedValue;
260
261     String JavaDoc strTypedValue = null;
262     boolean isStringTypedValue = typedValue instanceof String JavaDoc;
263     Number JavaDoc numTypedValue = null;
264     boolean isNumberTypedValue = typedValue instanceof Number JavaDoc;
265     Boolean JavaDoc boolTypedValue = null;
266     boolean isBooleanTypedValue = typedValue instanceof Boolean JavaDoc;
267     Date dateTypedValue = null;
268     boolean isDateTypedValue = typedValue instanceof Date;
269
270     if (isStringTypedValue)
271       strTypedValue = (String JavaDoc)typedValue;
272     if (isNumberTypedValue)
273       numTypedValue = (Number JavaDoc)typedValue;
274     if (isBooleanTypedValue)
275       boolTypedValue = (Boolean JavaDoc)typedValue;
276     if (isDateTypedValue)
277       dateTypedValue = (Date)typedValue;
278
279     Object JavaDoc v = null;
280     if (String JavaDoc.class.equals(untypedValueClass)) {
281       v = ObjectUtils.toString(typedValue);
282     } else if (BigDecimal.class.equals(untypedValueClass)) {
283       if (isStringTypedValue)
284         v = NumberUtils.createBigDecimal(strTypedValue);
285       else if (isNumberTypedValue)
286         v = new BigDecimal(numTypedValue.doubleValue());
287       else if (isBooleanTypedValue)
288         v = new BigDecimal(
289             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
290       else if (isDateTypedValue)
291         v = new BigDecimal(dateTypedValue.getTime());
292     } else if (Boolean JavaDoc.class.equals(untypedValueClass)) {
293       if (isStringTypedValue)
294         v = BooleanUtils.toBooleanObject(strTypedValue);
295       else if (isNumberTypedValue)
296         v = BooleanUtils.toBooleanObject(numTypedValue.intValue());
297       else if (isDateTypedValue)
298         v = BooleanUtils.toBooleanObject((int)dateTypedValue.getTime());
299     } else if (Byte JavaDoc.class.equals(untypedValueClass)) {
300       if (isStringTypedValue)
301         v = Byte.valueOf(strTypedValue);
302       else if (isNumberTypedValue)
303         v = new Byte JavaDoc(numTypedValue.byteValue());
304       else if (isBooleanTypedValue)
305         v = new Byte JavaDoc(
306             (byte)BooleanUtils.toInteger(boolTypedValue.booleanValue()));
307       else if (isDateTypedValue)
308         v = new Byte JavaDoc((byte)dateTypedValue.getTime());
309     } else if (byte[].class.equals(untypedValueClass)) {
310       if (isStringTypedValue)
311         v = strTypedValue.getBytes();
312     } else if (Double JavaDoc.class.equals(untypedValueClass)) {
313       if (isStringTypedValue)
314         v = NumberUtils.createDouble(strTypedValue);
315       else if (isNumberTypedValue)
316         v = new Double JavaDoc(numTypedValue.doubleValue());
317       else if (isBooleanTypedValue)
318         v = new Double JavaDoc(
319             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
320       else if (isDateTypedValue)
321         v = new Double JavaDoc(dateTypedValue.getTime());
322     } else if (Float JavaDoc.class.equals(untypedValueClass)) {
323       if (isStringTypedValue)
324         v = NumberUtils.createFloat(strTypedValue);
325       else if (isNumberTypedValue)
326         v = new Float JavaDoc(numTypedValue.floatValue());
327       else if (isBooleanTypedValue)
328         v = new Float JavaDoc(
329             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
330       else if (isDateTypedValue)
331         v = new Float JavaDoc(dateTypedValue.getTime());
332     } else if (Integer JavaDoc.class.equals(untypedValueClass)) {
333       if (isStringTypedValue)
334         v = NumberUtils.createInteger(strTypedValue);
335       else if (isNumberTypedValue)
336         v = new Integer JavaDoc(numTypedValue.intValue());
337       else if (isBooleanTypedValue)
338         v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue());
339       else if (isDateTypedValue)
340         v = new Integer JavaDoc((int)dateTypedValue.getTime());
341     } else if (Long JavaDoc.class.equals(untypedValueClass)) {
342       if (isStringTypedValue)
343         v = NumberUtils.createLong(strTypedValue);
344       else if (isNumberTypedValue)
345         v = new Long JavaDoc(numTypedValue.longValue());
346       else if (isBooleanTypedValue)
347         v = new Long JavaDoc(
348             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
349       else if (isDateTypedValue)
350         v = new Long JavaDoc(dateTypedValue.getTime());
351     } else if (java.sql.Date JavaDoc.class.equals(untypedValueClass)) {
352       if (isNumberTypedValue)
353         v = new java.sql.Date JavaDoc(numTypedValue.longValue());
354       else if (isDateTypedValue)
355         v = new java.sql.Date JavaDoc(dateTypedValue.getTime());
356     } else if (java.sql.Time JavaDoc.class.equals(untypedValueClass)) {
357       if (isNumberTypedValue)
358         v = new java.sql.Time JavaDoc(numTypedValue.longValue());
359       else if (isDateTypedValue)
360         v = new java.sql.Time JavaDoc(dateTypedValue.getTime());
361     } else if (java.sql.Timestamp JavaDoc.class.equals(untypedValueClass)) {
362       if (isNumberTypedValue)
363         v = new java.sql.Timestamp JavaDoc(numTypedValue.longValue());
364       else if (isDateTypedValue)
365         v = new java.sql.Timestamp JavaDoc(dateTypedValue.getTime());
366     } else if (Date.class.equals(untypedValueClass)) {
367       if (isNumberTypedValue)
368         v = new Date(numTypedValue.longValue());
369     }
370     return v;
371   }
372 }
373
Popular Tags