01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.backend.list.helper.reader;
16:
17: import java.math.BigDecimal;
18: import java.sql.ResultSet;
19: import java.sql.SQLException;
20: import java.sql.Timestamp;
21:
22: import org.araneaframework.core.util.ExceptionUtil;
23:
24: /**
25: * Default implementation of ResultSetColumnReader.
26: * <p>
27: * For each Java type the respectful "get" method is used of the {@link ResultSet}.
28: * For {@link java.util.Date} an instance of that class is returned (not a {@link java.sql.Date}).
29: * If unsupported Java type is used a runtime exception is thrown.
30: *
31: * @see ResultSetColumnReader
32: *
33: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
34: */
35: public class DefaultResultSetColumnReader implements
36: ResultSetColumnReader {
37:
38: protected static final ResultSetColumnReader instance = new DefaultResultSetColumnReader();
39:
40: /**
41: * Returns a Singleton instance of the <code>DefaultResultSetReader</code>.
42: */
43: public static ResultSetColumnReader getInstance() {
44: return instance;
45: }
46:
47: protected DefaultResultSetColumnReader() {
48: // hide
49: }
50:
51: /**
52: * Tries to read the same type as is given in the <code>javaType</code>.
53: */
54: public Object readFromResultSet(String columnName,
55: ResultSet resultSet, Class javaType) {
56: try {
57: if (resultSet.getObject(columnName) == null)
58: return null;
59:
60: if (Long.class.isAssignableFrom(javaType))
61: return new Long(resultSet.getLong(columnName));
62:
63: if (Integer.class.isAssignableFrom(javaType))
64: return new Integer(resultSet.getInt(columnName));
65:
66: if (Boolean.class.isAssignableFrom(javaType))
67: return resultSet.getBoolean(columnName) ? Boolean.TRUE
68: : Boolean.FALSE;
69:
70: if (BigDecimal.class.isAssignableFrom(javaType))
71: return resultSet.getBigDecimal(columnName);
72:
73: if (Timestamp.class.isAssignableFrom(javaType))
74: return resultSet.getTimestamp(columnName);
75:
76: if (java.sql.Date.class.isAssignableFrom(javaType))
77: return resultSet.getDate(columnName);
78:
79: if (java.util.Date.class.isAssignableFrom(javaType))
80: return new java.util.Date(resultSet.getTimestamp(
81: columnName).getTime());
82:
83: if (String.class.isAssignableFrom(javaType))
84: return resultSet.getString(columnName);
85:
86: } catch (SQLException e) {
87: throw ExceptionUtil.uncheckException(e);
88: }
89: throw new RuntimeException("Could not read column '"
90: + columnName + "' with Java type '" + javaType
91: + "' from the ResultSet!");
92: }
93:
94: }
|