1 21 package oracle.toplink.essentials.mappings.foundation; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.internal.descriptors.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.internal.sessions.*; 29 import oracle.toplink.essentials.mappings.DatabaseMapping; 30 import oracle.toplink.essentials.mappings.converters.*; 31 import oracle.toplink.essentials.queryframework.*; 32 import oracle.toplink.essentials.sessions.ObjectCopyingPolicy; 33 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 34 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 35 import oracle.toplink.essentials.internal.sessions.AbstractSession; 36 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager; 37 38 52 public abstract class AbstractDirectMapping extends DatabaseMapping { 53 54 55 protected DatabaseField field; 56 57 58 protected transient Class attributeClassification; 59 protected transient String attributeClassificationName; 60 61 62 protected transient Class attributeObjectClassification; 63 64 65 protected Converter converter; 66 67 68 protected transient Object nullValue; 69 70 75 protected Boolean isMutable; 76 77 80 public AbstractDirectMapping() { 81 super(); 82 this.setWeight(WEIGHT_1); 83 } 84 85 90 public Converter getConverter() { 91 return converter; 92 } 93 94 99 public void setConverter(Converter converter) { 100 this.converter = converter; 101 } 102 103 110 public boolean isMutable() { 111 if (isMutable == null) { 112 return false; 113 } 114 return isMutable.booleanValue(); 115 } 116 117 124 public void setIsMutable(boolean isMutable) { 125 if (isMutable == true) { 126 this.isMutable = Boolean.TRUE; 127 } else { 128 this.isMutable = Boolean.FALSE; 129 } 130 } 131 132 136 public void buildBackupClone(Object clone, Object backup, UnitOfWorkImpl unitOfWork) { 137 buildClone(clone, backup, unitOfWork, null); 138 } 139 140 144 public void buildClone(Object original, Object clone, UnitOfWorkImpl unitOfWork, JoinedAttributeManager joinedAttributeManager) { 145 buildCloneValue(original, clone, unitOfWork); 146 } 147 148 152 public void buildCloneValue(Object original, Object clone, AbstractSession session) { 153 if (isCloningRequired()) { 155 Object attributeValue = getAttributeValueFromObject(original); 156 if (isMutable()) { 157 attributeValue = getAttributeValue(getFieldValue(attributeValue, session), session); 158 } 159 setAttributeValueInObject(clone, attributeValue); 160 } 161 } 162 163 168 public void buildCopy(Object copy, Object original, ObjectCopyingPolicy policy) { 169 buildCloneValue(original, copy, policy.getSession()); 170 } 171 172 176 public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects) { 177 } 180 181 185 public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects) { 186 } 189 190 194 public Object clone() { 195 AbstractDirectMapping clone = (AbstractDirectMapping)super.clone(); 196 197 clone.setField((DatabaseField)getField().clone()); 199 200 return clone; 201 } 202 203 206 protected Vector collectFields() { 207 Vector databaseField = new Vector(1); 208 209 databaseField.addElement(getField()); 210 return databaseField; 211 } 212 213 217 public ChangeRecord compareForChange(Object clone, Object backUp, ObjectChangeSet owner, AbstractSession session) { 218 if ((owner.isNew()) || (!compareObjects(backUp, clone, session))) { 220 return buildChangeRecord(clone, owner, session); 221 } 222 return null; 223 } 224 225 229 public ChangeRecord buildChangeRecord(Object clone, ObjectChangeSet owner, AbstractSession session) { 230 return internalBuildChangeRecord(getAttributeValueFromObject(clone), owner); 231 } 232 233 237 public ChangeRecord internalBuildChangeRecord(Object newValue, ObjectChangeSet owner) { 238 DirectToFieldChangeRecord changeRecord = new DirectToFieldChangeRecord(owner); 239 changeRecord.setAttribute(getAttributeName()); 240 changeRecord.setMapping(this); 241 changeRecord.setNewValue(newValue); 242 return changeRecord; 243 } 244 245 249 public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) { 250 Object one = getAttributeValueFromObject(firstObject); 251 Object two = getAttributeValueFromObject(secondObject); 252 253 if (one == two) { 255 return true; 256 } 257 258 one = getFieldValue(one, session); 260 two = getFieldValue(two, session); 261 if (one == two) { 263 return true; 264 } 265 266 if ((one == null) || (two == null)) { 267 return false; 268 } 269 270 if ((one.getClass() == ClassConstants.APBYTE) && (two.getClass() == ClassConstants.APBYTE)) { 272 return Helper.compareByteArrays((byte[])one, (byte[])two); 273 } 274 if ((one.getClass() == ClassConstants.APCHAR) && (two.getClass() == ClassConstants.APCHAR)) { 275 return Helper.compareCharArrays((char[])one, (char[])two); 276 } 277 if ((one.getClass().isArray()) && (two.getClass().isArray())) { 278 return Helper.compareArrays((Object [])one, (Object [])two); 279 } 280 281 if (one instanceof java.math.BigDecimal && two instanceof java.math.BigDecimal ) { 283 return Helper.compareBigDecimals((java.math.BigDecimal )one, (java.math.BigDecimal )two); 284 } 285 286 return one.equals(two); 287 } 288 289 296 public void convertClassNamesToClasses(ClassLoader classLoader){ 297 super.convertClassNamesToClasses(classLoader); 298 if (converter != null) { 299 if (converter instanceof TypeConversionConverter) { 300 ((TypeConversionConverter)converter).convertClassNamesToClasses(classLoader); 301 } else if (converter instanceof ObjectTypeConverter) { 302 ((ObjectTypeConverter) converter).convertClassNamesToClasses(classLoader); 305 } 306 } 307 }; 308 309 315 public Class getAttributeClassification() { 316 return attributeClassification; 317 } 318 319 public String getAttributeClassificationName() { 320 return attributeClassificationName; 321 } 322 323 327 public Object getAttributeValue(Object fieldValue, AbstractSession session) { 328 Object attributeValue = fieldValue; 330 if ((fieldValue == null) && (getNullValue() != null)) { return this.nullValue; 332 } 333 334 if (this.converter != null) { 336 attributeValue = this.converter.convertDataValueToObjectValue(attributeValue, session); 337 } else { 338 if ((attributeValue == null) || (attributeValue.getClass() != this.attributeObjectClassification)) { 340 try { 341 attributeValue = session.getDatasourcePlatform().convertObject(attributeValue, this.attributeClassification); 342 } catch (ConversionException e) { 343 throw ConversionException.couldNotBeConverted(this, getDescriptor(), e); 344 } 345 } 346 } 347 if (attributeValue == null) { attributeValue = this.nullValue; 349 } 350 351 return attributeValue; 352 } 353 354 358 public DatabaseField getField() { 359 return field; 360 } 361 362 365 public boolean isAbstractDirectMapping() { 366 return true; 367 } 368 369 374 public Class getFieldClassification(DatabaseField fieldToClassify) { 375 if (fieldToClassify.type != null) { 378 return fieldToClassify.type; 379 } else { 380 if (this.converter != null) { 381 return null; 382 } else { 383 return this.attributeClassification; 384 } 385 } 386 } 387 388 394 public Class getFieldClassification() { 395 if (getField() == null) { 396 return null; 397 } 398 return getField().getType(); 399 } 400 401 408 public void setFieldClassification(Class fieldType) { 409 getField().setType(fieldType); 410 } 411 412 419 public void setFieldType(int jdbcType) { 420 getField().setSqlType(jdbcType); 421 } 422 423 427 public String getFieldName() { 428 return getField().getQualifiedName(); 429 } 430 431 436 public Object getFieldValue(Object attributeValue, AbstractSession session) { 437 Object fieldValue = attributeValue; 440 if ((this.nullValue != null) && (this.nullValue.equals(fieldValue))) { 441 return null; 442 } 443 444 if (this.converter != null) { 446 fieldValue = this.converter.convertObjectValueToDataValue(fieldValue, session); 447 } 448 Class fieldClassification = getFieldClassification(getField()); 449 if ((fieldValue == null) || (fieldClassification != fieldValue.getClass())) { 451 try { 452 fieldValue = session.getPlatform(getDescriptor().getJavaClass()).convertObject(fieldValue, fieldClassification); 453 } catch (ConversionException exception) { 454 throw ConversionException.couldNotBeConverted(this, getDescriptor(), exception); 455 } 456 } 457 return fieldValue; 458 } 459 460 467 public Object getNullValue() { 468 return nullValue; 469 } 470 471 476 public Integer getWeight() { 477 return this.weight; 478 } 479 480 484 public void preInitialize(AbstractSession session) throws DescriptorException { 485 super.preInitialize(session); 486 this.attributeClassification = getAttributeAccessor().getAttributeClass(); 487 this.attributeObjectClassification = Helper.getObjectClass(this.attributeClassification); 488 } 489 490 495 public void initialize(AbstractSession session) throws DescriptorException { 496 super.initialize(session); 497 498 if (this.isMutable == null) { 500 if (getConverter() != null) { 501 setIsMutable(getConverter().isMutable()); 502 } else { 503 setIsMutable(false); 504 } 505 } 506 507 if (getField() == null) { 508 session.getIntegrityChecker().handleError(DescriptorException.fieldNameNotSetInMapping(this)); 509 } 510 511 getDescriptor().buildField(getField()); 512 setFields(collectFields()); 513 514 if (getConverter() != null) { 515 getConverter().initialize(this, session); 516 } 517 } 518 519 522 public boolean isDirectToFieldMapping() { 523 return true; 524 } 525 526 530 public void iterate(DescriptorIterator iterator) { 531 if (iterator.shouldIterateOnPrimitives()) { 533 iterator.iteratePrimitiveForMapping(getAttributeValueFromObject(iterator.getVisitedParent()), this); 534 } 535 } 536 537 541 public void mergeChangesIntoObject(Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager) { 542 setAttributeValueInObject(target, ((DirectToFieldChangeRecord)changeRecord).getNewValue()); 543 } 544 545 550 public void mergeIntoObject(Object target, boolean isTargetUnInitialized, Object source, MergeManager mergeManager) { 551 Object attributeValue = getAttributeValueFromObject(source); 552 if ( (this.getDescriptor().getObjectChangePolicy().isObjectChangeTrackingPolicy()) && (!compareObjects(target, source, mergeManager.getSession())) ) { 553 Object targetAttribute = getAttributeValueFromObject(target); 555 setAttributeValueInObject(target, attributeValue); 556 this.getDescriptor().getObjectChangePolicy().raiseInternalPropertyChangeEvent(target, getAttributeName(), targetAttribute, attributeValue); 559 }else{ 560 setAttributeValueInObject(target, attributeValue); 562 } 563 } 564 565 571 public void setAttributeClassification(Class attributeClassification) { 572 this.attributeClassification = attributeClassification; 573 } 574 575 579 public void setAttributeClassificationName(String attributeClassificationName) { 580 this.attributeClassificationName = attributeClassificationName; 581 } 582 583 588 public void setField(DatabaseField theField) { 589 field = theField; 590 } 591 592 599 public void setNullValue(Object nullValue) { 600 this.nullValue = nullValue; 601 } 602 603 606 public String toString() { 607 return getClass().getName() + "[" + getAttributeName() + "-->" + getField() + "]"; 608 } 609 610 615 public void updateChangeRecord(Object clone, Object newValue, Object oldValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) { 616 DirectToFieldChangeRecord changeRecord = (DirectToFieldChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); 617 if (changeRecord == null) { 618 objectChangeSet.addChange(internalBuildChangeRecord(newValue, objectChangeSet)); 619 } else { 620 changeRecord.setNewValue(newValue); 621 } 622 } 623 624 628 public boolean isChangeTrackingSupported() { 629 return !isMutable(); 630 } 631 632 636 public boolean isCloningRequired() { 637 return isMutable() || getDescriptor().getCopyPolicy().buildsNewInstance(); 638 } 639 640 644 public void validateBeforeInitialization(AbstractSession session) throws DescriptorException { 645 if ((getFieldName() == null) || (getFieldName().length() == 0)) { 646 session.getIntegrityChecker().handleError(DescriptorException.noFieldNameForMapping(this)); 647 } 648 } 649 650 654 public Object valueFromObject(Object object, DatabaseField field, AbstractSession session) throws DescriptorException { 655 return getFieldValue(getAttributeValueFromObject(object), session); 656 } 657 658 665 public void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession) { 666 Object attributeValue = valueFromRow(databaseRow, joinManager, sourceQuery, executionSession); 671 672 setAttributeValueInObject(clone, attributeValue); 673 } 674 675 683 public void buildShallowOriginalFromRow(AbstractRecord databaseRow, Object original, ObjectBuildingQuery query, AbstractSession executionSession) { 684 readFromRowIntoObject(databaseRow, null, original, query, executionSession); 685 } 686 687 697 public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, AbstractSession executionSession) { 698 Object fieldValue = row.get(this.field); 700 Object attributeValue = getAttributeValue(fieldValue, executionSession); 701 702 return attributeValue; 703 } 704 705 709 public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session) { 710 if (isReadOnly()) { 711 return; 712 } 713 714 Object attributeValue = getAttributeValueFromObject(object); 715 Object fieldValue = getFieldValue(attributeValue, session); 716 717 writeValueIntoRow(row, getField(), fieldValue); 718 719 } 720 721 protected abstract void writeValueIntoRow(AbstractRecord row, DatabaseField field, Object value); 722 723 727 public void writeFromObjectIntoRowWithChangeRecord(ChangeRecord changeRecord, AbstractRecord row, AbstractSession session) { 728 if (isReadOnly()) { 729 return; 730 } 731 732 Object attributeValue = ((DirectToFieldChangeRecord)changeRecord).getNewValue(); 733 Object fieldValue = getFieldValue(attributeValue, session); 734 735 row.add(getField(), fieldValue); 736 } 737 738 742 public void writeFromObjectIntoRowForUpdate(WriteObjectQuery query, AbstractRecord aDatabaseRow) { 743 if (query.getSession().isUnitOfWork()) { 744 if (compareObjects(query.getBackupClone(), query.getObject(), query.getSession())) { 745 return; 746 } 747 } 748 749 super.writeFromObjectIntoRowForUpdate(query, aDatabaseRow); 750 } 751 752 756 public void writeInsertFieldsIntoRow(AbstractRecord databaseRow, AbstractSession session) { 757 if (isReadOnly()) { 758 return; 759 } 760 761 databaseRow.add(getField(), null); 762 } 763 } 764 | Popular Tags |