1 22 package org.jboss.ejb.plugins.cmp.jdbc2.bridge; 23 24 import org.jboss.ejb.plugins.cmp.bridge.CMPFieldBridge; 25 import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2; 26 import org.jboss.ejb.plugins.cmp.jdbc2.PersistentContext; 27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData; 28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCType; 29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore; 30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCResultSetReader; 31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge; 32 import org.jboss.ejb.EntityEnterpriseContext; 33 import org.jboss.deployment.DeploymentException; 34 import org.jboss.logging.Logger; 35 36 import javax.ejb.EJBException ; 37 import java.lang.reflect.Field ; 38 import java.sql.PreparedStatement ; 39 import java.sql.SQLException ; 40 import java.sql.ResultSet ; 41 42 46 public class JDBCCMPFieldBridge2 47 implements CMPFieldBridge, JDBCFieldBridge 48 { 49 private final JDBCEntityBridge2 entity; 50 private final int rowIndex; 51 private final JDBCType jdbcType; 52 private final Class pkClass; 53 private final Field pkField; 54 private final boolean isPrimaryKeyMember; 55 private final String fieldName; 56 private final Class fieldType; 57 private final String columnName; 58 59 private final JDBCCMPFieldBridge2 cmpFieldIAmMappedTo; 60 61 private final Logger log; 62 63 private int versionIndex = -1; 64 65 public JDBCCMPFieldBridge2(JDBCStoreManager2 manager, 66 JDBCEntityBridge2 entity, 67 JDBCCMPFieldMetaData metadata, 68 int rowIndex) 69 throws DeploymentException 70 { 71 this.rowIndex = rowIndex; 72 this.entity = entity; 73 jdbcType = manager.getJDBCTypeFactory().getJDBCType(metadata); 74 pkClass = metadata.getEntity().getPrimaryKeyClass(); 75 pkField = metadata.getPrimaryKeyField(); 76 isPrimaryKeyMember = metadata.isPrimaryKeyMember(); 77 fieldName = metadata.getFieldName(); 78 fieldType = metadata.getFieldType(); 79 cmpFieldIAmMappedTo = null; 80 columnName = metadata.getColumnName(); 81 82 log = Logger.getLogger(this.getClass().getName() + "." + entity.getEntityName() + "#" + getFieldName()); 83 } 84 85 public JDBCCMPFieldBridge2(JDBCCMPFieldBridge2 cmpField, JDBCCMPFieldBridge2 relatedPKField) 86 { 87 entity = cmpField.entity; 88 rowIndex = cmpField.rowIndex; 89 jdbcType = cmpField.jdbcType; 90 columnName = cmpField.columnName; 91 92 fieldName = relatedPKField.fieldName; 93 fieldType = relatedPKField.fieldType; 94 pkClass = relatedPKField.pkClass; 95 pkField = relatedPKField.pkField; 96 97 isPrimaryKeyMember = false; 98 99 cmpFieldIAmMappedTo = cmpField; 100 101 log = Logger.getLogger(this.getClass().getName() + "." + entity.getEntityName() + "#" + getFieldName()); 102 } 103 104 106 public void initVersion() 107 { 108 versionIndex = entity.getTable().addVersionField(); 109 } 110 111 public int getVersionIndex() 112 { 113 return versionIndex; 114 } 115 116 public String getColumnName() 117 { 118 return columnName; 119 } 120 121 public Object setPrimaryKeyValue(Object primaryKey, Object value) 122 throws IllegalArgumentException 123 { 124 try 125 { 126 if(pkField != null) 127 { 128 if(value == null && primaryKey == null) 130 { 131 return null; 132 } 133 134 if(primaryKey == null) 136 { 137 primaryKey = pkClass.newInstance(); 138 } 139 140 pkField.set(primaryKey, value); 142 return primaryKey; 143 } 144 else 145 { 146 return value; 148 } 149 } 150 catch(Exception e) 151 { 152 throw new EJBException ("Internal error setting instance field " + getFieldName(), e); 154 } 155 } 156 157 public void setValueInternal(EntityEnterpriseContext ctx, Object value, boolean makeDirty) 158 { 159 PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext(); 160 161 if(cmpFieldIAmMappedTo != null && cmpFieldIAmMappedTo.isPrimaryKeyMember) 163 { 164 Object curValue = pctx.getFieldValue(rowIndex); 165 if(value != null && !value.equals(curValue)) 166 { 167 throw new IllegalStateException ( 168 "Attempt to modify a primary key field through a foreign key field mapped to it: " 169 + 170 entity.getEntityName() 171 + "." 172 + cmpFieldIAmMappedTo.getFieldName() 173 + 174 " -> " 175 + entity.getQualifiedTableName() 176 + "." 177 + cmpFieldIAmMappedTo.getColumnName() + 178 ", current value=" + curValue + ", new value=" + value 179 ); 180 } 181 182 makeDirty = false; 183 } 184 else 185 { 186 pctx.setFieldValue(rowIndex, value); 187 } 188 189 if(makeDirty) 190 { 191 pctx.setDirty(); 192 } 193 } 194 195 public int setArgumentParameters(PreparedStatement ps, int parameterIndex, Object arg) 196 { 197 try 198 { 199 int[] jdbcTypes = jdbcType.getJDBCTypes(); 200 for(int i = 0; i < jdbcTypes.length; i++) 201 { 202 Object columnValue = jdbcType.getColumnValue(i, arg); 203 jdbcType.getParameterSetter()[i].set(ps, parameterIndex++, jdbcTypes[i], columnValue, log); 204 } 206 return parameterIndex; 207 } 208 catch(SQLException e) 209 { 210 throw new EJBException ("Internal error setting parameters for field " + getFieldName(), e); 212 } 213 } 214 215 public Object loadArgumentResults(ResultSet rs, int parameterIndex) 216 throws IllegalArgumentException 217 { 218 try 219 { 220 Class [] javaTypes = jdbcType.getJavaTypes(); 222 if(javaTypes.length > 1) 223 { 224 throw new IllegalStateException ("Complex types are not supported yet."); 225 } 226 227 JDBCResultSetReader[] rsReaders = jdbcType.getResultSetReaders(); 228 229 Object columnValue = null; 230 for(int i = 0; i < javaTypes.length; i++) 231 { 232 columnValue = rsReaders[i].get(rs, parameterIndex++, javaTypes[i], log); 233 columnValue = jdbcType.setColumnValue(i, null, columnValue); 234 } 235 236 return columnValue; 238 } 239 catch(SQLException e) 240 { 241 throw new EJBException ("Internal error getting results for field member " + getFieldName(), e); 243 } 244 } 245 246 public int getRowIndex() 247 { 248 return rowIndex; 249 } 250 251 public JDBCEntityPersistenceStore getManager() 252 { 253 return entity.getManager(); 254 } 255 256 258 public void initInstance(EntityEnterpriseContext ctx) 259 { 260 Object value; 261 Class fieldType = getFieldType(); 262 if(fieldType == boolean.class) 263 { 264 value = Boolean.FALSE; 265 } 266 else if(fieldType == byte.class) 267 { 268 value = new Byte ((byte) 0); 269 } 270 else if(fieldType == int.class) 271 { 272 value = new Integer (0); 273 } 274 else if(fieldType == long.class) 275 { 276 value = new Long (0L); 277 } 278 else if(fieldType == short.class) 279 { 280 value = new Short ((short) 0); 281 } 282 else if(fieldType == char.class) 283 { 284 value = new Character ('\u0000'); 285 } 286 else if(fieldType == double.class) 287 { 288 value = new Double (0d); 289 } 290 else if(fieldType == float.class) 291 { 292 value = new Float (0f); 293 } 294 else 295 { 296 value = null; 297 } 298 setValueInternal(ctx, value, false); 299 } 300 301 public void resetPersistenceContext(EntityEnterpriseContext ctx) 302 { 303 throw new UnsupportedOperationException (); 304 } 305 306 public int setInstanceParameters(PreparedStatement ps, int parameterIndex, EntityEnterpriseContext ctx) 307 { 308 throw new UnsupportedOperationException (); 309 } 310 311 public Object getInstanceValue(EntityEnterpriseContext ctx) 312 { 313 throw new UnsupportedOperationException (); 314 } 315 316 public void setInstanceValue(EntityEnterpriseContext ctx, Object value) 317 { 318 throw new UnsupportedOperationException (); 319 } 320 321 public int loadInstanceResults(ResultSet rs, int parameterIndex, EntityEnterpriseContext ctx) 322 { 323 throw new UnsupportedOperationException (); 324 } 325 326 public int loadArgumentResults(ResultSet rs, int parameterIndex, Object [] argumentRef) 327 { 328 throw new UnsupportedOperationException (); 329 } 330 331 public boolean isDirty(EntityEnterpriseContext ctx) 332 { 333 throw new UnsupportedOperationException (); 334 } 335 336 public void setClean(EntityEnterpriseContext ctx) 337 { 338 throw new UnsupportedOperationException (); 339 } 340 341 public boolean isCMPField() 342 { 343 return true; 344 } 345 346 public boolean isPrimaryKeyMember() 347 { 348 return isPrimaryKeyMember; 349 } 350 351 public boolean isReadOnly() 352 { 353 throw new UnsupportedOperationException (); 354 } 355 356 public boolean isReadTimedOut(EntityEnterpriseContext ctx) 357 { 358 throw new UnsupportedOperationException (); 359 } 360 361 public boolean isLoaded(EntityEnterpriseContext ctx) 362 { 363 throw new UnsupportedOperationException (); 364 } 365 366 public JDBCType getJDBCType() 367 { 368 return jdbcType; 369 } 370 371 public Object getPrimaryKeyValue(Object primaryKey) 372 throws IllegalArgumentException 373 { 374 try 375 { 376 if(pkField != null) 377 { 378 if(primaryKey == null) 379 { 380 return null; 381 } 382 383 return pkField.get(primaryKey); 384 } 385 else 386 { 387 return primaryKey; 388 } 389 } 390 catch(Exception e) 391 { 392 throw new EJBException ("Internal error getting primary key field member " + getFieldName(), e); 393 } 394 } 395 396 398 public String getFieldName() 399 { 400 return fieldName; 401 } 402 403 public Object getValue(EntityEnterpriseContext ctx) 404 { 405 PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext(); 406 return pctx.getFieldValue(rowIndex); 407 } 408 409 public void setValue(EntityEnterpriseContext ctx, Object value) 410 { 411 setValueInternal(ctx, value, true); 412 } 413 414 public Class getFieldType() 415 { 416 return fieldType; 417 } 418 } 419 | Popular Tags |