1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.Method ; 25 26 40 public final class JDBCTypeComplexProperty 41 { 42 private final String propertyName; 43 private final String columnName; 44 private final Class javaType; 45 private final int jdbcType; 46 private final String sqlType; 47 private final boolean notNull; 48 private final JDBCResultSetReader resulSetReader; 49 private final JDBCParameterSetter paramSetter; 50 51 private final Method [] getters; 52 private final Method [] setters; 53 54 public JDBCTypeComplexProperty( 55 String propertyName, 56 String columnName, 57 Class javaType, 58 int jdbcType, 59 String sqlType, 60 boolean notNull, 61 Method [] getters, 62 Method [] setters) 63 { 64 65 this.propertyName = propertyName; 66 this.columnName = columnName; 67 this.javaType = javaType; 68 this.jdbcType = jdbcType; 69 this.sqlType = sqlType; 70 this.notNull = notNull; 71 this.getters = getters; 72 this.setters = setters; 73 this.resulSetReader = JDBCUtil.getResultSetReader(jdbcType, javaType); 74 this.paramSetter = JDBCUtil.getParameterSetter(jdbcType, javaType); 75 } 76 77 public JDBCTypeComplexProperty( 78 JDBCTypeComplexProperty defaultProperty, 79 String columnName, 80 int jdbcType, 81 String sqlType, 82 boolean notNull) 83 { 84 85 this.propertyName = defaultProperty.propertyName; 86 this.columnName = columnName; 87 this.javaType = defaultProperty.javaType; 88 this.jdbcType = jdbcType; 89 this.sqlType = sqlType; 90 this.notNull = notNull; 91 this.getters = defaultProperty.getters; 92 this.setters = defaultProperty.setters; 93 this.resulSetReader = JDBCUtil.getResultSetReader(jdbcType, javaType); 94 this.paramSetter = JDBCUtil.getParameterSetter(jdbcType, javaType); 95 } 96 97 public String getPropertyName() 98 { 99 return propertyName; 100 } 101 102 public String getColumnName() 103 { 104 return columnName; 105 } 106 107 public Class getJavaType() 108 { 109 return javaType; 110 } 111 112 public int getJDBCType() 113 { 114 return jdbcType; 115 } 116 117 public String getSQLType() 118 { 119 return sqlType; 120 } 121 122 public boolean isNotNull() 123 { 124 return notNull; 125 } 126 127 public JDBCResultSetReader getResulSetReader() 128 { 129 return resulSetReader; 130 } 131 132 public JDBCParameterSetter getParameterSetter() 133 { 134 return paramSetter; 135 } 136 137 public Object getColumnValue(Object value) throws Exception 138 { 139 Object [] noArgs = new Object [0]; 140 141 for(int i = 0; i < getters.length; i++) 142 { 143 if(value == null) 144 { 145 return null; 146 } 147 value = getters[i].invoke(value, noArgs); 148 } 149 return value; 150 } 151 152 public Object setColumnValue( 153 Object value, 154 Object columnValue) throws Exception 155 { 156 157 Object [] noArgs = new Object [0]; 159 Object [] singleArg = new Object [1]; 160 161 Object returnValue = value; 163 164 for(int i = 0; i < getters.length - 1; i++) 166 { 167 Object next = getters[i].invoke(value, noArgs); 169 170 if(next == null) 172 { 173 next = getters[i].getReturnType().newInstance(); 175 176 singleArg[0] = next; 178 179 setters[i].invoke(value, singleArg); 180 } 181 182 value = next; 184 } 185 186 singleArg[0] = columnValue; 188 setters[setters.length - 1].invoke(value, singleArg); 189 190 return returnValue; 192 } 193 } 194 | Popular Tags |