1 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 ; 26 import java.sql.ResultSet ; 27 import java.sql.ResultSetMetaData ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.Calendar ; 31 32 35 public class DefaultObjectResultSetMapper extends ResultSetMapper { 36 37 40 protected static final TypeMappingsFactory _tmf = TypeMappingsFactory.getInstance(); 41 42 51 public Object mapToResultType(ControlBeanContext context, Method m, ResultSet resultSet, Calendar cal) { 52 53 final Class 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 e) { 67 throw new ControlException(e.getMessage(), e); 68 } 69 } 70 71 75 85 protected Object arrayFromResultSet(ResultSet rs, int maxRows, Class arrayClass, Calendar cal) 86 throws SQLException { 87 88 Class componentType = arrayClass.getComponentType(); 89 ResultSetMetaData md = rs.getMetaData(); 90 91 ArrayList <Object > list = new ArrayList <Object >(); 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 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 iae) { 108 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 |