1 21 package oracle.toplink.essentials.internal.descriptors; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.expressions.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.internal.identitymaps.*; 31 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager; 32 import oracle.toplink.essentials.internal.sessions.*; 33 import oracle.toplink.essentials.logging.SessionLog; 34 import oracle.toplink.essentials.mappings.*; 35 import oracle.toplink.essentials.mappings.foundation.*; 36 import oracle.toplink.essentials.queryframework.*; 37 import oracle.toplink.essentials.querykeys.*; 38 import oracle.toplink.essentials.descriptors.DescriptorEventManager; 39 import oracle.toplink.essentials.sessions.ObjectCopyingPolicy; 40 import oracle.toplink.essentials.sessions.SessionProfiler; 41 import oracle.toplink.essentials.sessions.DatabaseRecord; 42 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 43 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 44 import oracle.toplink.essentials.internal.sessions.AbstractSession; 45 import oracle.toplink.essentials.descriptors.ClassDescriptor; 46 47 55 public class ObjectBuilder implements Cloneable , Serializable { 56 protected ClassDescriptor descriptor; 57 protected Map mappingsByAttribute; 58 protected Map mappingsByField; 59 protected Map readOnlyMappingsByField; 60 protected Vector primaryKeyMappings; 61 protected Vector primaryKeyClassifications; 62 protected transient Vector nonPrimaryKeyMappings; 63 protected transient Expression primaryKeyExpression; 64 65 66 protected Vector joinedAttributes = null; 67 68 69 protected List cloningMappings; 70 71 public ObjectBuilder(ClassDescriptor descriptor) { 72 this.mappingsByField = new HashMap(20); 73 this.readOnlyMappingsByField = new HashMap(20); 74 this.mappingsByAttribute = new HashMap(20); 75 this.primaryKeyMappings = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(5); 76 this.nonPrimaryKeyMappings = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(10); 77 this.cloningMappings = new ArrayList(10); 78 this.descriptor = descriptor; 79 } 80 81 85 public AbstractRecord createRecord() { 86 return new DatabaseRecord(); 87 } 88 89 93 public AbstractRecord createRecord(int size) { 94 return new DatabaseRecord(size); 95 } 96 97 101 public void addPrimaryKeyForNonDefaultTable(AbstractRecord databaseRow) { 102 addPrimaryKeyForNonDefaultTable(databaseRow, null, null); 105 } 106 107 111 public void addPrimaryKeyForNonDefaultTable(AbstractRecord databaseRow, Object object, AbstractSession session) { 112 if (!getDescriptor().hasMultipleTables()) { 113 return; 114 } 115 Enumeration tablesEnum = getDescriptor().getTables().elements(); 116 117 tablesEnum.nextElement(); 119 while (tablesEnum.hasMoreElements()) { 120 DatabaseTable table = (DatabaseTable)tablesEnum.nextElement(); 121 Map keyMapping = (Map)getDescriptor().getAdditionalTablePrimaryKeyFields().get(table); 122 123 if (keyMapping != null) { 126 Iterator primaryKeyFieldEnum = keyMapping.keySet().iterator(); 127 Iterator secondaryKeyFieldEnum = keyMapping.values().iterator(); 128 while (primaryKeyFieldEnum.hasNext()) { 129 DatabaseField primaryKeyField = (DatabaseField)primaryKeyFieldEnum.next(); 130 DatabaseField secondaryKeyField = (DatabaseField)secondaryKeyFieldEnum.next(); 131 Object primaryValue = databaseRow.get(primaryKeyField); 132 133 if ((primaryValue == null) && (!databaseRow.containsKey(primaryKeyField))) { 136 if (object != null) { 137 DatabaseMapping mapping = getMappingForField(secondaryKeyField); 138 if (mapping == null) { 139 throw DescriptorException.missingMappingForField(secondaryKeyField, getDescriptor()); 140 } 141 mapping.writeFromObjectIntoRow(object, databaseRow, session); 142 } 143 databaseRow.put(primaryKeyField, databaseRow.get(secondaryKeyField)); 144 } else { 145 databaseRow.put(secondaryKeyField, primaryValue); 146 } 147 } 148 } 149 } 150 } 151 152 156 public void assignReturnRow(Object object, AbstractSession writeSession, AbstractRecord row) throws DatabaseException { 157 writeSession.log(SessionLog.FINEST, SessionLog.QUERY, "assign_return_row", row); 158 159 ReadObjectQuery query = new ReadObjectQuery(); 161 query.setSession(writeSession); 162 163 HashSet handledMappings = new HashSet(row.size()); 166 for (int index = 0; index < row.size(); index++) { 167 DatabaseField field = (DatabaseField)row.getFields().elementAt(index); 168 assignReturnValueForField(object, query, row, field, handledMappings); 169 } 170 } 171 172 176 public void assignReturnValueForField(Object object, ReadObjectQuery query, AbstractRecord row, DatabaseField field, Collection handledMappings) { 177 DatabaseMapping mapping = getMappingForField(field); 178 if (mapping != null) { 179 assignReturnValueToMapping(object, query, row, field, mapping, handledMappings); 180 } 181 Vector mappingVector = getReadOnlyMappingsForField(field); 182 if (mappingVector != null) { 183 for (int j = 0; j < mappingVector.size(); j++) { 184 mapping = (DatabaseMapping)mappingVector.elementAt(j); 185 assignReturnValueToMapping(object, query, row, field, mapping, handledMappings); 186 } 187 } 188 } 189 190 194 protected void assignReturnValueToMapping(Object object, ReadObjectQuery query, AbstractRecord row, DatabaseField field, DatabaseMapping mapping, Collection handledMappings) { 195 if (handledMappings.contains(mapping)) { 196 return; 197 } 198 Object attributeValue; 199 if (mapping.isAggregateObjectMapping()) { 200 attributeValue = ((AggregateObjectMapping)mapping).readFromReturnRowIntoObject(row, object, query, handledMappings); 201 } else if (mapping.isDirectToFieldMapping()) { 202 attributeValue = mapping.readFromRowIntoObject(row, null, object, query); 203 } else { 204 query.getSession().log(SessionLog.FINEST, SessionLog.QUERY, "field_for_unsupported_mapping_returned", field, getDescriptor()); 205 } 206 } 207 208 215 public Object assignSequenceNumber(Object object, AbstractSession writeSession) throws DatabaseException { 216 DatabaseField sequenceNumberField = getDescriptor().getSequenceNumberField(); 217 Object existingValue = getBaseValueForField(sequenceNumberField, object); 218 219 if (existingValue != null) { 221 if (!writeSession.getSequencing().shouldOverrideExistingValue(object.getClass(), existingValue)) { 222 return null; 223 } 224 } 225 Object sequenceValue = writeSession.getSequencing().getNextValue(object.getClass()); 226 227 writeSession.log(SessionLog.FINEST, SessionLog.SEQUENCING, "assign_sequence", sequenceValue, object); 229 230 if (sequenceValue == null) { 232 return null; 233 } 234 235 AbstractRecord tempRow = createRecord(1); 237 tempRow.put(sequenceNumberField, sequenceValue); 238 239 ReadObjectQuery query = new ReadObjectQuery(); 241 query.setSession(writeSession); 242 DatabaseMapping mapping = getBaseMappingForField(sequenceNumberField); 243 Object sequenceIntoObject = getParentObjectForField(sequenceNumberField, object); 244 245 Object convertedSequenceValue = mapping.readFromRowIntoObject(tempRow, null, sequenceIntoObject, query); 247 248 return convertedSequenceValue; 249 } 250 251 254 public void buildAttributesIntoObject(Object domainObject, AbstractRecord databaseRow, ObjectBuildingQuery query, JoinedAttributeManager joinManager, boolean forRefresh) throws DatabaseException { 255 AbstractSession executionSession = query.getSession().getExecutionSession(query); 256 257 Vector mappings = getDescriptor().getMappings(); 259 260 boolean readAllMappings = query.shouldReadAllMappings(); 262 int mappingsSize = mappings.size(); 263 for (int index = 0; index < mappingsSize; index++) { 264 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 265 if (readAllMappings || query.shouldReadMapping(mapping)) { 266 mapping.readFromRowIntoObject(databaseRow, joinManager, domainObject, query, executionSession); 267 } 268 } 269 270 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 272 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(domainObject); 275 event.setQuery(query); 276 event.setSession(query.getSession()); 277 event.setRecord(databaseRow); 278 if (forRefresh) { 279 event.setEventCode(DescriptorEventManager.PostRefreshEvent); 284 } else { 285 event.setEventCode(DescriptorEventManager.PostBuildEvent); 286 } 287 getDescriptor().getEventManager().executeEvent(event); 288 } 289 } 290 291 295 public Object buildBackupClone(Object clone, UnitOfWorkImpl unitOfWork) { 296 Object backup = getDescriptor().getCopyPolicy().buildClone(clone, unitOfWork); 298 299 List mappings = getCloningMappings(); 301 for (int index = 0; index < mappings.size(); index++) { 302 ((DatabaseMapping)mappings.get(index)).buildBackupClone(clone, backup, unitOfWork); 303 } 304 305 return backup; 306 } 307 308 312 public Expression buildDeleteExpression(DatabaseTable table, AbstractRecord row) { 313 if (getDescriptor().usesOptimisticLocking() && (getDescriptor().getTables().firstElement().equals(table))) { 314 return getDescriptor().getOptimisticLockingPolicy().buildDeleteExpression(table, primaryKeyExpression, row); 315 } else { 316 return buildPrimaryKeyExpression(table); 317 } 318 } 319 320 323 public Object buildNewInstance() { 324 return getDescriptor().getInstantiationPolicy().buildNewInstance(); 325 } 326 327 331 public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) throws DatabaseException, QueryException { 332 AbstractSession session = query.getSession(); 334 session.startOperationProfile(SessionProfiler.OBJECT_BUILDING); 335 336 Vector primaryKey = extractPrimaryKeyFromRow(databaseRow, session); 337 338 if ((primaryKey == null) && (!query.hasPartialAttributeExpressions()) && (!getDescriptor().isAggregateCollectionDescriptor())) { 340 session.endOperationProfile(SessionProfiler.OBJECT_BUILDING); 342 343 if (query.getProperty("return null if primary key is null") != null) { 349 return null; 350 } else { 351 throw QueryException.nullPrimaryKeyInBuildingObject(query, databaseRow); 352 } 353 } 354 ClassDescriptor concreteDescriptor = getDescriptor(); 355 if (concreteDescriptor.hasInheritance() && concreteDescriptor.getInheritancePolicy().shouldReadSubclasses()) { 356 Class classValue = concreteDescriptor.getInheritancePolicy().classFromRow(databaseRow, session); 357 concreteDescriptor = session.getDescriptor(classValue); 358 if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) { 359 concreteDescriptor = getDescriptor(); 360 } 361 if (concreteDescriptor == null) { 362 session.endOperationProfile(SessionProfiler.OBJECT_BUILDING); 364 throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue); 365 } 366 } 367 Object domainObject = null; 368 try { 369 if (session.isUnitOfWork()) { 370 domainObject = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl)session, primaryKey, concreteDescriptor); 373 } else { 374 domainObject = buildObject(query, databaseRow, session, primaryKey, concreteDescriptor, joinManager); 375 376 if (query.shouldUseWrapperPolicy()) { 378 domainObject = concreteDescriptor.getObjectBuilder().wrapObject(domainObject, session); 379 } 380 } 381 } finally { 382 session.endOperationProfile(SessionProfiler.OBJECT_BUILDING); 383 } 384 return domainObject; 385 } 386 387 397 protected Object buildObjectInUnitOfWork(ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Vector primaryKey, ClassDescriptor concreteDescriptor) throws DatabaseException, QueryException { 398 if ((unitOfWork.getCommitManager().isActive() || unitOfWork.wasTransactionBegunPrematurely()) && !unitOfWork.isClassReadOnly(concreteDescriptor.getJavaClass())) { 402 return concreteDescriptor.getObjectBuilder().buildWorkingCopyCloneFromRow(query, joinManager, databaseRow, unitOfWork, primaryKey); 404 } 405 406 return buildWorkingCopyCloneNormally(query, databaseRow, unitOfWork, primaryKey, concreteDescriptor, joinManager); 407 } 408 409 422 protected Object buildWorkingCopyCloneNormally(ObjectBuildingQuery query, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Vector primaryKey, ClassDescriptor concreteDescriptor, JoinedAttributeManager joinManager) throws DatabaseException, QueryException { 423 AbstractSession session = unitOfWork.getParentIdentityMapSession(query); 427 Object original = null; 428 Object clone = null; 429 430 query.setSession(session); 433 if (session.isUnitOfWork()) { 434 original = buildObjectInUnitOfWork(query, joinManager, databaseRow, (UnitOfWorkImpl)session, primaryKey, concreteDescriptor); 435 } else { 436 original = buildObject(query, databaseRow, session, primaryKey, concreteDescriptor, joinManager); 437 } 438 query.setSession(unitOfWork); 439 if (unitOfWork.shouldCascadeCloneToJoinedRelationship()) { 441 clone = query.registerIndividualResult(original, unitOfWork, false, joinManager); } else { 443 clone = query.registerIndividualResult(original, unitOfWork, false, null); } 445 return clone; 446 } 447 448 452 protected Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, AbstractSession session, Vector primaryKey, ClassDescriptor concreteDescriptor, JoinedAttributeManager joinManager) throws DatabaseException, QueryException { 453 Object domainObject = null; 454 455 CacheKey cacheKey = null; 457 try { 458 if (query.shouldMaintainCache()) { 460 if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || joinManager.hasJoinedAttributes())) { 464 cacheKey = session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor); 465 domainObject = cacheKey.getObject(); 466 467 int counter = 0; 468 while ((domainObject == null) && (counter < 1000)) { 469 if (cacheKey.getMutex().getActiveThread() == Thread.currentThread()) { 470 break; 471 } 472 cacheKey.releaseDeferredLock(); 476 477 try { 480 Thread.sleep(10); 481 } catch (InterruptedException exception) { 482 } 483 cacheKey = session.getIdentityMapAccessorInstance().acquireDeferredLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor); 484 domainObject = cacheKey.getObject(); 485 counter++; 486 } 487 if (counter == 1000) { 488 throw ConcurrencyException.maxTriesLockOnBuildObjectExceded(cacheKey.getMutex().getActiveThread(), Thread.currentThread()); 489 } 490 } else { 491 cacheKey = session.getIdentityMapAccessorInstance().acquireLock(primaryKey, concreteDescriptor.getJavaClass(), concreteDescriptor); 492 domainObject = cacheKey.getObject(); 493 } 494 } 495 496 if (domainObject == null) { 497 if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) { 498 domainObject = ((ReadObjectQuery)query).getSelectionObject(); 499 } else { 500 domainObject = concreteDescriptor.getObjectBuilder().buildNewInstance(); 501 } 502 503 if (query.shouldMaintainCache()) { 505 cacheKey.setObject(domainObject); 506 507 copyQueryInfoToCacheKey(cacheKey, query, databaseRow, session, concreteDescriptor); 508 509 if (concreteDescriptor.hasFetchGroupManager()) { 511 concreteDescriptor.getFetchGroupManager().setObjectFetchGroup(domainObject, query.getFetchGroup()); 512 } 513 } 514 515 concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, databaseRow, query, joinManager, false); 516 } else { 517 if (query.isReadObjectQuery() && ((ReadObjectQuery)query).shouldLoadResultIntoSelectionObject()) { 518 copyInto(domainObject, ((ReadObjectQuery)query).getSelectionObject()); 519 domainObject = ((ReadObjectQuery)query).getSelectionObject(); 520 } 521 522 boolean isInvalidated = concreteDescriptor.getCacheInvalidationPolicy().isInvalidated(cacheKey, query.getExecutionTime()); 524 525 if ((cacheKey.getMutex().getActiveThread() == Thread.currentThread()) && ((query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache() || isInvalidated) && ((cacheKey.getLastUpdatedQueryId() != query.getQueryId()) && !cacheKey.getMutex().isLockedByMergeManager()))) { 530 if (concreteDescriptor.hasFetchGroupManager() && concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject)) { 533 revertFetchGroupData(domainObject, concreteDescriptor, cacheKey, ((ObjectLevelReadQuery)query), joinManager, databaseRow, session); 535 } else { 536 boolean refreshRequired = true; 537 if (concreteDescriptor.usesOptimisticLocking()) { 538 OptimisticLockingPolicy policy = concreteDescriptor.getOptimisticLockingPolicy(); 539 Object cacheValue = policy.getValueToPutInCache(databaseRow, session); 540 if (concreteDescriptor.shouldOnlyRefreshCacheIfNewerVersion()) { 541 refreshRequired = policy.isNewerVersion(databaseRow, domainObject, primaryKey, session); 542 if (!refreshRequired) { 543 cacheKey.setReadTime(query.getExecutionTime()); 544 } 545 } 546 if (refreshRequired) { 547 cacheKey.setWriteLockValue(cacheValue); 549 } 550 } 551 if (refreshRequired) { 552 cacheKey.setLastUpdatedQueryId(query.getQueryId()); 554 concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, databaseRow, query, joinManager, true); 555 cacheKey.setReadTime(query.getExecutionTime()); 556 } 557 } 558 } else if (concreteDescriptor.hasFetchGroupManager() && (concreteDescriptor.getFetchGroupManager().isPartialObject(domainObject) && (!concreteDescriptor.getFetchGroupManager().isObjectValidForFetchGroup(domainObject, query.getFetchGroup())))) { 559 concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, databaseRow, query, joinManager, false); 562 concreteDescriptor.getFetchGroupManager().unionFetchGroupIntoObject(domainObject, query.getFetchGroup()); 563 } 564 else if (joinManager.hasJoinedAttributeExpressions()) { 567 for (Iterator e = joinManager.getJoinedAttributeExpressions().iterator(); 568 e.hasNext();) { 569 QueryKeyExpression qke = (QueryKeyExpression)e.next(); 570 571 if (qke.getBaseExpression().isExpressionBuilder()) { 573 DatabaseMapping dm = getMappingForAttributeName(qke.getName()); 574 575 if (dm == null) { 576 throw ValidationException.missingMappingForAttribute(concreteDescriptor, qke.getName(), this.toString()); 577 } else { 578 Object attributeValue = dm.getAttributeValueFromObject(domainObject); 580 if (!((attributeValue != null) && dm.isForeignReferenceMapping() && ((ForeignReferenceMapping)dm).usesIndirection() && ((ForeignReferenceMapping)dm).getIndirectionPolicy().objectIsInstantiated(attributeValue))) { 581 dm.readFromRowIntoObject(databaseRow, joinManager, domainObject, query, query.getSession().getExecutionSession(query)); 582 } 583 } 584 } 585 } 586 } 587 } 588 } finally { 589 if (query.shouldMaintainCache() && (cacheKey != null)) { 590 if (cacheKey.getObject() != null) { 594 cacheKey.updateAccess(); 595 } 596 597 if (DeferredLockManager.SHOULD_USE_DEFERRED_LOCKS && (concreteDescriptor.shouldAcquireCascadedLocks() || joinManager.hasJoinedAttributes())) { 599 cacheKey.releaseDeferredLock(); 600 } else { 601 cacheKey.release(); 602 } 603 } 604 } 605 606 return domainObject; 607 } 608 609 612 private void revertFetchGroupData(Object domainObject, ClassDescriptor concreteDescriptor, CacheKey cacheKey, ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, AbstractSession session) { 613 concreteDescriptor.getFetchGroupManager().reset(domainObject); 616 concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, databaseRow, query, joinManager, false); 618 concreteDescriptor.getFetchGroupManager().setObjectFetchGroup(domainObject, query.getFetchGroup()); 620 concreteDescriptor.getFetchGroupManager().setRefreshOnFetchGroupToObject(domainObject, (query.shouldRefreshIdentityMapResult() || concreteDescriptor.shouldAlwaysRefreshCache())); 622 cacheKey.setLastUpdatedQueryId(query.getQueryId()); 624 if (concreteDescriptor.usesOptimisticLocking()) { 626 OptimisticLockingPolicy policy = concreteDescriptor.getOptimisticLockingPolicy(); 627 cacheKey.setWriteLockValue(policy.getValueToPutInCache(databaseRow, session)); 628 } 629 cacheKey.setReadTime(query.getExecutionTime()); 630 cacheKey.setInvalidationState(CacheKey.CHECK_INVALIDATION_POLICY); 632 } 633 634 638 public Object buildObjectsInto(ReadAllQuery query, Vector databaseRows, Object domainObjects) throws DatabaseException { 639 Set identitySet = null; 640 for (Enumeration rowsEnum = databaseRows.elements(); rowsEnum.hasMoreElements();) { 641 AbstractRecord databaseRow = (AbstractRecord)rowsEnum.nextElement(); 642 643 if (databaseRow != null) { 645 Object domainObject = buildObject(query, databaseRow, query.getJoinedAttributeManager()); 646 647 if (query.getJoinedAttributeManager().isToManyJoin()) { 649 if (identitySet == null) { 650 identitySet = new TopLinkIdentityHashSet(databaseRows.size()); 651 } 652 if (!identitySet.contains(domainObject)) { 653 identitySet.add(domainObject); 654 query.getContainerPolicy().addInto(domainObject, domainObjects, query.getSession()); 655 } 656 } else { 657 query.getContainerPolicy().addInto(domainObject, domainObjects, query.getSession()); 658 } 659 } 660 } 661 662 return domainObjects; 663 } 664 665 668 public Expression buildPrimaryKeyExpression(DatabaseTable table) throws DescriptorException { 669 if (getDescriptor().getTables().firstElement().equals(table)) { 670 return getPrimaryKeyExpression(); 671 } 672 673 Map keyMapping = (Map)getDescriptor().getAdditionalTablePrimaryKeyFields().get(table); 674 if (keyMapping == null) { 675 throw DescriptorException.multipleTablePrimaryKeyNotSpecified(getDescriptor()); 676 } 677 678 ExpressionBuilder builder = new ExpressionBuilder(); 679 Expression expression = null; 680 for (Iterator primaryKeyEnum = keyMapping.values().iterator(); primaryKeyEnum.hasNext();) { 681 DatabaseField field = (DatabaseField)primaryKeyEnum.next(); 682 expression = (builder.getField(field).equal(builder.getParameter(field))).and(expression); 683 } 684 685 return expression; 686 } 687 688 691 public Expression buildPrimaryKeyExpressionFromKeys(Vector primaryKeyValues, AbstractSession session) { 692 Expression expression = null; 693 Expression subExpression; 694 Expression builder = new ExpressionBuilder(); 695 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 696 697 for (int index = 0; index < primaryKeyFields.size(); index++) { 698 Object value = primaryKeyValues.get(index); 699 DatabaseField field = (DatabaseField)primaryKeyFields.get(index); 700 if (value != null) { 701 subExpression = builder.getField(field).equal(value); 702 expression = subExpression.and(expression); 703 } 704 } 705 706 return expression; 707 } 708 709 712 public Expression buildPrimaryKeyExpressionFromObject(Object domainObject, AbstractSession session) { 713 return buildPrimaryKeyExpressionFromKeys(extractPrimaryKeyFromObject(domainObject, session), session); 714 } 715 716 719 public AbstractRecord buildRow(Object object, AbstractSession session) { 720 return buildRow(createRecord(), object, session); 721 } 722 723 726 public AbstractRecord buildRow(AbstractRecord databaseRow, Object object, AbstractSession session) { 727 Vector mappings = getDescriptor().getMappings(); 729 int mappingsSize = mappings.size(); 730 for (int index = 0; index < mappingsSize; index++) { 731 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 732 mapping.writeFromObjectIntoRow(object, databaseRow, session); 733 } 734 735 if (getDescriptor().hasInheritance()) { 737 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 738 } 739 740 if (!getDescriptor().isAggregateDescriptor()) { 743 addPrimaryKeyForNonDefaultTable(databaseRow); 744 } 745 746 return databaseRow; 747 } 748 749 753 public AbstractRecord buildRowForShallowInsert(Object object, AbstractSession session) { 754 return buildRowForShallowInsert(createRecord(), object, session); 755 } 756 757 761 public AbstractRecord buildRowForShallowInsert(AbstractRecord databaseRow, Object object, AbstractSession session) { 762 Vector mappings = getDescriptor().getMappings(); 764 int mappingsSize = mappings.size(); 765 for (int index = 0; index < mappingsSize; index++) { 766 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 767 mapping.writeFromObjectIntoRowForShallowInsert(object, databaseRow, session); 768 } 769 770 if (getDescriptor().hasInheritance()) { 772 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 773 } 774 775 if (!getDescriptor().isAggregateDescriptor()) { 778 addPrimaryKeyForNonDefaultTable(databaseRow); 779 } 780 781 return databaseRow; 782 } 783 784 787 public AbstractRecord buildRowWithChangeSet(ObjectChangeSet objectChangeSet, AbstractSession session) { 788 return buildRowWithChangeSet(createRecord(), objectChangeSet, session); 789 } 790 791 794 public AbstractRecord buildRowWithChangeSet(AbstractRecord databaseRow, ObjectChangeSet objectChangeSet, AbstractSession session) { 795 for (Enumeration changeRecords = objectChangeSet.getChanges().elements(); 796 changeRecords.hasMoreElements();) { 797 ChangeRecord changeRecord = (ChangeRecord)changeRecords.nextElement(); 798 DatabaseMapping mapping = changeRecord.getMapping(); 799 mapping.writeFromObjectIntoRowWithChangeRecord(changeRecord, databaseRow, session); 800 } 801 802 if (getDescriptor().hasInheritance()) { 804 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 805 } 806 807 return databaseRow; 808 } 809 810 814 public AbstractRecord buildRowForShallowInsertWithChangeSet(ObjectChangeSet objectChangeSet, AbstractSession session) { 815 return buildRowForShallowInsertWithChangeSet(createRecord(), objectChangeSet, session); 816 } 817 818 822 public AbstractRecord buildRowForShallowInsertWithChangeSet(AbstractRecord databaseRow, ObjectChangeSet objectChangeSet, AbstractSession session) { 823 for (Iterator changeRecords = objectChangeSet.getChanges().iterator(); 824 changeRecords.hasNext();) { 825 ChangeRecord changeRecord = (ChangeRecord)changeRecords.next(); 826 DatabaseMapping mapping = changeRecord.getMapping(); 827 mapping.writeFromObjectIntoRowForShallowInsertWithChangeRecord(changeRecord, databaseRow, session); 828 } 829 830 if (getDescriptor().hasInheritance()) { 832 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 833 } 834 835 if (!getDescriptor().isAggregateDescriptor()) { 838 addPrimaryKeyForNonDefaultTable(databaseRow); 839 } 840 841 return databaseRow; 842 } 843 844 848 public AbstractRecord buildRowForTranslation(Object object, AbstractSession session) { 849 AbstractRecord databaseRow = createRecord(); 850 851 for (Iterator mappings = getPrimaryKeyMappings().iterator(); mappings.hasNext();) { 852 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 853 if (mapping != null) { 854 mapping.writeFromObjectIntoRow(object, databaseRow, session); 855 } 856 } 857 858 addPrimaryKeyForNonDefaultTable(databaseRow, object, session); 862 863 return databaseRow; 864 } 865 866 870 public AbstractRecord buildRowForUpdate(WriteObjectQuery query) { 871 AbstractRecord databaseRow = createRecord(); 872 873 for (Iterator mappings = getNonPrimaryKeyMappings().iterator(); 874 mappings.hasNext();) { 875 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 876 mapping.writeFromObjectIntoRowForUpdate(query, databaseRow); 877 } 878 879 if (getDescriptor().hasInheritance() && getDescriptor().isAggregateDescriptor()) { 885 if (query.getObject() != null) { 886 if (query.getBackupClone() == null) { 887 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 888 } else { 889 if (!query.getObject().getClass().equals(query.getBackupClone().getClass())) { 890 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToRow(databaseRow); 891 } 892 } 893 } 894 } 895 896 return databaseRow; 897 } 898 899 903 public AbstractRecord buildRowForUpdateWithChangeSet(WriteObjectQuery query) { 904 AbstractRecord databaseRow = createRecord(); 905 906 for (Iterator changeRecords = query.getObjectChangeSet().getChanges().iterator(); 907 changeRecords.hasNext();) { 908 ChangeRecord changeRecord = (ChangeRecord)changeRecords.next(); 909 DatabaseMapping mapping = changeRecord.getMapping(); 910 mapping.writeFromObjectIntoRowWithChangeRecord(changeRecord, databaseRow, query.getSession()); 911 } 912 913 return databaseRow; 914 } 915 916 919 public AbstractRecord buildRowForWhereClause(ObjectLevelModifyQuery query) { 920 AbstractRecord databaseRow = createRecord(); 921 922 for (Iterator mappings = getDescriptor().getMappings().iterator(); 923 mappings.hasNext();) { 924 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 925 mapping.writeFromObjectIntoRowForWhereClause(query, databaseRow); 926 } 927 928 if (!getDescriptor().isAggregateDescriptor()) { 931 addPrimaryKeyForNonDefaultTable(databaseRow); 932 } 933 934 return databaseRow; 935 } 936 937 940 public AbstractRecord buildRowFromPrimaryKeyValues(Vector key, AbstractSession session) { 941 AbstractRecord databaseRow = createRecord(key.size()); 942 int keySize = key.size(); 943 for (int index = 0; index < keySize; index++) { 944 DatabaseField field = (DatabaseField)getDescriptor().getPrimaryKeyFields().get(index); 945 Object value = key.elementAt(index); 946 value = session.getPlatform(getDescriptor().getJavaClass()).getConversionManager().convertObject(value, field.getType()); 947 databaseRow.put(field, value); 948 } 949 950 return databaseRow; 951 } 952 953 956 public AbstractRecord buildTemplateInsertRow(AbstractSession session) { 957 AbstractRecord databaseRow = createRecord(); 958 buildTemplateInsertRow(session, databaseRow); 959 return databaseRow; 960 } 961 962 public void buildTemplateInsertRow(AbstractSession session, AbstractRecord databaseRow) { 963 for (Iterator mappings = getDescriptor().getMappings().iterator(); 964 mappings.hasNext();) { 965 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 966 mapping.writeInsertFieldsIntoRow(databaseRow, session); 967 } 968 969 if (getDescriptor().hasInheritance()) { 971 getDescriptor().getInheritancePolicy().addClassIndicatorFieldToInsertRow(databaseRow); 972 } 973 974 if (!getDescriptor().isAggregateDescriptor()) { 977 addPrimaryKeyForNonDefaultTable(databaseRow); 978 } 979 980 if (getDescriptor().usesOptimisticLocking()) { 981 getDescriptor().getOptimisticLockingPolicy().addLockFieldsToUpdateRow(databaseRow, session); 982 } 983 984 if (getDescriptor().usesSequenceNumbers() && session.getSequencing().shouldAcquireValueAfterInsert(getDescriptor().getJavaClass())) { 986 databaseRow.remove(getDescriptor().getSequenceNumberField()); 987 } 988 } 989 990 994 public AbstractRecord buildTemplateUpdateRow(AbstractSession session) { 995 AbstractRecord databaseRow = createRecord(); 996 997 for (Iterator mappings = getNonPrimaryKeyMappings().iterator(); 998 mappings.hasNext();) { 999 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 1000 mapping.writeUpdateFieldsIntoRow(databaseRow, session); 1001 } 1002 1003 if (getDescriptor().usesOptimisticLocking()) { 1004 getDescriptor().getOptimisticLockingPolicy().addLockFieldsToUpdateRow(databaseRow, session); 1005 } 1006 1007 return databaseRow; 1008 } 1009 1010 1014 public Expression buildUpdateExpression(DatabaseTable table, AbstractRecord transactionRow, AbstractRecord modifyRow) { 1015 Expression primaryKeyExpression = buildPrimaryKeyExpression(table); 1017 if (getDescriptor().usesOptimisticLocking()) { 1018 return getDescriptor().getOptimisticLockingPolicy().buildUpdateExpression(table, primaryKeyExpression, transactionRow, modifyRow); 1019 } else { 1020 return primaryKeyExpression; 1021 } 1022 } 1023 1024 1028 public void buildPrimaryKeyAttributesIntoObject(Object original, AbstractRecord databaseRow, ObjectBuildingQuery query) throws DatabaseException, QueryException { 1029 AbstractSession executionSession = query.getSession().getExecutionSession(query); 1030 1031 Vector mappings = getPrimaryKeyMappings(); 1033 int mappingsSize = mappings.size(); 1034 for (int i = 0; i < mappingsSize; i++) { 1035 DatabaseMapping mapping = (DatabaseMapping)mappings.get(i); 1036 mapping.buildShallowOriginalFromRow(databaseRow, original, query, executionSession); 1037 } 1038 } 1039 1040 1049 public void buildAttributesIntoShallowObject(Object original, AbstractRecord databaseRow, ObjectBuildingQuery query) throws DatabaseException, QueryException { 1050 AbstractSession executionSession = query.getSession().getExecutionSession(query); 1051 1052 Vector pkMappings = getPrimaryKeyMappings(); 1054 int mappingsSize = pkMappings.size(); 1055 for (int i = 0; i < mappingsSize; i++) { 1056 DatabaseMapping mapping = (DatabaseMapping)pkMappings.get(i); 1057 1058 if (!mapping.isDirectToFieldMapping()) { 1060 mapping.buildShallowOriginalFromRow(databaseRow, original, query, executionSession); 1061 } 1062 } 1063 Vector mappings = getDescriptor().getMappings(); 1064 mappingsSize = mappings.size(); 1065 for (int i = 0; i < mappingsSize; i++) { 1066 DatabaseMapping mapping = (DatabaseMapping)mappings.get(i); 1067 1068 if (mapping.isDirectToFieldMapping()) { 1070 mapping.buildShallowOriginalFromRow(databaseRow, original, query, executionSession); 1071 } 1072 } 1073 } 1074 1075 1080 public void buildAttributesIntoWorkingCopyClone(Object clone, ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, boolean forRefresh) throws DatabaseException, QueryException { 1081 AbstractSession executionSession = unitOfWork; 1082 Vector mappings = getDescriptor().getMappings(); 1085 int mappingsSize = mappings.size(); 1086 for (int i = 0; i < mappingsSize; i++) { 1087 DatabaseMapping mapping = (DatabaseMapping)mappings.get(i); 1088 if (query.shouldReadMapping(mapping)) { 1089 mapping.buildCloneFromRow(databaseRow, joinManager, clone, query, unitOfWork, executionSession); 1090 } 1091 } 1092 1093 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1095 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(clone); 1098 event.setQuery(query); 1099 event.setSession(query.getSession()); 1100 event.setRecord(databaseRow); 1101 if (forRefresh) { 1102 event.setEventCode(DescriptorEventManager.PostRefreshEvent); 1103 } else { 1104 event.setEventCode(DescriptorEventManager.PostBuildEvent); 1105 } 1106 getDescriptor().getEventManager().executeEvent(event); 1107 } 1108 } 1109 1110 1118 protected Object buildWorkingCopyCloneFromRow(ObjectBuildingQuery query, JoinedAttributeManager joinManager, AbstractRecord databaseRow, UnitOfWorkImpl unitOfWork, Vector primaryKey) throws DatabaseException, QueryException { 1119 Object workingClone = unitOfWork.getIdentityMapAccessorInstance().getIdentityMapManager().getFromIdentityMap(primaryKey, getDescriptor().getJavaClass(), getDescriptor()); 1123 1124 boolean wasAClone = workingClone != null; 1126 boolean isARefresh = query.shouldRefreshIdentityMapResult() || (query.isLockQuery() && (!wasAClone || !query.isClonePessimisticLocked(workingClone, unitOfWork))); 1127 if (wasAClone && (!isARefresh)) { 1128 return workingClone; 1129 } 1130 1131 boolean wasAnOriginal = false; 1132 Object original = null; 1133 1134 if (!isARefresh && !unitOfWork.shouldReadFromDB()) { 1136 CacheKey originalCacheKey = unitOfWork.getParentIdentityMapSession(query).getIdentityMapAccessorInstance().getCacheKeyForObject(primaryKey, getDescriptor().getJavaClass(), getDescriptor()); 1137 if (originalCacheKey != null) { 1138 try { 1140 originalCacheKey.acquireReadLock(); 1141 original = originalCacheKey.getObject(); 1142 } finally { 1143 originalCacheKey.releaseReadLock(); 1144 } 1145 wasAnOriginal = original != null; 1146 isARefresh = wasAnOriginal && (getDescriptor().shouldAlwaysRefreshCache() || getDescriptor().getCacheInvalidationPolicy().isInvalidated(originalCacheKey, query.getExecutionTime())); 1148 if (wasAnOriginal && (!isARefresh)) { 1150 return unitOfWork.cloneAndRegisterObject(original, originalCacheKey, joinManager); 1151 } 1152 } 1153 } 1154 1155 CacheKey unitOfWorkCacheKey = null; 1156 if (!wasAClone) { 1157 if (wasAnOriginal) { 1162 workingClone = instantiateWorkingCopyClone(original, unitOfWork); 1163 unitOfWork.getCloneToOriginals().put(workingClone, original); 1165 } else { 1166 workingClone = instantiateWorkingCopyCloneFromRow(databaseRow, query); 1168 } 1169 1170 unitOfWorkCacheKey = unitOfWork.getIdentityMapAccessorInstance().internalPutInIdentityMap(workingClone, primaryKey, null, 0, getDescriptor()); 1173 1174 unitOfWork.getCloneMapping().put(workingClone, workingClone); 1176 } 1177 1178 if (unitOfWorkCacheKey == null) { 1182 unitOfWorkCacheKey = unitOfWork.getIdentityMapAccessorInstance().getCacheKeyForObject(primaryKey, getDescriptor().getJavaClass(), getDescriptor()); 1183 } 1184 1185 if (wasAClone && (unitOfWorkCacheKey.getLastUpdatedQueryId() >= query.getQueryId())) { 1187 return workingClone; 1188 } 1189 copyQueryInfoToCacheKey(unitOfWorkCacheKey, query, databaseRow, unitOfWork, getDescriptor()); 1190 1191 if (!wasAClone) { 1193 descriptor.getObjectChangePolicy().setChangeListener(workingClone, unitOfWork, getDescriptor()); 1195 } 1196 1197 descriptor.getObjectChangePolicy().dissableEventProcessing(workingClone); 1199 buildAttributesIntoWorkingCopyClone(workingClone, query, joinManager, databaseRow, unitOfWork, wasAClone); 1201 Object backupClone = getDescriptor().getObjectChangePolicy().buildBackupClone(workingClone, this, unitOfWork); 1202 1203 if (wasAClone) { 1205 descriptor.getObjectChangePolicy().clearChanges(workingClone, unitOfWork, getDescriptor()); 1206 } 1207 descriptor.getObjectChangePolicy().enableEventProcessing(workingClone); 1208 unitOfWork.getCloneMapping().put(workingClone, backupClone); 1209 query.recordCloneForPessimisticLocking(workingClone, unitOfWork); 1210 1211 return workingClone; 1212 } 1213 1214 1217 public Object clone() { 1218 Object object = null; 1219 1220 try { 1221 object = super.clone(); 1222 } catch (Exception exception) { 1223 ; 1224 } 1225 1226 ((ObjectBuilder)object).setMappingsByAttribute(new HashMap(getMappingsByAttribute())); 1228 ((ObjectBuilder)object).setMappingsByField(new HashMap(getMappingsByField())); 1229 ((ObjectBuilder)object).setReadOnlyMappingsByField(new HashMap(getReadOnlyMappingsByField())); 1230 ((ObjectBuilder)object).setPrimaryKeyMappings((Vector)getPrimaryKeyMappings().clone()); 1231 ((ObjectBuilder)object).setNonPrimaryKeyMappings((Vector)getNonPrimaryKeyMappings().clone()); 1232 1233 return object; 1234 } 1235 1236 1241 public void cascadePerformRemove(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects) { 1242 Iterator mappings = getDescriptor().getMappings().iterator(); 1243 while (mappings.hasNext()) { 1244 ((DatabaseMapping)mappings.next()).cascadePerformRemoveIfRequired(object, uow, visitedObjects); 1245 } 1246 } 1247 1248 1253 public void cascadeRegisterNewForCreate(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects) { 1254 Iterator mappings = getDescriptor().getMappings().iterator(); 1255 while (mappings.hasNext()) { 1256 ((DatabaseMapping)mappings.next()).cascadeRegisterNewIfRequired(object, uow, visitedObjects); 1257 } 1258 } 1259 1260 1265 public ObjectChangeSet compareForChange(Object clone, Object backUp, UnitOfWorkChangeSet changeSet, AbstractSession session) { 1266 return descriptor.getObjectChangePolicy().calculateChanges(clone, backUp, changeSet, session, getDescriptor(), true); 1268 } 1269 1270 1273 public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) { 1274 Vector mappings = getDescriptor().getMappings(); 1276 for (int index = 0; index < mappings.size(); index++) { 1277 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 1278 1279 if (!mapping.compareObjects(firstObject, secondObject, session)) { 1280 Object firstValue = mapping.getAttributeValueFromObject(firstObject); 1281 Object secondValue = mapping.getAttributeValueFromObject(secondObject); 1282 session.log(SessionLog.FINEST, SessionLog.QUERY, "compare_failed", mapping, firstValue, secondValue); 1283 return false; 1284 } 1285 } 1286 1287 return true; 1288 } 1289 1290 1293 public void copyInto(Object source, Object target) { 1294 Vector mappings = getDescriptor().getMappings(); 1296 for (int index = 0; index < mappings.size(); index++) { 1297 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 1298 mapping.setAttributeValueInObject(target, mapping.getAttributeValueFromObject(source)); 1299 } 1300 } 1301 1302 1307 public Object copyObject(Object original, ObjectCopyingPolicy policy) { 1308 Object copy = policy.getCopies().get(original); 1309 if (copy != null) { 1310 return copy; 1311 } 1312 1313 copy = instantiateClone(original, policy.getSession()); 1314 policy.getCopies().put(original, copy); 1315 1316 List mappings = getCloningMappings(); 1318 for (int index = 0; index < mappings.size(); index++) { 1319 ((DatabaseMapping)mappings.get(index)).buildCopy(copy, original, policy); 1320 } 1321 1322 if (policy.shouldResetPrimaryKey() && (!(getDescriptor().isAggregateDescriptor() || getDescriptor().isAggregateCollectionDescriptor()))) { 1323 boolean hasOneToOne = false; 1325 for (Enumeration keyMappingsEnum = getPrimaryKeyMappings().elements(); 1326 keyMappingsEnum.hasMoreElements();) { 1327 if (((DatabaseMapping)keyMappingsEnum.nextElement()).isOneToOneMapping()) { 1328 hasOneToOne = true; 1329 } 1330 } 1331 if (!hasOneToOne) { 1332 for (Enumeration keyMappingsEnum = getPrimaryKeyMappings().elements(); 1333 keyMappingsEnum.hasMoreElements();) { 1334 DatabaseMapping mapping = (DatabaseMapping)keyMappingsEnum.nextElement(); 1335 1336 if (mapping.isDirectToFieldMapping()) { 1338 Object nullValue = ((AbstractDirectMapping)mapping).getAttributeValue(null, policy.getSession()); 1339 mapping.setAttributeValueInObject(copy, nullValue); 1340 } else if (mapping.isTransformationMapping()) { 1341 mapping.setAttributeValueInObject(copy, null); 1342 } 1343 } 1344 } 1345 } 1346 1347 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1349 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(copy); 1350 event.setSession(policy.getSession()); 1351 event.setOriginalObject(original); 1352 event.setEventCode(DescriptorEventManager.PostCloneEvent); 1353 getDescriptor().getEventManager().executeEvent(event); 1354 } 1355 1356 return copy; 1357 } 1358 1359 1366 public ObjectChangeSet createObjectChangeSet(Object clone, UnitOfWorkChangeSet uowChangeSet, AbstractSession session) { 1367 boolean isNew = ((UnitOfWorkImpl)session).isObjectNew(clone); 1368 return createObjectChangeSet(clone, uowChangeSet, isNew, session); 1369 } 1370 1371 1379 public ObjectChangeSet createObjectChangeSet(Object clone, UnitOfWorkChangeSet uowChangeSet, boolean isNew, AbstractSession session) { 1380 ObjectChangeSet changes = (ObjectChangeSet)uowChangeSet.getObjectChangeSetForClone(clone); 1381 if (changes == null) { 1382 if (getDescriptor().isAggregateDescriptor()) { 1383 changes = new AggregateObjectChangeSet(new Vector(0), clone, uowChangeSet, isNew); 1384 } else { 1385 changes = new ObjectChangeSet(extractPrimaryKeyFromObject(clone, session), clone, uowChangeSet, isNew); 1386 } 1387 changes.setIsAggregate(getDescriptor().isAggregateDescriptor() || getDescriptor().isAggregateCollectionDescriptor()); 1388 uowChangeSet.addObjectChangeSetForIdentity(changes, clone); 1389 } 1390 return changes; 1391 } 1392 1393 1396 public void createPrimaryKeyExpression(AbstractSession session) { 1397 Expression expression = null; 1398 Expression builder = new ExpressionBuilder(); 1399 Expression subExp1; 1400 Expression subExp2; 1401 Expression subExpression; 1402 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1403 1404 for (int index = 0; index < primaryKeyFields.size(); index++) { 1405 DatabaseField primaryKeyField = (DatabaseField)primaryKeyFields.get(index); 1406 subExp1 = builder.getField(primaryKeyField); 1407 subExp2 = builder.getParameter(primaryKeyField); 1408 subExpression = subExp1.equal(subExp2); 1409 1410 if (expression == null) { 1411 expression = subExpression; 1412 } else { 1413 expression = expression.and(subExpression); 1414 } 1415 } 1416 1417 setPrimaryKeyExpression(expression); 1418 } 1419 1420 1423 public Vector extractPrimaryKeyFromExpression(boolean requiresExactMatch, Expression expression, AbstractRecord translationRow, AbstractSession session) { 1424 AbstractRecord primaryKeyRow = createRecord(getPrimaryKeyMappings().size()); 1425 1426 AbstractSession oldSession = expression.getBuilder().getSession(); 1429 expression.getBuilder().setSession(session); 1430 boolean isValid = expression.extractPrimaryKeyValues(requiresExactMatch, getDescriptor(), primaryKeyRow, translationRow); 1432 expression.getBuilder().setSession(oldSession); 1433 if (requiresExactMatch && (!isValid)) { 1434 return null; 1435 } 1436 1437 if (primaryKeyRow.size() != getDescriptor().getPrimaryKeyFields().size()) { 1439 return null; 1440 } 1441 1442 return extractPrimaryKeyFromRow(primaryKeyRow, session); 1443 } 1444 1445 1448 public Vector extractPrimaryKeyFromObject(Object domainObject, AbstractSession session) { 1449 if (getDescriptor().hasInheritance() && (domainObject.getClass() != getDescriptor().getJavaClass()) && (!domainObject.getClass().getSuperclass().equals(getDescriptor().getJavaClass()))) { 1451 return session.getDescriptor(domainObject).getObjectBuilder().extractPrimaryKeyFromObject(domainObject, session); 1452 } else { 1453 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1454 Vector primaryKeyValues = new Vector(primaryKeyFields.size()); 1455 1456 if (getDescriptor().hasSimplePrimaryKey()) { 1458 for (int index = 0; index < getPrimaryKeyMappings().size(); index++) { 1460 AbstractDirectMapping mapping = (AbstractDirectMapping)getPrimaryKeyMappings().get(index); 1461 Object keyValue = mapping.valueFromObject(domainObject, (DatabaseField)primaryKeyFields.get(index), session); 1462 primaryKeyValues.add(keyValue); 1463 } 1464 } else { 1465 AbstractRecord databaseRow = createRecord(getPrimaryKeyMappings().size()); 1466 1467 for (int index = 0; index < getPrimaryKeyMappings().size(); index++) { 1469 DatabaseMapping mapping = (DatabaseMapping)getPrimaryKeyMappings().get(index); 1470 1471 if (mapping != null) { 1473 mapping.writeFromObjectIntoRow(domainObject, databaseRow, session); 1474 } 1475 } 1476 1477 for (int index = 0; index < primaryKeyFields.size(); index++) { 1479 Class classification = (Class )getPrimaryKeyClassifications().get(index); 1482 Object value = databaseRow.get((DatabaseField)primaryKeyFields.get(index)); 1483 1484 primaryKeyValues.addElement(session.getPlatform(domainObject.getClass()).convertObject(value, classification)); 1486 } 1487 } 1488 1489 return primaryKeyValues; 1490 } 1491 } 1492 1493 1497 public Vector extractPrimaryKeyFromRow(AbstractRecord databaseRow, AbstractSession session) { 1498 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1499 Vector primaryKeyValues = new Vector(primaryKeyFields.size()); 1500 1501 for (int index = 0; index < primaryKeyFields.size(); index++) { 1503 DatabaseField field = (DatabaseField)primaryKeyFields.get(index); 1504 1505 Class classification = (Class )getPrimaryKeyClassifications().get(index); 1507 Object value = databaseRow.get(field); 1508 if (value != null) { 1509 primaryKeyValues.addElement(session.getPlatform(getDescriptor().getJavaClass()).convertObject(value, classification)); 1510 } else { 1511 return null; 1512 } 1513 } 1514 1515 return primaryKeyValues; 1516 } 1517 1518 1521 public AbstractRecord extractPrimaryKeyRowFromExpression(Expression expression, AbstractRecord translationRow, AbstractSession session) { 1522 AbstractRecord primaryKeyRow = createRecord(getPrimaryKeyMappings().size()); 1523 1524 AbstractSession oldSession = expression.getBuilder().getSession(); 1527 expression.getBuilder().setSession(session); 1528 boolean isValid = expression.extractPrimaryKeyValues(true, getDescriptor(), primaryKeyRow, translationRow); 1530 expression.getBuilder().setSession(session); 1531 if (!isValid) { 1532 return null; 1533 } 1534 1535 if (primaryKeyRow.size() != getDescriptor().getPrimaryKeyFields().size()) { 1537 return null; 1538 } 1539 1540 return primaryKeyRow; 1541 } 1542 1543 1546 public AbstractRecord extractPrimaryKeyRowFromObject(Object domainObject, AbstractSession session) { 1547 AbstractRecord databaseRow = createRecord(getPrimaryKeyMappings().size()); 1548 1549 for (int index = 0; index < getPrimaryKeyMappings().size(); index++) { 1551 ((DatabaseMapping)getPrimaryKeyMappings().get(index)).writeFromObjectIntoRow(domainObject, databaseRow, session); 1552 } 1553 1554 if (getDescriptor().hasSimplePrimaryKey()) { 1556 return databaseRow; 1557 } 1558 AbstractRecord primaryKeyRow = createRecord(getPrimaryKeyMappings().size()); 1559 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1560 for (int index = 0; index < primaryKeyFields.size(); index++) { 1561 Class classification = (Class )getPrimaryKeyClassifications().get(index); 1564 DatabaseField field = (DatabaseField)primaryKeyFields.get(index); 1565 Object value = databaseRow.get(field); 1566 primaryKeyRow.put(field, session.getPlatform(domainObject.getClass()).convertObject(value, classification)); 1567 } 1568 1569 return primaryKeyRow; 1570 } 1571 1572 1575 public Object extractValueFromObjectForField(Object domainObject, DatabaseField field, AbstractSession session) throws DescriptorException { 1576 ClassDescriptor descriptor = null; 1579 if (getDescriptor().hasInheritance() && (domainObject.getClass() != getDescriptor().getJavaClass()) && ((descriptor = session.getDescriptor(domainObject)).getJavaClass() != getDescriptor().getJavaClass())) { 1580 return descriptor.getObjectBuilder().extractValueFromObjectForField(domainObject, field, session); 1581 } else { 1582 DatabaseMapping mapping = getMappingForField(field); 1583 if (mapping == null) { 1584 throw DescriptorException.missingMappingForField(field, getDescriptor()); 1585 } 1586 1587 return mapping.valueFromObject(domainObject, field, session); 1588 } 1589 } 1590 1591 1594 public DatabaseMapping getBaseMappingForField(DatabaseField databaseField) { 1595 DatabaseMapping mapping = getMappingForField(databaseField); 1596 1597 while (mapping.isAggregateObjectMapping()) { 1599 mapping = ((AggregateObjectMapping)mapping).getReferenceDescriptor().getObjectBuilder().getMappingForField(databaseField); 1600 } 1601 return mapping; 1602 } 1603 1604 1607 public Object getBaseValueForField(DatabaseField databaseField, Object domainObject) { 1608 Object valueIntoObject = domainObject; 1609 DatabaseMapping mapping = getMappingForField(databaseField); 1610 1611 while (mapping.isAggregateObjectMapping()) { 1613 valueIntoObject = mapping.getAttributeValueFromObject(valueIntoObject); 1614 mapping = ((AggregateMapping)mapping).getReferenceDescriptor().getObjectBuilder().getMappingForField(databaseField); 1615 } 1616 return mapping.getAttributeValueFromObject(valueIntoObject); 1617 } 1618 1619 1622 public ClassDescriptor getDescriptor() { 1623 return descriptor; 1624 } 1625 1626 1631 public Class getFieldClassification(DatabaseField fieldToClassify) throws DescriptorException { 1632 DatabaseMapping mapping = getMappingForField(fieldToClassify); 1633 if (mapping == null) { 1634 return null; 1638 } 1639 1640 return mapping.getFieldClassification(fieldToClassify); 1641 } 1642 1643 1646 public DatabaseField getFieldForQueryKeyName(String name) { 1647 QueryKey key = getDescriptor().getQueryKeyNamed(name); 1648 if (key == null) { 1649 DatabaseMapping mapping = getMappingForAttributeName(name); 1650 if (mapping == null) { 1651 return null; 1652 } 1653 if (mapping.getFields().isEmpty()) { 1654 return null; 1655 } 1656 return (DatabaseField)mapping.getFields().firstElement(); 1657 } 1658 if (key.isDirectQueryKey()) { 1659 return ((DirectQueryKey)key).getField(); 1660 } 1661 return null; 1662 } 1663 1664 1669 public List getCloningMappings() { 1670 return cloningMappings; 1671 } 1672 1673 1677 public Vector getJoinedAttributes() { 1678 return joinedAttributes; 1679 } 1680 1681 1686 public boolean hasJoinedAttributes() { 1687 return (joinedAttributes != null); 1688 } 1689 1690 1693 public DatabaseMapping getMappingForAttributeName(String name) { 1694 return (DatabaseMapping)getMappingsByAttribute().get(name); 1695 } 1696 1697 1700 public DatabaseMapping getMappingForField(DatabaseField field) { 1701 return (DatabaseMapping)getMappingsByField().get(field); 1702 } 1703 1704 1707 public Vector getReadOnlyMappingsForField(DatabaseField field) { 1708 return (Vector)getReadOnlyMappingsByField().get(field); 1709 } 1710 1711 1714 protected Map getMappingsByAttribute() { 1715 return mappingsByAttribute; 1716 } 1717 1718 1722 public Map getMappingsByField() { 1723 return mappingsByField; 1724 } 1725 1726 1730 public Map getReadOnlyMappingsByField() { 1731 return readOnlyMappingsByField; 1732 } 1733 1734 1737 protected Vector getNonPrimaryKeyMappings() { 1738 return nonPrimaryKeyMappings; 1739 } 1740 1741 1744 public Object getParentObjectForField(DatabaseField databaseField, Object domainObject) { 1745 Object valueIntoObject = domainObject; 1746 DatabaseMapping mapping = getMappingForField(databaseField); 1747 1748 while (mapping.isAggregateObjectMapping()) { 1750 valueIntoObject = mapping.getAttributeValueFromObject(valueIntoObject); 1751 mapping = ((AggregateMapping)mapping).getReferenceDescriptor().getObjectBuilder().getMappingForField(databaseField); 1752 } 1753 return valueIntoObject; 1754 } 1755 1756 1760 public Vector getPrimaryKeyClassifications() { 1761 if (primaryKeyClassifications == null) { 1762 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1763 Vector classifications = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(primaryKeyFields.size()); 1764 1765 for (int index = 0; index < primaryKeyFields.size(); index++) { 1766 DatabaseMapping mapping = (DatabaseMapping)getPrimaryKeyMappings().get(index); 1767 DatabaseField field = (DatabaseField)primaryKeyFields.get(index); 1768 if (mapping != null) { 1769 classifications.add(Helper.getObjectClass(mapping.getFieldClassification(field))); 1770 } else { 1771 classifications.add(null); 1772 } 1773 primaryKeyClassifications = classifications; 1774 } 1775 } 1776 return primaryKeyClassifications; 1777 } 1778 1779 1782 public Expression getPrimaryKeyExpression() { 1783 return primaryKeyExpression; 1784 } 1785 1786 1789 public Vector getPrimaryKeyMappings() { 1790 return primaryKeyMappings; 1791 } 1792 1793 1796 public DatabaseField getTargetFieldForQueryKeyName(String queryKeyName) { 1797 DatabaseMapping mapping = (DatabaseMapping)getMappingForAttributeName(queryKeyName); 1798 if ((mapping != null) && mapping.isDirectToFieldMapping()) { 1799 return ((AbstractDirectMapping)mapping).getField(); 1800 } 1801 1802 QueryKey queryKey = getDescriptor().getQueryKeyNamed(queryKeyName); 1805 if ((queryKey != null) && queryKey.isDirectQueryKey()) { 1806 return ((DirectQueryKey)queryKey).getField(); 1807 } 1808 1809 return null; 1811 } 1812 1813 1816 public void initialize(AbstractSession session) throws DescriptorException { 1817 this.getMappingsByField().clear(); 1818 this.getReadOnlyMappingsByField().clear(); 1819 this.getMappingsByAttribute().clear(); 1820 this.getCloningMappings().clear(); 1821 1822 for (Enumeration mappings = getDescriptor().getMappings().elements(); 1823 mappings.hasMoreElements();) { 1824 DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement(); 1825 1826 if (!mapping.isWriteOnly()) { 1828 getMappingsByAttribute().put(mapping.getAttributeName(), mapping); 1829 } 1830 if (mapping.isCloningRequired()) { 1831 getCloningMappings().add(mapping); 1832 } 1833 1834 for (Enumeration fields = mapping.getFields().elements(); fields.hasMoreElements();) { 1836 Object field = fields.nextElement(); 1837 if (!mapping.isReadOnly()) { 1838 if (getMappingsByField().containsKey(field)) { 1839 if (!mapping.isAggregateObjectMapping()) { 1841 session.getIntegrityChecker().handleError(DescriptorException.multipleWriteMappingsForField(field.toString(), mapping)); 1842 } 1843 } else { 1844 getMappingsByField().put(field, mapping); 1845 } 1846 } else { 1847 Vector mappingVector = (Vector)getReadOnlyMappingsByField().get(field); 1848 if (mappingVector == null) { 1849 mappingVector = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 1850 getReadOnlyMappingsByField().put(field, mappingVector); 1851 } 1852 mappingVector.add(mapping); 1853 } 1854 } 1855 } 1856 1857 initializePrimaryKey(session); 1858 1859 initializeJoinedAttributes(); 1860 } 1861 1862 1869 public void initializeJoinedAttributes() { 1870 Vector joinedAttributes = null; 1873 Vector mappings = getDescriptor().getMappings(); 1874 for (int i = 0; i < mappings.size(); i++) { 1875 DatabaseMapping mapping = (DatabaseMapping)mappings.get(i); 1876 if (mapping.isOneToOneMapping() && (mapping.isRelationalMapping()) && ((OneToOneMapping)mapping).shouldUseJoining()) { 1877 if (joinedAttributes == null) { 1878 joinedAttributes = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 1879 } 1880 joinedAttributes.add(mapping.getAttributeName()); 1881 } 1882 } 1883 this.joinedAttributes = joinedAttributes; 1884 } 1885 1886 1890 protected void copyQueryInfoToCacheKey(CacheKey cacheKey, ObjectBuildingQuery query, AbstractRecord databaseRow, AbstractSession session, ClassDescriptor concreteDescriptor) { 1891 cacheKey.setLastUpdatedQueryId(query.getQueryId()); 1893 1894 if (concreteDescriptor.usesOptimisticLocking()) { 1895 OptimisticLockingPolicy policy = concreteDescriptor.getOptimisticLockingPolicy(); 1896 Object cacheValue = policy.getValueToPutInCache(databaseRow, session); 1897 1898 cacheKey.setWriteLockValue(cacheValue); 1900 } 1901 cacheKey.setReadTime(query.getExecutionTime()); 1902 } 1903 1904 1907 public void initializePrimaryKey(AbstractSession session) throws DescriptorException { 1908 createPrimaryKeyExpression(session); 1909 1910 List primaryKeyfields = getDescriptor().getPrimaryKeyFields(); 1911 1912 this.getPrimaryKeyMappings().clear(); 1913 this.getNonPrimaryKeyMappings().clear(); 1914 1915 for (Iterator fields = getMappingsByField().keySet().iterator(); fields.hasNext();) { 1917 DatabaseField field = (DatabaseField)fields.next(); 1918 if (!primaryKeyfields.contains(field)) { 1919 DatabaseMapping mapping = getMappingForField(field); 1920 if (!getNonPrimaryKeyMappings().contains(mapping)) { 1921 getNonPrimaryKeyMappings().addElement(mapping); 1922 } 1923 } 1924 } 1925 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 1926 for (int index = 0; index < primaryKeyFields.size(); index++) { 1927 DatabaseField primaryKeyField = (DatabaseField)primaryKeyFields.get(index); 1928 DatabaseMapping mapping = getMappingForField(primaryKeyField); 1929 1930 if ((mapping == null) && (!getDescriptor().isAggregateDescriptor()) && (!getDescriptor().isAggregateCollectionDescriptor())) { 1931 throw DescriptorException.noMappingForPrimaryKey(primaryKeyField, getDescriptor()); 1932 } 1933 1934 getPrimaryKeyMappings().addElement(mapping); 1935 if (mapping != null) { 1936 mapping.setIsPrimaryKeyMapping(true); 1937 } 1938 1939 if (getDescriptor().hasMultipleTables()) { 1942 for (Iterator secondaryKeysEnum = getDescriptor().getAdditionalTablePrimaryKeyFields().values().iterator(); 1943 secondaryKeysEnum.hasNext();) { 1944 Map keyMaping = (Map)secondaryKeysEnum.next(); 1945 DatabaseField secondaryField = (DatabaseField)keyMaping.get(primaryKeyField); 1946 1947 if ((secondaryField != null) && (mapping != null)) { 1949 getMappingsByField().put(secondaryField, mapping); 1950 } 1951 } 1952 } 1953 } 1954 1955 boolean hasSimplePrimaryKey = true; 1958 for (int index = 0; index < getPrimaryKeyMappings().size(); index++) { 1959 DatabaseMapping mapping = (DatabaseMapping)getPrimaryKeyMappings().get(index); 1960 1961 if ((mapping == null) || (!mapping.isDirectToFieldMapping())) { 1963 hasSimplePrimaryKey = false; 1964 break; 1965 } 1966 } 1967 getDescriptor().setHasSimplePrimaryKey(hasSimplePrimaryKey); 1968 } 1969 1970 1975 public Object instantiateClone(Object domainObject, AbstractSession session) { 1976 return getDescriptor().getCopyPolicy().buildClone(domainObject, session); 1977 } 1978 1979 1987 public Object instantiateWorkingCopyClone(Object domainObject, AbstractSession session) { 1988 return getDescriptor().getCopyPolicy().buildWorkingCopyClone(domainObject, session); 1989 } 1990 1991 1999 public Object instantiateWorkingCopyCloneFromRow(AbstractRecord row, ObjectBuildingQuery query) { 2000 if (query.isObjectLevelReadQuery()){ return getDescriptor().getCopyPolicy().buildWorkingCopyCloneFromRow(row, ((ObjectLevelReadQuery)query)); 2002 }else{ 2003 return getDescriptor().getCopyPolicy().buildWorkingCopyCloneFromRow(row, query); 2004 } 2005 } 2006 2007 public boolean isPrimaryKeyMapping(DatabaseMapping mapping) { 2008 return getPrimaryKeyMappings().contains(mapping); 2009 } 2010 2011 2015 public void iterate(DescriptorIterator iterator) { 2016 Vector mappings = getDescriptor().getMappings(); 2018 int mappingsSize = mappings.size(); 2019 for (int index = 0; index < mappingsSize; index++) { 2020 ((DatabaseMapping)mappings.get(index)).iterate(iterator); 2021 } 2022 } 2023 2024 2028 public void mergeChangesIntoObject(Object target, ObjectChangeSet changeSet, Object source, MergeManager mergeManager) { 2029 for (Enumeration changes = changeSet.getChanges().elements(); changes.hasMoreElements();) { 2030 ChangeRecord record = (ChangeRecord)changes.nextElement(); 2031 2032 DatabaseMapping mapping = getMappingForAttributeName(record.getAttribute()); 2035 mapping.mergeChangesIntoObject(target, record, source, mergeManager); 2036 } 2037 2038 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 2040 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(target); 2041 event.setSession(mergeManager.getSession()); 2042 event.setOriginalObject(source); 2043 event.setChangeSet(changeSet); 2044 event.setEventCode(DescriptorEventManager.PostMergeEvent); 2045 getDescriptor().getEventManager().executeEvent(event); 2046 } 2047 } 2048 2049 2054 public void mergeIntoObject(Object target, boolean isUnInitialized, Object source, MergeManager mergeManager) { 2055 Vector mappings = getDescriptor().getMappings(); 2057 for (int index = 0; index < mappings.size(); index++) { 2058 ((DatabaseMapping)mappings.get(index)).mergeIntoObject(target, isUnInitialized, source, mergeManager); 2059 } 2060 2061 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 2063 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(target); 2064 event.setSession(mergeManager.getSession()); 2065 event.setOriginalObject(source); 2066 event.setEventCode(DescriptorEventManager.PostMergeEvent); 2067 getDescriptor().getEventManager().executeEvent(event); 2068 } 2069 } 2070 2071 2075 public void populateAttributesForClone(Object original, Object clone, UnitOfWorkImpl unitOfWork, JoinedAttributeManager joinedAttributeManager) { 2076 List mappings = getCloningMappings(); 2078 for (int index = 0; index < mappings.size(); index++) { 2079 ((DatabaseMapping)mappings.get(index)).buildClone(original, clone, unitOfWork, joinedAttributeManager); 2080 } 2081 2082 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 2084 oracle.toplink.essentials.descriptors.DescriptorEvent event = new oracle.toplink.essentials.descriptors.DescriptorEvent(clone); 2085 event.setSession(unitOfWork); 2086 event.setOriginalObject(original); 2087 event.setEventCode(DescriptorEventManager.PostCloneEvent); 2088 getDescriptor().getEventManager().executeEvent(event); 2089 } 2090 } 2091 2092 2097 public void rehashFieldDependancies(AbstractSession session) { 2098 setMappingsByField(Helper.rehashMap(getMappingsByField())); 2099 setReadOnlyMappingsByField(Helper.rehashMap(getReadOnlyMappingsByField())); 2100 setPrimaryKeyMappings(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2)); 2101 setNonPrimaryKeyMappings(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2)); 2102 initializePrimaryKey(session); 2103 } 2104 2105 2108 public void setDescriptor(ClassDescriptor aDescriptor) { 2109 descriptor = aDescriptor; 2110 } 2111 2112 2115 protected void setMappingsByAttribute(Map theAttributeMappings) { 2116 mappingsByAttribute = theAttributeMappings; 2117 } 2118 2119 2123 public void setMappingsByField(Map theFieldMappings) { 2124 mappingsByField = theFieldMappings; 2125 } 2126 2127 2131 public void setReadOnlyMappingsByField(Map theReadOnlyFieldMappings) { 2132 readOnlyMappingsByField = theReadOnlyFieldMappings; 2133 } 2134 2135 2138 protected void setNonPrimaryKeyMappings(Vector theNonPrimaryKeyMappings) { 2139 nonPrimaryKeyMappings = theNonPrimaryKeyMappings; 2140 } 2141 2142 2146 protected void setPrimaryKeyClassifications(Vector primaryKeyClassifications) { 2147 this.primaryKeyClassifications = primaryKeyClassifications; 2148 } 2149 2150 2153 public void setPrimaryKeyExpression(Expression criteria) { 2154 primaryKeyExpression = criteria; 2155 } 2156 2157 2160 protected void setPrimaryKeyMappings(Vector thePrimaryKeyMappings) { 2161 primaryKeyMappings = thePrimaryKeyMappings; 2162 } 2163 2164 public String toString() { 2165 return Helper.getShortClassName(getClass()) + "(" + getDescriptor().toString() + ")"; 2166 } 2167 2168 2172 public Object unwrapObject(Object proxy, AbstractSession session) { 2173 if (proxy == null) { 2174 return null; 2175 } 2176 2177 if ((getDescriptor().getJavaClass() == proxy.getClass()) || !getDescriptor().hasWrapperPolicy() || !getDescriptor().getWrapperPolicy().isWrapped(proxy)) { 2179 return proxy; 2180 } 2181 2182 if (getDescriptor().hasInheritance() && (getDescriptor().getInheritancePolicy().hasChildren())) { 2184 ClassDescriptor descriptor = session.getDescriptor(proxy); 2185 if (descriptor != getDescriptor()) { 2186 return descriptor.getObjectBuilder().unwrapObject(proxy, session); 2187 } 2188 } 2189 2190 if (getDescriptor().hasWrapperPolicy()) { 2191 return getDescriptor().getWrapperPolicy().unwrapObject(proxy, session); 2192 } else { 2193 return proxy; 2194 } 2195 } 2196 2197 2201 public void validate(AbstractSession session) throws DescriptorException { 2202 if (getDescriptor().usesSequenceNumbers()) { 2203 if (getMappingForField(getDescriptor().getSequenceNumberField()) == null) { 2204 throw DescriptorException.mappingForSequenceNumberField(getDescriptor()); 2205 } 2206 } 2207 } 2208 2209 2218 public boolean verifyDelete(Object object, AbstractSession session) { 2219 AbstractRecord translationRow = buildRowForTranslation(object, session); 2220 2221 if ((getDescriptor().getQueryManager().getReadObjectQuery() != null) && getDescriptor().getQueryManager().getReadObjectQuery().isCallQuery()) { 2223 Object result = session.readObject(object); 2224 if (result != null) { 2225 return false; 2226 } 2227 } else { 2228 for (Enumeration tables = getDescriptor().getTables().elements(); 2229 tables.hasMoreElements();) { 2230 DatabaseTable table = (DatabaseTable)tables.nextElement(); 2231 2232 SQLSelectStatement sqlStatement = new SQLSelectStatement(); 2233 sqlStatement.addTable(table); 2234 if (table == getDescriptor().getTables().firstElement()) { 2235 sqlStatement.setWhereClause((Expression)getPrimaryKeyExpression().clone()); 2236 } else { 2237 sqlStatement.setWhereClause(buildPrimaryKeyExpression(table)); 2238 } 2239 DatabaseField all = new DatabaseField("*"); 2240 all.setTable(table); 2241 sqlStatement.addField(all); 2242 sqlStatement.normalize(session, null); 2243 2244 DataReadQuery dataReadQuery = new DataReadQuery(); 2245 dataReadQuery.setSQLStatement(sqlStatement); 2246 dataReadQuery.setSessionName(getDescriptor().getSessionName()); 2247 2248 Vector queryResults = (Vector)session.executeQuery(dataReadQuery, translationRow); 2250 if (!queryResults.isEmpty()) { 2251 return false; 2252 } 2253 } 2254 } 2255 2256 for (Enumeration mappings = getDescriptor().getMappings().elements(); 2258 mappings.hasMoreElements();) { 2259 DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement(); 2260 2261 if (!mapping.verifyDelete(object, session)) { 2262 return false; 2263 } 2264 } 2265 2266 return true; 2267 } 2268 2269 2273 public Object wrapObject(Object implementation, AbstractSession session) { 2274 if (implementation == null) { 2275 return null; 2276 } 2277 2278 if (!getDescriptor().hasWrapperPolicy() || getDescriptor().getWrapperPolicy().isWrapped(implementation)) { 2280 return implementation; 2281 } 2282 2283 if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().hasChildren() && (implementation.getClass() != getDescriptor().getJavaClass())) { 2285 ClassDescriptor descriptor = session.getDescriptor(implementation); 2286 if(descriptor != getDescriptor()) { 2287 return descriptor.getObjectBuilder().wrapObject(implementation, session); 2288 } 2289 } 2290 if (getDescriptor().hasWrapperPolicy()) { 2291 return getDescriptor().getWrapperPolicy().wrapObject(implementation, session); 2292 } else { 2293 return implementation; 2294 } 2295 } 2296} 2297 2298 | Popular Tags |