1 23 24 30 31 package com.sun.jdo.spi.persistence.support.sqlstore.sql; 32 33 import com.sun.jdo.api.persistence.support.JDOFatalInternalException; 34 import com.sun.jdo.spi.persistence.support.sqlstore.*; 35 import com.sun.jdo.spi.persistence.support.sqlstore.model.ClassDesc; 36 import com.sun.jdo.spi.persistence.support.sqlstore.model.FieldDesc; 37 import com.sun.jdo.spi.persistence.support.sqlstore.model.ForeignFieldDesc; 38 import com.sun.jdo.spi.persistence.support.sqlstore.model.LocalFieldDesc; 39 import com.sun.jdo.spi.persistence.support.sqlstore.sql.concurrency.Concurrency; 40 import com.sun.jdo.spi.persistence.utility.I18NHelper; 41 import com.sun.jdo.spi.persistence.utility.logging.Logger; 42 43 import java.util.*; 44 45 48 public class UpdateObjectDescImpl implements UpdateObjectDesc { 49 50 51 private List afterHiddenValues; 52 53 private SQLStateManager afterImage; 54 55 56 private List beforeHiddenValues; 57 58 private SQLStateManager beforeImage; 59 60 private Concurrency concurrency; 61 62 private Class pcClass; 63 64 private int updateAction; 65 66 70 private List updatedFields; 71 72 private Map updatedJoinTableRelationships; 73 74 75 private boolean relationshipChanged = false; 76 77 78 private static Logger logger = LogHelperSQLStore.getLogger(); 79 80 81 private final static ResourceBundle messages = I18NHelper.loadBundle( 82 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", UpdateObjectDescImpl.class.getClassLoader()); 84 85 public UpdateObjectDescImpl(Class pcClass) { 86 this.pcClass = pcClass; 87 updatedFields = new ArrayList(); 88 } 89 90 public Class getPersistenceCapableClass() { 91 return pcClass; 92 } 93 94 public void reset() { 95 updatedFields.clear(); 96 97 if (updatedJoinTableRelationships != null) { 98 updatedJoinTableRelationships.clear(); 99 } 100 101 relationshipChanged = false; 102 concurrency = null; 103 } 104 105 public boolean hasUpdatedFields() { 106 return (updatedFields.size() > 0); 107 } 108 109 public Collection getUpdatedJoinTableFields() { 110 if (updatedJoinTableRelationships == null) { 111 return null; 112 } 113 114 return updatedJoinTableRelationships.keySet(); 115 } 116 117 public Collection getUpdateJoinTableDescs(FieldDesc fieldDesc) { 119 HashMap updateJoinTableDescs = (HashMap) updatedJoinTableRelationships.get(fieldDesc); 120 121 if (updateJoinTableDescs != null) { 122 return updateJoinTableDescs.values(); 123 } 124 125 return null; 126 } 127 128 public boolean hasUpdatedJoinTableRelationships() { 129 return (updatedJoinTableRelationships != null && 130 updatedJoinTableRelationships.size() > 0); 131 } 132 133 136 public boolean hasModifiedLobField() { 137 138 if (updatedFields != null) { 139 for (Iterator i = updatedFields.iterator(); i.hasNext(); ) { 140 141 LocalFieldDesc field = (LocalFieldDesc)i.next(); 144 if (field.isMappedToLob()) { 145 return true; 146 } 147 } 148 } 149 150 return false; 151 } 152 153 160 public void markRelationshipChange(FieldDesc fieldDesc) { 161 if (fieldDesc.isRelationshipField() || fieldDesc.absoluteID < 0) { 162 if (logger.isLoggable(Logger.FINEST)) { 163 logger.finest("sqlstore.sql.updateobjdescimpl.markrelationshipchange"); } 165 relationshipChanged = true; 167 } 168 } 169 170 175 public boolean hasChangedRelationships() { 176 if (relationshipChanged) { 179 return true; 180 } 181 182 if (hasUpdatedJoinTableRelationships()) { 184 return true; 185 } 186 187 if (updatedFields != null) { 189 for (Iterator iter = updatedFields.iterator(); iter.hasNext(); ) { 190 LocalFieldDesc field = (LocalFieldDesc) iter.next(); 191 if (field.absoluteID < 0) { 192 return true; 193 } 194 } 195 } 196 197 return false; 198 } 199 200 212 public boolean removeUpdatedJoinTableRelationship(ForeignFieldDesc fieldDesc, 213 SQLStateManager foreignSM, 214 int action) { 215 HashMap updateJoinTableDescs = null; 216 217 if ((updatedJoinTableRelationships == null) || 218 ((updateJoinTableDescs = (HashMap) updatedJoinTableRelationships.get(fieldDesc)) == null)) { 219 return false; 220 } 221 222 UpdateJoinTableDesc desc = (UpdateJoinTableDesc) updateJoinTableDescs.get(foreignSM); 223 if (desc != null && desc.getAction() == action) { 224 return (updateJoinTableDescs.remove(foreignSM) != null); 225 } 226 227 return false; 228 } 229 230 243 public void recordUpdatedJoinTableRelationship(ForeignFieldDesc fieldDesc, 244 SQLStateManager parentSM, 245 SQLStateManager foreignSM, 246 int action) { 247 if (updatedJoinTableRelationships == null) { 248 updatedJoinTableRelationships = new HashMap(); 249 } 250 251 HashMap updateJoinTableDescs = null; 252 253 if ((updateJoinTableDescs = (HashMap) updatedJoinTableRelationships.get(fieldDesc)) == null) { 254 updateJoinTableDescs = new HashMap(); 255 updatedJoinTableRelationships.put(fieldDesc, updateJoinTableDescs); 256 } 257 258 UpdateJoinTableDesc desc = null; 259 260 if ((desc = (UpdateJoinTableDesc) updateJoinTableDescs.get(foreignSM)) == null) { 261 desc = new UpdateJoinTableDesc(parentSM, foreignSM, action); 262 updateJoinTableDescs.put(foreignSM, desc); 263 } 264 } 265 266 public void clearUpdatedJoinTableRelationships() { 267 updatedJoinTableRelationships = null; 268 } 269 270 public void recordUpdatedField(LocalFieldDesc fieldDesc) { 271 if (!updatedFields.contains(fieldDesc)) 272 updatedFields.add(fieldDesc); 273 } 274 275 public List getUpdatedFields() { 276 return updatedFields; 277 } 278 279 public Object getAfterValue(FieldDesc f) { 280 if (afterImage == null) { 281 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 282 "sqlstore.sql.updateobjdescimpl.afterimagenull")); } 284 285 if (f.absoluteID < 0) { 286 return afterHiddenValues.get(-(f.absoluteID + 1)); 287 } else { 288 return f.getValue(afterImage); 289 } 290 } 291 292 public Object getBeforeValue(FieldDesc f) { 293 if (beforeImage == null) { 294 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 295 "sqlstore.sql.updateobjdescimpl.beforeimagenull")); } 297 298 if (f.absoluteID < 0) { 299 return beforeHiddenValues.get(-(f.absoluteID + 1)); 300 } else { 301 return f.getValue(beforeImage); 302 } 303 } 304 305 public int getUpdateAction() { 306 return updateAction; 307 } 308 309 public ClassDesc getConfig() { 310 return (ClassDesc) afterImage.getPersistenceConfig(); 311 } 312 313 public SQLStateManager getAfterImage() { 314 return afterImage; 315 } 316 317 public boolean isBeforeImageRequired() { 318 return afterImage.isBeforeImageRequired(); 319 } 320 321 public Concurrency getConcurrency() { 322 return concurrency; 323 } 324 325 public void setConcurrency(Concurrency concurrency) { 326 this.concurrency = concurrency; 327 } 328 329 347 public void setObjectInfo(StateManager biStateManager, 348 StateManager aiStateManager, 349 int action) { 350 351 this.beforeImage = (SQLStateManager) biStateManager; 352 this.afterImage = (SQLStateManager) aiStateManager; 353 ClassDesc config = (ClassDesc) afterImage.getPersistenceConfig(); 354 updateAction = action; 355 356 this.afterHiddenValues = afterImage.hiddenValues; 357 358 if (beforeImage != null) { 359 this.beforeHiddenValues = beforeImage.hiddenValues; 360 } 361 362 366 boolean debug = logger.isLoggable(Logger.FINER); 367 368 for (int i = 0; i < config.fields.size(); i++) { 369 FieldDesc f = (FieldDesc) config.fields.get(i); 370 LocalFieldDesc lf = null; 371 boolean updated = false; 372 373 if (f instanceof LocalFieldDesc) { 374 lf = (LocalFieldDesc) f; 375 } else { 376 continue; 377 } 378 379 if ((updateAction == LOG_DESTROY) || 380 ((lf.sqlProperties & FieldDesc.PROP_RECORD_ON_UPDATE) > 0)) { 381 continue; 382 } else if (lf.absoluteID < 0) { 383 if ((beforeImage == null) || 384 (beforeImage.getHiddenValue(lf.absoluteID) != 385 afterImage.getHiddenValue(lf.absoluteID))) { 386 updated = true; 387 } 388 } else if (lf.getType().isPrimitive() || 389 String .class == lf.getType() || 390 java.util.Date .class == lf.getType()) { 391 Object afterVal = lf.getValue(afterImage); 392 Object beforeVal = null; 393 394 if (beforeImage != null) { 395 beforeVal = lf.getValue(beforeImage); 396 } 397 398 if ((beforeVal != null) && (afterVal != null)) { 399 if (!beforeVal.equals(afterVal)) { 400 updated = true; 401 } 402 } else { 403 updated = true; 404 } 405 } else { 406 } 408 409 if (updated) { 410 if (debug) { 411 logger.finer("sqlstore.sql.updateobjdescimpl.updated", f.getName()); } 413 414 updatedFields.add(lf); 415 } 416 } 417 418 if (concurrency != null) { 419 concurrency.commit(this, beforeImage, afterImage, updateAction); 420 } 421 } 422 423 434 public void incrementVersion() { 435 436 if (afterImage.hasVersionConsistency() 437 && updateAction == ActionDesc.LOG_UPDATE 438 && hasUpdatedFields()) { 439 440 afterImage.incrementVersion(); 441 } 442 } 443 444 447 public void setVerificationFailed() { 448 afterImage.setVerificationFailed(); 449 } 450 451 public boolean hasVersionConsistency() { 452 return afterImage.hasVersionConsistency(); 453 } 454 } 455 | Popular Tags |