KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.beehive.controls.api.context.ControlBeanContext;
23 import org.apache.beehive.controls.system.jdbc.JdbcControl.SQL;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Calendar JavaDoc;
31
32 /**
33  * Default ResultSetMapper implementation for Objects.
34  */

35 public class DefaultObjectResultSetMapper extends ResultSetMapper {
36
37     /**
38      * static reference to the TypeMappingsFactory for this class
39      */

40     protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance();
41
42     /**
43      * Map the ResultSet to the method's return type. The object type returned is defined by the return type of the method.
44      *
45      * @param context A ControlBeanContext instance, see Beehive controls javadoc for additional information
46      * @param m Method assoicated with this call.
47      * @param resultSet Result set to map.
48      * @param cal A Calendar instance for time/date value resolution.
49      * @return The Object resulting from the ResultSet
50      */

51     public Object JavaDoc mapToResultType(ControlBeanContext context, Method JavaDoc m, ResultSet JavaDoc resultSet, Calendar JavaDoc cal) {
52
53         final Class JavaDoc returnType = m.getReturnType();
54         final boolean isArray = returnType.isArray();
55
56         try {
57             if (isArray) {
58                 final SQL methodSQL = (SQL) context.getMethodPropertySet(m, SQL.class);
59                 return arrayFromResultSet(resultSet, methodSQL.arrayMaxLength(), returnType, cal);
60             } else {
61                 if (!resultSet.next()) {
62                     return _tmf.fixNull(m.getReturnType());
63                 }
64                 return RowMapperFactory.getRowMapper(resultSet, returnType, cal).mapRowToReturnType();
65             }
66         } catch (SQLException JavaDoc e) {
67             throw new ControlException(e.getMessage(), e);
68         }
69     }
70
71     //
72
// ////////////////////////////////// PRIVATE METHODS //////////////////////////////////////////
73
//
74

75     /**
76      * Invoked when the return type of the method is an array type.
77      *
78      * @param rs ResultSet to process.
79      * @param maxRows The maximum size of array to create
80      * @param arrayClass The class of object contained within the array
81      * @param cal A calendar instance to use for date/time values
82      * @return An array of the specified class type
83      * @throws SQLException On error.
84      */

85     protected Object JavaDoc arrayFromResultSet(ResultSet JavaDoc rs, int maxRows, Class JavaDoc arrayClass, Calendar JavaDoc cal)
86             throws SQLException JavaDoc {
87
88         Class JavaDoc componentType = arrayClass.getComponentType();
89         ResultSetMetaData JavaDoc md = rs.getMetaData();
90
91         ArrayList JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
92         int numRows;
93
94         boolean hasMoreRows = rs.next();
95         RowMapper rowMapper = RowMapperFactory.getRowMapper(rs, componentType, cal);
96
97         for (numRows = 0; numRows != maxRows && hasMoreRows; numRows++) {
98             list.add(rowMapper.mapRowToReturnType());
99             hasMoreRows = rs.next();
100         }
101
102         Object JavaDoc array = java.lang.reflect.Array.newInstance(componentType, numRows);
103         try {
104             for (int i = 0; i < numRows; i++) {
105                 java.lang.reflect.Array.set(array, i, list.get(i));
106             }
107         } catch (IllegalArgumentException JavaDoc iae) {
108             // assuming no errors in resultSetObject() this can only happen
109
// for single column result sets.
110
throw new ControlException("The declared Java type for array " + componentType.getName()
111                                        + "is incompatible with the SQL format of column " + md.getColumnName(1)
112                                        + md.getColumnTypeName(1) + "which returns objects of type + "
113                                        + list.get(0).getClass().getName());
114         }
115         return array;
116     }
117 }
118
Popular Tags