KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > RowMapper


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.controls.system.jdbc;
20
21 import org.apache.beehive.controls.api.ControlException;
22
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.ResultSetMetaData JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Modifier JavaDoc;
31
32 /**
33  * Abstract base class for all row mappers.
34  *
35  * RowMappers are used to map the contents of a row in a ResultSet to the return type of an annotated method.
36  * Supported RowMapper types include: HashMap, Map, Object, XmlObject. When a ResultSetMapper is ready to
37  * map a ResultSet row to an object, it requests a RowMapper for the return type of the method from the
38  * RowMapperFactory.
39  *
40  */

41 public abstract class RowMapper {
42
43     private static final String JavaDoc SETTER_NAME_REGEX = "^(set)([A-Z_]\\w*+)";
44     protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance();
45     protected static final Pattern JavaDoc _setterRegex = Pattern.compile(SETTER_NAME_REGEX);
46
47     /** ResultSet to map. */
48     protected final ResultSet JavaDoc _resultSet;
49
50     /** Calendar instance for date/time mappings. */
51     protected final Calendar JavaDoc _cal;
52
53     /** Class to map ResultSet Rows to. */
54     protected final Class JavaDoc<?> _returnTypeClass;
55
56     /**
57      * Create a new RowMapper for the specified ResultSet and return type Class.
58      * @param resultSet ResultSet to map
59      * @param returnTypeClass Class to map ResultSet rows to.
60      * @param cal Calendar instance for date/time values.
61      */

62     protected RowMapper(ResultSet JavaDoc resultSet, Class JavaDoc<?> returnTypeClass, Calendar JavaDoc cal) {
63         _resultSet = resultSet;
64         _returnTypeClass = returnTypeClass;
65         _cal = cal;
66     }
67
68     /**
69      * Map a ResultSet row to the return type class
70      * @return An instance of class.
71      */

72     public abstract Object JavaDoc mapRowToReturnType();
73
74
75     /**
76      * Build a String array of column names from the ResultSet.
77      * @return A String array containing the column names contained within the ResultSet.
78      * @throws SQLException on error
79      */

80     protected String JavaDoc[] getKeysFromResultSet() throws SQLException JavaDoc {
81
82         String JavaDoc[] keys;
83         final ResultSetMetaData JavaDoc md = _resultSet.getMetaData();
84         final int columnCount = md.getColumnCount();
85
86         keys = new String JavaDoc[columnCount + 1];
87         for (int i = 1; i <= columnCount; i++) {
88             keys[i] = md.getColumnName(i).toUpperCase();
89         }
90         return keys;
91     }
92
93     /**
94      * Determine if the given method is a java bean setter method.
95      * @param method Method to check
96      * @return True if the method is a setter method.
97      */

98     protected boolean isSetterMethod(Method JavaDoc method) {
99         Matcher JavaDoc matcher = _setterRegex.matcher(method.getName());
100         if (matcher.matches()) {
101
102             if (Modifier.isStatic(method.getModifiers())) return false;
103             if (!Modifier.isPublic(method.getModifiers())) return false;
104             if (!Void.TYPE.equals(method.getReturnType())) return false;
105
106             // method parameter checks
107
Class JavaDoc[] params = method.getParameterTypes();
108             if (params.length != 1) return false;
109             if (TypeMappingsFactory.TYPE_UNKNOWN == _tmf.getTypeId(params[0])) return false;
110
111             return true;
112         }
113         return false;
114     }
115
116     /**
117      * Extract a column value from the ResultSet and return it as resultType.
118      *
119      * @param index The column index of the value to extract from the ResultSet.
120      * @param resultType The return type. Defined in TypeMappingsFactory.
121      * @return The extracted value
122      * @throws java.sql.SQLException on error.
123      */

124     protected Object JavaDoc extractColumnValue(int index, int resultType) throws SQLException JavaDoc {
125
126         switch (resultType) {
127             case TypeMappingsFactory.TYPE_INT:
128                 return new Integer JavaDoc(_resultSet.getInt(index));
129             case TypeMappingsFactory.TYPE_LONG:
130                 return new Long JavaDoc(_resultSet.getLong(index));
131             case TypeMappingsFactory.TYPE_FLOAT:
132                 return new Float JavaDoc(_resultSet.getFloat(index));
133             case TypeMappingsFactory.TYPE_DOUBLE:
134                 return new Double JavaDoc(_resultSet.getDouble(index));
135             case TypeMappingsFactory.TYPE_BYTE:
136                 return new Byte JavaDoc(_resultSet.getByte(index));
137             case TypeMappingsFactory.TYPE_SHORT:
138                 return new Short JavaDoc(_resultSet.getShort(index));
139             case TypeMappingsFactory.TYPE_BOOLEAN:
140                 return _resultSet.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE;
141             case TypeMappingsFactory.TYPE_INT_OBJ:
142                 {
143                     int i = _resultSet.getInt(index);
144                     return _resultSet.wasNull() ? null : new Integer JavaDoc(i);
145                 }
146             case TypeMappingsFactory.TYPE_LONG_OBJ:
147                 {
148                     long i = _resultSet.getLong(index);
149                     return _resultSet.wasNull() ? null : new Long JavaDoc(i);
150                 }
151             case TypeMappingsFactory.TYPE_FLOAT_OBJ:
152                 {
153                     float i = _resultSet.getFloat(index);
154                     return _resultSet.wasNull() ? null : new Float JavaDoc(i);
155                 }
156             case TypeMappingsFactory.TYPE_DOUBLE_OBJ:
157                 {
158                     double i = _resultSet.getDouble(index);
159                     return _resultSet.wasNull() ? null : new Double JavaDoc(i);
160                 }
161             case TypeMappingsFactory.TYPE_BYTE_OBJ:
162                 {
163                     byte i = _resultSet.getByte(index);
164                     return _resultSet.wasNull() ? null : new Byte JavaDoc(i);
165                 }
166             case TypeMappingsFactory.TYPE_SHORT_OBJ:
167                 {
168                     short i = _resultSet.getShort(index);
169                     return _resultSet.wasNull() ? null : new Short JavaDoc(i);
170                 }
171             case TypeMappingsFactory.TYPE_BOOLEAN_OBJ:
172                 {
173                     boolean i = _resultSet.getBoolean(index);
174                     return _resultSet.wasNull() ? null : (i ? Boolean.TRUE : Boolean.FALSE);
175                 }
176             case TypeMappingsFactory.TYPE_STRING:
177             case TypeMappingsFactory.TYPE_XMLBEAN_ENUM:
178                 return _resultSet.getString(index);
179             case TypeMappingsFactory.TYPE_BIG_DECIMAL:
180                 return _resultSet.getBigDecimal(index);
181             case TypeMappingsFactory.TYPE_BYTES:
182                 return _resultSet.getBytes(index);
183             case TypeMappingsFactory.TYPE_TIMESTAMP:
184                 {
185                     if (null == _cal)
186                         return _resultSet.getTimestamp(index);
187                     else
188                         return _resultSet.getTimestamp(index, _cal);
189                 }
190             case TypeMappingsFactory.TYPE_TIME:
191                 {
192                     if (null == _cal)
193                         return _resultSet.getTime(index);
194                     else
195                         return _resultSet.getTime(index, _cal);
196                 }
197             case TypeMappingsFactory.TYPE_SQLDATE:
198                 {
199                     if (null == _cal)
200                         return _resultSet.getDate(index);
201                     else
202                         return _resultSet.getDate(index, _cal);
203                 }
204             case TypeMappingsFactory.TYPE_DATE:
205                 {
206                     // convert explicity to java.util.Date
207
// 12918 | knex does not return java.sql.Date properly from web service
208
java.sql.Timestamp JavaDoc ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal);
209                     if (null == ts)
210                         return null;
211                     return new java.util.Date JavaDoc(ts.getTime());
212                 }
213             case TypeMappingsFactory.TYPE_CALENDAR:
214                 {
215                     java.sql.Timestamp JavaDoc ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal);
216                     if (null == ts)
217                         return null;
218                     Calendar JavaDoc c = (null == _cal) ? Calendar.getInstance() : (Calendar JavaDoc) _cal.clone();
219                     c.setTimeInMillis(ts.getTime());
220                     return c;
221                 }
222             case TypeMappingsFactory.TYPE_REF:
223                 return _resultSet.getRef(index);
224             case TypeMappingsFactory.TYPE_BLOB:
225                 return _resultSet.getBlob(index);
226             case TypeMappingsFactory.TYPE_CLOB:
227                 return _resultSet.getClob(index);
228             case TypeMappingsFactory.TYPE_ARRAY:
229                 return _resultSet.getArray(index);
230             case TypeMappingsFactory.TYPE_READER:
231             case TypeMappingsFactory.TYPE_STREAM:
232                 throw new ControlException("streaming return types are not supported by the JdbcControl; use ResultSet instead");
233             case TypeMappingsFactory.TYPE_STRUCT:
234             case TypeMappingsFactory.TYPE_UNKNOWN:
235                 // JAVA_TYPE (could be any), or REF
236
return _resultSet.getObject(index);
237             default:
238                 throw new ControlException("internal error: unknown type ID: " + Integer.toString(resultType));
239         }
240     }
241 }
242
Popular Tags