KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > jdbc > ColumnConversion


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.jdbc;
32
33 import java.math.BigDecimal JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Calendar JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * @author Gert Rijs
43  * @version 1.0
44  */

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