1 package org.apache.ojb.broker.accesslayer; 2 3 17 18 import java.lang.reflect.Method ; 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.util.Map ; 22 23 import org.apache.ojb.broker.PersistenceBrokerException; 24 import org.apache.ojb.broker.metadata.ClassDescriptor; 25 import org.apache.ojb.broker.metadata.FieldDescriptor; 26 import org.apache.ojb.broker.util.ClassHelper; 27 import org.apache.ojb.broker.util.SqlHelper; 28 29 34 35 public class RowReaderDefaultImpl implements RowReader 36 { 37 40 private static final String OJB_CONCRETE_CLASS_KEY = "ojbTemporaryNoneColumnKey"; 41 44 private static final Object [] NO_ARGS = {}; 45 46 private ClassDescriptor m_cld; 47 48 public RowReaderDefaultImpl(ClassDescriptor cld) 49 { 50 this.m_cld = cld; 51 } 52 53 80 public Object readObjectFrom(Map row) throws PersistenceBrokerException 81 { 82 ClassDescriptor cld = selectClassDescriptor(row); 84 return buildOrRefreshObject(row, cld, null); 85 } 86 87 90 public void refreshObject(Object instance, Map row) 91 { 92 ClassDescriptor targetClassDescriptor = selectClassDescriptor(row); 94 buildOrRefreshObject(row, targetClassDescriptor, instance); 96 } 97 98 108 protected Object buildOrRefreshObject(Map row, ClassDescriptor targetClassDescriptor, Object targetObject) 109 { 110 Object result = targetObject; 111 FieldDescriptor fmd; 112 FieldDescriptor[] fields = targetClassDescriptor.getFieldDescriptor(true); 113 114 if(targetObject == null) 115 { 116 result = ClassHelper.buildNewObjectInstance(targetClassDescriptor); 118 } 119 120 for (int i = 0; i < fields.length; i++) 122 { 123 fmd = fields[i]; 124 fmd.getPersistentField().set(result, row.get(fmd.getColumnName())); 125 } 126 127 if(targetObject == null) 128 { 129 Method initializationMethod = targetClassDescriptor.getInitializationMethod(); 131 if (initializationMethod != null) 132 { 133 try 134 { 135 initializationMethod.invoke(result, NO_ARGS); 136 } 137 catch (Exception ex) 138 { 139 throw new PersistenceBrokerException("Unable to invoke initialization method:" + initializationMethod.getName() + " for class:" + m_cld.getClassOfObject(), ex); 140 } 141 } 142 } 143 return result; 144 } 145 146 164 public void readObjectArrayFrom(ResultSetAndStatement rs_stmt, Map row) 165 { 166 FieldDescriptor[] fields; 167 171 if (m_cld.getSuperClass() != null) 172 { 173 177 fields = m_cld.getFieldDescriptorsInHeirarchy(); 178 } 179 else 180 { 181 String ojbConcreteClass = extractOjbConcreteClass(m_cld, rs_stmt.m_rs, row); 182 187 if(ojbConcreteClass != null) 188 { 189 ClassDescriptor cld = m_cld.getRepository().getDescriptorFor(ojbConcreteClass); 190 row.put(OJB_CONCRETE_CLASS_KEY, cld.getClassOfObject()); 191 fields = cld.getFieldDescriptor(true); 192 } 193 else 194 { 195 String ojbClass = SqlHelper.getOjbClassName(rs_stmt.m_rs); 196 if (ojbClass != null) 197 { 198 ClassDescriptor cld = m_cld.getRepository().getDescriptorFor(ojbClass); 199 row.put(OJB_CONCRETE_CLASS_KEY, cld.getClassOfObject()); 200 fields = cld.getFieldDescriptor(true); 201 } 202 else 203 { 204 fields = m_cld.getFieldDescriptor(true); 205 } 206 } 207 } 208 readValuesFrom(rs_stmt, row, fields); 209 } 210 211 215 public void readPkValuesFrom(ResultSetAndStatement rs_stmt, Map row) 216 { 217 String ojbClass = SqlHelper.getOjbClassName(rs_stmt.m_rs); 218 ClassDescriptor cld; 219 220 if (ojbClass != null) 221 { 222 cld = m_cld.getRepository().getDescriptorFor(ojbClass); 223 } 224 else 225 { 226 cld = m_cld; 227 } 228 229 FieldDescriptor[] pkFields = cld.getPkFields(); 230 readValuesFrom(rs_stmt, row, pkFields); 231 } 232 233 protected void readValuesFrom(ResultSetAndStatement rs_stmt, Map row, FieldDescriptor[] fields) 234 { 235 int size = fields.length; 236 Object val; 237 FieldDescriptor fld = null; 238 try 239 { 240 for (int j = 0; j < size; j++) 241 { 242 fld = fields[j]; 243 if(!row.containsKey(fld.getColumnName())) 244 { 245 int idx = rs_stmt.m_sql.getColumnIndex(fld); 246 val = fld.getJdbcType().getObjectFromColumn(rs_stmt.m_rs, null, fld.getColumnName(), idx); 247 row.put(fld.getColumnName(), fld.getFieldConversion().sqlToJava(val)); 248 } 249 } 250 } 251 catch (SQLException t) 252 { 253 throw new PersistenceBrokerException("Error reading class '" 254 + (fld != null ? fld.getClassDescriptor().getClassNameOfObject() : m_cld.getClassNameOfObject()) 255 + "' from result set, current read field was '" 256 + (fld != null ? fld.getPersistentField().getName() + "'" : null), t); 257 } 258 } 259 260 protected String extractOjbConcreteClass(ClassDescriptor cld, ResultSet rs, Map row) 261 { 262 FieldDescriptor fld = m_cld.getOjbConcreteClassField(); 263 if (fld == null) 264 { 265 return null; 266 } 267 try 268 { 269 Object tmp = fld.getJdbcType().getObjectFromColumn(rs, fld.getColumnName()); 270 String result = (String ) fld.getFieldConversion().sqlToJava(tmp); 272 result = result != null ? result.trim() : null; 273 if (result == null || result.length() == 0) 274 { 275 throw new PersistenceBrokerException( 276 "ojbConcreteClass field for class " + cld.getClassNameOfObject() 277 + " returned null or 0-length string"); 278 } 279 else 280 { 281 284 row.put(fld.getColumnName(), result); 285 return result; 286 } 287 } 288 catch(SQLException e) 289 { 290 throw new PersistenceBrokerException("Unexpected error while try to read 'ojbConcretClass'" + 291 " field from result set using column name " + fld.getColumnName() + " main class" + 292 " was " + m_cld.getClassNameOfObject(), e); 293 } 294 } 295 296 299 protected ClassDescriptor selectClassDescriptor(Map row) throws PersistenceBrokerException 300 { 301 ClassDescriptor result = m_cld; 302 Class ojbConcreteClass = (Class ) row.get(OJB_CONCRETE_CLASS_KEY); 303 if(ojbConcreteClass != null) 304 { 305 result = m_cld.getRepository().getDescriptorFor(ojbConcreteClass); 306 if (result == null) 308 { 309 throw new PersistenceBrokerException("Can't find class-descriptor for ojbConcreteClass '" 310 + ojbConcreteClass + "', the main class was " + m_cld.getClassNameOfObject()); 311 } 312 } 313 return result; 314 } 315 316 public void setClassDescriptor(ClassDescriptor cld) 317 { 318 this.m_cld = cld; 319 } 320 321 public ClassDescriptor getClassDescriptor() 322 { 323 return m_cld; 324 } 325 } 326 | Popular Tags |