KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

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

219     if (untypedValueClass == null)
220       return typedValue;
221
222     if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass))
223       return typedValue;
224
225     String JavaDoc strTypedValue = null;
226     boolean isStringTypedValue = typedValue instanceof String JavaDoc;
227     Number JavaDoc numTypedValue = null;
228     boolean isNumberTypedValue = typedValue instanceof Number JavaDoc;
229     Boolean JavaDoc boolTypedValue = null;
230     boolean isBooleanTypedValue = typedValue instanceof Boolean JavaDoc;
231     Date dateTypedValue = null;
232     boolean isDateTypedValue = typedValue instanceof Date;
233
234     if (isStringTypedValue)
235       strTypedValue = (String JavaDoc)typedValue;
236     if (isNumberTypedValue)
237       numTypedValue = (Number JavaDoc)typedValue;
238     if (isBooleanTypedValue)
239       boolTypedValue = (Boolean JavaDoc)typedValue;
240     if (isDateTypedValue)
241       dateTypedValue = (Date)typedValue;
242
243     Object JavaDoc v = null;
244     if (String JavaDoc.class.equals(untypedValueClass)) {
245       v = ObjectUtils.toString(typedValue);
246     } else if (BigDecimal.class.equals(untypedValueClass)) {
247       if (isStringTypedValue)
248         v = NumberUtils.createBigDecimal(strTypedValue);
249       else if (isNumberTypedValue)
250         v = new BigDecimal(numTypedValue.doubleValue());
251       else if (isBooleanTypedValue)
252         v = new BigDecimal(
253             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
254       else if (isDateTypedValue)
255         v = new BigDecimal(dateTypedValue.getTime());
256     } else if (Boolean JavaDoc.class.equals(untypedValueClass)) {
257       if (isStringTypedValue)
258         v = BooleanUtils.toBooleanObject(strTypedValue);
259       else if (isNumberTypedValue)
260         v = BooleanUtils.toBooleanObject(numTypedValue.intValue());
261       else if (isDateTypedValue)
262         v = BooleanUtils.toBooleanObject((int)dateTypedValue.getTime());
263     } else if (Byte JavaDoc.class.equals(untypedValueClass)) {
264       if (isStringTypedValue)
265         v = Byte.valueOf(strTypedValue);
266       else if (isNumberTypedValue)
267         v = new Byte JavaDoc(numTypedValue.byteValue());
268       else if (isBooleanTypedValue)
269         v = new Byte JavaDoc(
270             (byte)BooleanUtils.toInteger(boolTypedValue.booleanValue()));
271       else if (isDateTypedValue)
272         v = new Byte JavaDoc((byte)dateTypedValue.getTime());
273     } else if (byte[].class.equals(untypedValueClass)) {
274       if (isStringTypedValue)
275         v = strTypedValue.getBytes();
276     } else if (Double JavaDoc.class.equals(untypedValueClass)) {
277       if (isStringTypedValue)
278         v = NumberUtils.createDouble(strTypedValue);
279       else if (isNumberTypedValue)
280         v = new Double JavaDoc(numTypedValue.doubleValue());
281       else if (isBooleanTypedValue)
282         v = new Double JavaDoc(
283             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
284       else if (isDateTypedValue)
285         v = new Double JavaDoc(dateTypedValue.getTime());
286     } else if (Float JavaDoc.class.equals(untypedValueClass)) {
287       if (isStringTypedValue)
288         v = NumberUtils.createFloat(strTypedValue);
289       else if (isNumberTypedValue)
290         v = new Float JavaDoc(numTypedValue.floatValue());
291       else if (isBooleanTypedValue)
292         v = new Float JavaDoc(
293             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
294       else if (isDateTypedValue)
295         v = new Float JavaDoc(dateTypedValue.getTime());
296     } else if (Integer JavaDoc.class.equals(untypedValueClass)) {
297       if (isStringTypedValue)
298         v = NumberUtils.createInteger(strTypedValue);
299       else if (isNumberTypedValue)
300         v = new Integer JavaDoc(numTypedValue.intValue());
301       else if (isBooleanTypedValue)
302         v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue());
303       else if (isDateTypedValue)
304         v = new Integer JavaDoc((int)dateTypedValue.getTime());
305     } else if (Long JavaDoc.class.equals(untypedValueClass)) {
306       if (isStringTypedValue)
307         v = NumberUtils.createLong(strTypedValue);
308       else if (isNumberTypedValue)
309         v = new Long JavaDoc(numTypedValue.longValue());
310       else if (isBooleanTypedValue)
311         v = new Long JavaDoc(
312             BooleanUtils.toInteger(boolTypedValue.booleanValue()));
313       else if (isDateTypedValue)
314         v = new Long JavaDoc(dateTypedValue.getTime());
315     } else if (java.sql.Date JavaDoc.class.equals(untypedValueClass)) {
316       if (isNumberTypedValue)
317         v = new java.sql.Date JavaDoc(numTypedValue.longValue());
318       else if (isDateTypedValue)
319         v = new java.sql.Date JavaDoc(dateTypedValue.getTime());
320     } else if (java.sql.Time JavaDoc.class.equals(untypedValueClass)) {
321       if (isNumberTypedValue)
322         v = new java.sql.Time JavaDoc(numTypedValue.longValue());
323       else if (isDateTypedValue)
324         v = new java.sql.Time JavaDoc(dateTypedValue.getTime());
325     } else if (java.sql.Timestamp JavaDoc.class.equals(untypedValueClass)) {
326       if (isNumberTypedValue)
327         v = new java.sql.Timestamp JavaDoc(numTypedValue.longValue());
328       else if (isDateTypedValue)
329         v = new java.sql.Timestamp JavaDoc(dateTypedValue.getTime());
330     } else if (Date.class.equals(untypedValueClass)) {
331       if (isNumberTypedValue)
332         v = new Date(numTypedValue.longValue());
333     }
334     return v;
335   }
336 }
337
Popular Tags