1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.util.HashMap ; 25 import javax.ejb.EJBException ; 26 27 43 public final class JDBCTypeComplex implements JDBCType { 44 private final JDBCTypeComplexProperty[] properties; 45 private final String [] columnNames; 46 private final Class [] javaTypes; 47 private final int[] jdbcTypes; 48 private final String [] sqlTypes; 49 private final boolean[] notNull; 50 private final JDBCResultSetReader[] resultSetReaders; 51 private final JDBCParameterSetter[] paramSetters; 52 private final Class fieldType; 53 private final HashMap propertiesByName = new HashMap (); 54 55 public JDBCTypeComplex( 56 JDBCTypeComplexProperty[] properties, 57 Class fieldType) { 58 59 this.properties = properties; 60 this.fieldType = fieldType; 61 62 int propNum = properties.length; 63 columnNames = new String [propNum]; 64 javaTypes = new Class [propNum]; 65 jdbcTypes = new int[propNum]; 66 sqlTypes = new String [propNum]; 67 notNull = new boolean[propNum]; 68 resultSetReaders = new JDBCResultSetReader[propNum]; 69 paramSetters = new JDBCParameterSetter[propNum]; 70 for(int i=0; i<properties.length; i++) 71 { 72 JDBCTypeComplexProperty property = properties[i]; 73 columnNames[i] = property.getColumnName(); 74 javaTypes[i] = property.getJavaType(); 75 jdbcTypes[i] = property.getJDBCType(); 76 sqlTypes[i] = property.getSQLType(); 77 notNull[i] = property.isNotNull(); 78 resultSetReaders[i] = property.getResulSetReader(); 79 paramSetters[i] = property.getParameterSetter(); 80 propertiesByName.put(property.getPropertyName(), property); 81 } 82 } 83 84 public String [] getColumnNames() { 85 return columnNames; 86 } 87 88 public Class [] getJavaTypes() { 89 return javaTypes; 90 } 91 92 public int[] getJDBCTypes() { 93 return jdbcTypes; 94 } 95 96 public String [] getSQLTypes() { 97 return sqlTypes; 98 } 99 100 public boolean[] getNotNull() { 101 return notNull; 102 } 103 104 public boolean[] getAutoIncrement() { 105 return new boolean[] {false}; 106 } 107 108 public Object getColumnValue(int index, Object value) { 109 return getColumnValue(properties[index], value); 110 } 111 112 public Object setColumnValue(int index, Object value, Object columnValue) { 113 return setColumnValue(properties[index], value, columnValue); 114 } 115 116 public boolean hasMapper() 117 { 118 return false; 119 } 120 121 public boolean isSearchable() 122 { 123 return false; 124 } 125 126 public JDBCResultSetReader[] getResultSetReaders() 127 { 128 return resultSetReaders; 129 } 130 131 public JDBCParameterSetter[] getParameterSetter() 132 { 133 return paramSetters; 134 } 135 136 public JDBCTypeComplexProperty[] getProperties() { 137 return properties; 138 } 139 140 public JDBCTypeComplexProperty getProperty(String propertyName) { 141 JDBCTypeComplexProperty prop = (JDBCTypeComplexProperty)propertiesByName.get(propertyName); 142 if(prop == null) { 143 throw new EJBException (fieldType.getName() + 144 " does not have a property named " + propertyName); 145 } 146 return prop; 147 } 148 149 private static Object getColumnValue(JDBCTypeComplexProperty property, Object value) { 150 try { 151 return property.getColumnValue(value); 152 } catch(EJBException e) { 153 throw e; 154 } catch(Exception e) { 155 throw new EJBException ("Error getting column value", e); 156 } 157 } 158 159 private Object setColumnValue( 160 JDBCTypeComplexProperty property, 161 Object value, 162 Object columnValue) { 163 164 if(value==null && columnValue==null) { 165 return null; 167 } 168 169 try { 170 if(value == null) { 171 value = fieldType.newInstance(); 172 } 173 return property.setColumnValue(value, columnValue); 174 } catch(Exception e) { 175 e.printStackTrace(); 176 throw new EJBException ("Error setting column value", e); 177 } 178 } 179 } 180 | Popular Tags |