1 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 .class); 41 dataTypeClassMap.put(DataTypeEnum.DATE_TYPE, Date.class); 42 dataTypeClassMap.put(DataTypeEnum.DATETIME_TYPE, Date.class); 43 dataTypeClassMap.put(DataTypeEnum.DOUBLE_TYPE, Double .class); 44 dataTypeClassMap.put(DataTypeEnum.INTEGER_TYPE, Long .class); 45 dataTypeClassMap.put(DataTypeEnum.MONEY_TYPE, Double .class); 46 dataTypeClassMap.put(DataTypeEnum.PERCENT_TYPE, Double .class); 47 dataTypeClassMap.put(DataTypeEnum.STRING_TYPE, String .class); 48 dataTypeClassMap.put(DataTypeEnum.OBJECT_TYPE, Object .class); 49 dataTypeClassMap.put(DataTypeEnum.UNKNOWN_TYPE, null); 50 } 51 52 public DataTypeEnum getDataType(String dataType) { 53 return DataTypeEnum.getEnum(dataType); 54 } 55 56 public DataTypeEnum getDataType(int dataType) { 57 return DataTypeEnum.getEnum(dataType); 58 } 59 60 public Class getJavaClass(DataTypeEnum dataType) { 61 return (Class )dataTypeClassMap.get(dataType); 62 } 63 64 public Object create(DataTypeEnum dataType) { 65 Class clazz = getJavaClass(dataType); 66 if (clazz != null) { 67 try { 68 Object value = clazz.newInstance(); 69 if (DataTypeEnum.DATE_TYPE.equals(dataType)) 70 value = DateUtils.truncate(value, Calendar.DATE); 71 return value; 72 } 73 catch (InstantiationException ex) { 74 return null; 75 }catch (IllegalAccessException ex) { 76 return null; 77 } 78 } 79 return null; 80 } 81 82 public Object create(DataTypeEnum dataType, String argument) { 83 Class 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 [] {String .class}); 92 return strConstructor.newInstance(new Object [] {argument}); 93 } 94 catch (ParseException ex) { 95 return null; 96 } 97 catch (NoSuchMethodException ex) { 98 return null; 99 } 100 catch (InvocationTargetException ex) { 101 return null; 102 } 103 catch (InstantiationException ex) { 104 return null; 105 }catch (IllegalAccessException ex) { 106 return null; 107 } 108 } 109 return null; 110 } 111 112 public Object create(DataTypeEnum dataType, Object [] arguments) { 113 if (arguments == null || arguments.length == 0) 114 return create(dataType); 115 116 Class clazz = getJavaClass(dataType); 117 if (clazz != null) { 118 try { 119 Class [] argTypes = new Class [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 [] {String .class}); 125 return strConstructor.newInstance(arguments); 126 } 127 catch (NoSuchMethodException ex) { 128 return null; 129 } 130 catch (InvocationTargetException ex) { 131 return null; 132 } 133 catch (InstantiationException ex) { 134 return null; 135 }catch (IllegalAccessException ex) { 136 return null; 137 } 138 } 139 return null; 140 } 141 142 public Object toDataType(DataTypeEnum dataType, Object untypedValue) { 143 Class 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 v = null; 155 String strUntypedValue = null; 156 boolean isStringUntypedValue = untypedValue instanceof String ; 157 Number numUntypedValue = null; 158 boolean isNumberUntypedValue = untypedValue instanceof Number ; 159 if (isStringUntypedValue) 160 strUntypedValue = (String )untypedValue; 161 if (isNumberUntypedValue) 162 numUntypedValue = (Number )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 (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 (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 (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 (numUntypedValue.doubleValue()); 203 else if (isStringUntypedValue) 204 v = NumberUtils.createDouble(strUntypedValue); 205 break; 206 } 207 return v; 208 } 209 210 public Object fromDataType( 211 Class untypedValueClass, 212 DataTypeEnum dataType, 213 Object typedValue) { 214 if (typedValue == null) 215 return null; 216 Class dataTypeClass = getJavaClass(dataType); 217 219 if (untypedValueClass == null) 220 return typedValue; 221 222 if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass)) 223 return typedValue; 224 225 String strTypedValue = null; 226 boolean isStringTypedValue = typedValue instanceof String ; 227 Number numTypedValue = null; 228 boolean isNumberTypedValue = typedValue instanceof Number ; 229 Boolean boolTypedValue = null; 230 boolean isBooleanTypedValue = typedValue instanceof Boolean ; 231 Date dateTypedValue = null; 232 boolean isDateTypedValue = typedValue instanceof Date; 233 234 if (isStringTypedValue) 235 strTypedValue = (String )typedValue; 236 if (isNumberTypedValue) 237 numTypedValue = (Number )typedValue; 238 if (isBooleanTypedValue) 239 boolTypedValue = (Boolean )typedValue; 240 if (isDateTypedValue) 241 dateTypedValue = (Date)typedValue; 242 243 Object v = null; 244 if (String .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 .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 .class.equals(untypedValueClass)) { 264 if (isStringTypedValue) 265 v = Byte.valueOf(strTypedValue); 266 else if (isNumberTypedValue) 267 v = new Byte (numTypedValue.byteValue()); 268 else if (isBooleanTypedValue) 269 v = new Byte ( 270 (byte)BooleanUtils.toInteger(boolTypedValue.booleanValue())); 271 else if (isDateTypedValue) 272 v = new Byte ((byte)dateTypedValue.getTime()); 273 } else if (byte[].class.equals(untypedValueClass)) { 274 if (isStringTypedValue) 275 v = strTypedValue.getBytes(); 276 } else if (Double .class.equals(untypedValueClass)) { 277 if (isStringTypedValue) 278 v = NumberUtils.createDouble(strTypedValue); 279 else if (isNumberTypedValue) 280 v = new Double (numTypedValue.doubleValue()); 281 else if (isBooleanTypedValue) 282 v = new Double ( 283 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 284 else if (isDateTypedValue) 285 v = new Double (dateTypedValue.getTime()); 286 } else if (Float .class.equals(untypedValueClass)) { 287 if (isStringTypedValue) 288 v = NumberUtils.createFloat(strTypedValue); 289 else if (isNumberTypedValue) 290 v = new Float (numTypedValue.floatValue()); 291 else if (isBooleanTypedValue) 292 v = new Float ( 293 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 294 else if (isDateTypedValue) 295 v = new Float (dateTypedValue.getTime()); 296 } else if (Integer .class.equals(untypedValueClass)) { 297 if (isStringTypedValue) 298 v = NumberUtils.createInteger(strTypedValue); 299 else if (isNumberTypedValue) 300 v = new Integer (numTypedValue.intValue()); 301 else if (isBooleanTypedValue) 302 v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue()); 303 else if (isDateTypedValue) 304 v = new Integer ((int)dateTypedValue.getTime()); 305 } else if (Long .class.equals(untypedValueClass)) { 306 if (isStringTypedValue) 307 v = NumberUtils.createLong(strTypedValue); 308 else if (isNumberTypedValue) 309 v = new Long (numTypedValue.longValue()); 310 else if (isBooleanTypedValue) 311 v = new Long ( 312 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 313 else if (isDateTypedValue) 314 v = new Long (dateTypedValue.getTime()); 315 } else if (java.sql.Date .class.equals(untypedValueClass)) { 316 if (isNumberTypedValue) 317 v = new java.sql.Date (numTypedValue.longValue()); 318 else if (isDateTypedValue) 319 v = new java.sql.Date (dateTypedValue.getTime()); 320 } else if (java.sql.Time .class.equals(untypedValueClass)) { 321 if (isNumberTypedValue) 322 v = new java.sql.Time (numTypedValue.longValue()); 323 else if (isDateTypedValue) 324 v = new java.sql.Time (dateTypedValue.getTime()); 325 } else if (java.sql.Timestamp .class.equals(untypedValueClass)) { 326 if (isNumberTypedValue) 327 v = new java.sql.Timestamp (numTypedValue.longValue()); 328 else if (isDateTypedValue) 329 v = new java.sql.Timestamp (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 |