1 30 31 package com.genimen.djeneric.jdbc; 32 33 import java.math.BigDecimal ; 34 import java.sql.SQLException ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Calendar ; 37 import java.util.Date ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 45 46 public abstract class ColumnConversion 47 { 48 protected static final String CONVERSION_NOT_SUPPORTED = "Conversion not supported"; 49 private static Map _convertors = new HashMap (); 50 static 51 { 52 _convertors.put(Date .class, new CvtDate()); 53 _convertors.put(Calendar .class, new CvtCalendar()); 54 _convertors.put(BigDecimal .class, new CvtBigDecimal()); 55 _convertors.put(String .class, new CvtString()); 56 _convertors.put(Boolean .class, new CvtBoolean()); 57 _convertors.put(byte[].class, new CvtByteArray()); 58 } 59 60 private ColumnConversion() 61 { 62 } 63 64 public static final String toString(Object o) throws SQLException 65 { 66 if (o == null) return null; 67 Class c = o.getClass(); 68 Cvt cvt = (Cvt) _convertors.get(c); 69 if (cvt == null) throw new SQLException (CONVERSION_NOT_SUPPORTED); 70 return cvt.toString(o); 71 } 72 73 public static final BigDecimal toBigDecimal(Object o) throws SQLException 74 { 75 if (o == null) return null; 76 Class c = o.getClass(); 77 Cvt cvt = (Cvt) _convertors.get(c); 78 if (cvt == null) throw new SQLException (CONVERSION_NOT_SUPPORTED); 79 return cvt.toBigDecimal(o); 80 } 81 82 public static final Calendar toCalendar(Object o) throws SQLException 83 { 84 if (o == null) return null; 85 Class c = o.getClass(); 86 Cvt cvt = (Cvt) _convertors.get(c); 87 if (cvt == null) throw new SQLException (CONVERSION_NOT_SUPPORTED); 88 return cvt.toCalendar(o); 89 } 90 91 public static final Date toDate(Object o) throws SQLException 92 { 93 if (o == null) return null; 94 Class c = o.getClass(); 95 Cvt cvt = (Cvt) _convertors.get(c); 96 if (cvt == null) throw new SQLException (CONVERSION_NOT_SUPPORTED); 97 return cvt.toDate(o); 98 } 99 100 public static final Boolean toBoolean(Object o) throws SQLException 101 { 102 if (o == null) return null; 103 Class c = o.getClass(); 104 Cvt cvt = (Cvt) _convertors.get(c); 105 if (cvt == null) throw new SQLException (CONVERSION_NOT_SUPPORTED); 106 return cvt.toBoolean(o); 107 } 108 } 109 110 abstract class Cvt 111 { 112 protected static final SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); 113 protected static final String CONVERSION_NOT_SUPPORTED = "Conversion not supported"; 114 protected static final String CONVERSION_FAILLURE = "Conversion has failed"; 115 116 public String toString(Object o) throws SQLException 117 { 118 return o.toString(); 119 } 120 121 public BigDecimal toBigDecimal(Object o) throws SQLException 122 { 123 throw new SQLException (CONVERSION_NOT_SUPPORTED); 124 } 125 126 public Calendar toCalendar(Object o) throws SQLException 127 { 128 throw new SQLException (CONVERSION_NOT_SUPPORTED); 129 } 130 131 public Date toDate(Object o) throws SQLException 132 { 133 throw new SQLException (CONVERSION_NOT_SUPPORTED); 134 } 135 136 public Boolean toBoolean(Object o) throws SQLException 137 { 138 throw new SQLException (CONVERSION_NOT_SUPPORTED); 139 } 140 141 public byte[] toByteArray(Object o) throws SQLException 142 { 143 throw new SQLException (CONVERSION_NOT_SUPPORTED); 144 } 145 } 146 147 class CvtByteArray extends Cvt 148 { 149 public String toString(Object o) 150 { 151 return new String ((byte[]) o); 152 } 153 154 public byte[] toByteArray(Object o) 155 { 156 return (byte[]) o; 157 } 158 } 159 160 class CvtDate extends Cvt 161 { 162 public Date toDate(Object o) 163 { 164 return (Date ) o; 165 } 166 167 public Calendar toCalendar(Object o) throws SQLException 168 { 169 Calendar c = Calendar.getInstance(); 170 c.setTime(toDate(o)); 171 return c; 172 } 173 174 public String toString(Object o) 175 { 176 return SDF.format(toDate(o)); 177 } 178 179 public BigDecimal toBigDecimal(Object o) 180 { 181 return new BigDecimal (toDate(o).getTime()); 182 } 183 } 184 class CvtCalendar extends Cvt 185 { 186 public Calendar toCalendar(Object o) 187 { 188 return (Calendar ) o; 189 } 190 191 public String toString(Object o) 192 { 193 return SDF.format(toCalendar(o).getTime()); 194 } 195 196 public Date toDate(Object o) 197 { 198 return toCalendar(o).getTime(); 199 } 200 201 public BigDecimal toBigDecimal(Object o) 202 { 203 return new BigDecimal (toCalendar(o).getTime().getTime()); 204 } 205 } 206 class CvtBigDecimal extends Cvt 207 { 208 public BigDecimal toBigDecimal(Object o) 209 { 210 return (BigDecimal ) o; 211 } 212 } 213 class CvtString extends Cvt 214 { 215 public String toString(Object o) 216 { 217 return (String ) o; 218 } 219 220 public Calendar toCalendar(Object o) throws SQLException 221 { 222 try 223 { 224 Date d = SDF.parse(toString(o)); 225 Calendar c = Calendar.getInstance(); 226 c.setTime(d); 227 return c; 228 } 229 catch (Exception e) 230 { 231 throw new SQLException (CONVERSION_FAILLURE + ": " + e.toString()); 232 } 233 } 234 235 public Date toDate(Object o) throws SQLException 236 { 237 try 238 { 239 return SDF.parse(toString(o)); 240 } 241 catch (Exception e) 242 { 243 throw new SQLException (CONVERSION_FAILLURE + ": " + e.toString()); 244 } 245 } 246 247 public BigDecimal toBigDecimal(Object o) 248 { 249 return new BigDecimal (toString(o)); 250 } 251 252 public byte[] toByteArray(Object o) 253 { 254 return toString(o).getBytes(); 255 } 256 } 257 258 class CvtBoolean extends Cvt 259 { 260 public Boolean toBoolean(Object o) 261 { 262 return (Boolean ) o; 263 } 264 265 public String toString(Object o) 266 { 267 return toBoolean(o).toString(); 268 } 269 } | Popular Tags |