1 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 .class); 77 dataTypeClassMap.put(DataTypeEnum.DATE_TYPE, Date.class); 78 dataTypeClassMap.put(DataTypeEnum.DATETIME_TYPE, Date.class); 79 dataTypeClassMap.put(DataTypeEnum.DOUBLE_TYPE, Double .class); 80 dataTypeClassMap.put(DataTypeEnum.INTEGER_TYPE, Long .class); 81 dataTypeClassMap.put(DataTypeEnum.MONEY_TYPE, Double .class); 82 dataTypeClassMap.put(DataTypeEnum.PERCENT_TYPE, Double .class); 83 dataTypeClassMap.put(DataTypeEnum.STRING_TYPE, String .class); 84 dataTypeClassMap.put(DataTypeEnum.OBJECT_TYPE, Object .class); 85 dataTypeClassMap.put(DataTypeEnum.UNKNOWN_TYPE, null); 86 } 87 88 public DataTypeEnum getDataType(String dataType) { 89 return DataTypeEnum.getEnum(dataType); 90 } 91 92 public DataTypeEnum getDataType(int dataType) { 93 return DataTypeEnum.getEnum(dataType); 94 } 95 96 public Class getJavaClass(DataTypeEnum dataType) { 97 return (Class )dataTypeClassMap.get(dataType); 98 } 99 100 public Object create(DataTypeEnum dataType) { 101 Class clazz = getJavaClass(dataType); 102 if (clazz != null) { 103 try { 104 Object value = clazz.newInstance(); 105 if (DataTypeEnum.DATE_TYPE.equals(dataType)) 106 value = DateUtils.truncate(value, Calendar.DATE); 107 return value; 108 } 109 catch (InstantiationException ex) { 110 return null; 111 }catch (IllegalAccessException ex) { 112 return null; 113 } 114 } 115 return null; 116 } 117 118 public Object create(DataTypeEnum dataType, String argument) { 119 Class 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 [] {String .class}); 128 return strConstructor.newInstance(new Object [] {argument}); 129 } 130 catch (ParseException ex) { 131 return null; 132 } 133 catch (NoSuchMethodException ex) { 134 return null; 135 } 136 catch (InvocationTargetException ex) { 137 return null; 138 } 139 catch (InstantiationException ex) { 140 return null; 141 }catch (IllegalAccessException ex) { 142 return null; 143 } 144 } 145 return null; 146 } 147 148 public Object create(DataTypeEnum dataType, Object [] arguments) { 149 if (arguments == null || arguments.length == 0) 150 return create(dataType); 151 152 Class clazz = getJavaClass(dataType); 153 if (clazz != null) { 154 try { 155 Class [] argTypes = new Class [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 [] {String .class}); 161 return strConstructor.newInstance(arguments); 162 } 163 catch (NoSuchMethodException ex) { 164 return null; 165 } 166 catch (InvocationTargetException ex) { 167 return null; 168 } 169 catch (InstantiationException ex) { 170 return null; 171 }catch (IllegalAccessException ex) { 172 return null; 173 } 174 } 175 return null; 176 } 177 178 public Object toDataType(DataTypeEnum dataType, Object untypedValue) { 179 Class 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 v = null; 191 String strUntypedValue = null; 192 boolean isStringUntypedValue = untypedValue instanceof String ; 193 Number numUntypedValue = null; 194 boolean isNumberUntypedValue = untypedValue instanceof Number ; 195 if (isStringUntypedValue) 196 strUntypedValue = (String )untypedValue; 197 if (isNumberUntypedValue) 198 numUntypedValue = (Number )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 (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 (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 (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 (numUntypedValue.doubleValue()); 239 else if (isStringUntypedValue) 240 v = NumberUtils.createDouble(strUntypedValue); 241 break; 242 } 243 return v; 244 } 245 246 public Object fromDataType( 247 Class untypedValueClass, 248 DataTypeEnum dataType, 249 Object typedValue) { 250 if (typedValue == null) 251 return null; 252 Class dataTypeClass = getJavaClass(dataType); 253 255 if (untypedValueClass == null) 256 return typedValue; 257 258 if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass)) 259 return typedValue; 260 261 String strTypedValue = null; 262 boolean isStringTypedValue = typedValue instanceof String ; 263 Number numTypedValue = null; 264 boolean isNumberTypedValue = typedValue instanceof Number ; 265 Boolean boolTypedValue = null; 266 boolean isBooleanTypedValue = typedValue instanceof Boolean ; 267 Date dateTypedValue = null; 268 boolean isDateTypedValue = typedValue instanceof Date; 269 270 if (isStringTypedValue) 271 strTypedValue = (String )typedValue; 272 if (isNumberTypedValue) 273 numTypedValue = (Number )typedValue; 274 if (isBooleanTypedValue) 275 boolTypedValue = (Boolean )typedValue; 276 if (isDateTypedValue) 277 dateTypedValue = (Date)typedValue; 278 279 Object v = null; 280 if (String .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 .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 .class.equals(untypedValueClass)) { 300 if (isStringTypedValue) 301 v = Byte.valueOf(strTypedValue); 302 else if (isNumberTypedValue) 303 v = new Byte (numTypedValue.byteValue()); 304 else if (isBooleanTypedValue) 305 v = new Byte ( 306 (byte)BooleanUtils.toInteger(boolTypedValue.booleanValue())); 307 else if (isDateTypedValue) 308 v = new Byte ((byte)dateTypedValue.getTime()); 309 } else if (byte[].class.equals(untypedValueClass)) { 310 if (isStringTypedValue) 311 v = strTypedValue.getBytes(); 312 } else if (Double .class.equals(untypedValueClass)) { 313 if (isStringTypedValue) 314 v = NumberUtils.createDouble(strTypedValue); 315 else if (isNumberTypedValue) 316 v = new Double (numTypedValue.doubleValue()); 317 else if (isBooleanTypedValue) 318 v = new Double ( 319 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 320 else if (isDateTypedValue) 321 v = new Double (dateTypedValue.getTime()); 322 } else if (Float .class.equals(untypedValueClass)) { 323 if (isStringTypedValue) 324 v = NumberUtils.createFloat(strTypedValue); 325 else if (isNumberTypedValue) 326 v = new Float (numTypedValue.floatValue()); 327 else if (isBooleanTypedValue) 328 v = new Float ( 329 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 330 else if (isDateTypedValue) 331 v = new Float (dateTypedValue.getTime()); 332 } else if (Integer .class.equals(untypedValueClass)) { 333 if (isStringTypedValue) 334 v = NumberUtils.createInteger(strTypedValue); 335 else if (isNumberTypedValue) 336 v = new Integer (numTypedValue.intValue()); 337 else if (isBooleanTypedValue) 338 v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue()); 339 else if (isDateTypedValue) 340 v = new Integer ((int)dateTypedValue.getTime()); 341 } else if (Long .class.equals(untypedValueClass)) { 342 if (isStringTypedValue) 343 v = NumberUtils.createLong(strTypedValue); 344 else if (isNumberTypedValue) 345 v = new Long (numTypedValue.longValue()); 346 else if (isBooleanTypedValue) 347 v = new Long ( 348 BooleanUtils.toInteger(boolTypedValue.booleanValue())); 349 else if (isDateTypedValue) 350 v = new Long (dateTypedValue.getTime()); 351 } else if (java.sql.Date .class.equals(untypedValueClass)) { 352 if (isNumberTypedValue) 353 v = new java.sql.Date (numTypedValue.longValue()); 354 else if (isDateTypedValue) 355 v = new java.sql.Date (dateTypedValue.getTime()); 356 } else if (java.sql.Time .class.equals(untypedValueClass)) { 357 if (isNumberTypedValue) 358 v = new java.sql.Time (numTypedValue.longValue()); 359 else if (isDateTypedValue) 360 v = new java.sql.Time (dateTypedValue.getTime()); 361 } else if (java.sql.Timestamp .class.equals(untypedValueClass)) { 362 if (isNumberTypedValue) 363 v = new java.sql.Timestamp (numTypedValue.longValue()); 364 else if (isDateTypedValue) 365 v = new java.sql.Timestamp (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 |