1 21 package oracle.toplink.essentials.internal.queryframework; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.internal.descriptors.OptimisticLockingPolicy; 27 import oracle.toplink.essentials.descriptors.VersionLockingPolicy; 28 import oracle.toplink.essentials.descriptors.DescriptorEvent; 29 import oracle.toplink.essentials.descriptors.DescriptorEventManager; 30 import oracle.toplink.essentials.descriptors.DescriptorQueryManager; 31 import oracle.toplink.essentials.internal.helper.*; 32 import oracle.toplink.essentials.internal.databaseaccess.*; 33 import oracle.toplink.essentials.internal.sessions.*; 34 import oracle.toplink.essentials.sessions.DatabaseRecord; 35 import oracle.toplink.essentials.internal.identitymaps.CacheKey; 36 import oracle.toplink.essentials.expressions.*; 37 import oracle.toplink.essentials.queryframework.*; 38 import oracle.toplink.essentials.exceptions.*; 39 import oracle.toplink.essentials.mappings.*; 40 import oracle.toplink.essentials.logging.SessionLog; 41 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 42 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 43 import oracle.toplink.essentials.internal.sessions.AbstractSession; 44 import oracle.toplink.essentials.descriptors.ClassDescriptor; 45 46 61 public abstract class DatabaseQueryMechanism implements Cloneable , Serializable { 62 63 64 protected DatabaseQuery query; 65 66 69 public DatabaseQueryMechanism() { 70 } 71 72 76 public DatabaseQueryMechanism(DatabaseQuery query) { 77 this.query = query; 78 } 79 80 83 protected void addWriteLockFieldForInsert() { 84 if (getDescriptor().usesOptimisticLocking()) { 85 getDescriptor().getOptimisticLockingPolicy().setupWriteFieldsForInsert(getWriteObjectQuery()); 86 } 87 } 88 89 93 public void buildSelectionCriteria(AbstractSession session) { 94 } 96 97 104 public Object checkCacheForObject(AbstractRecord translationRow, AbstractSession session) { 105 if ((translationRow == null) || (translationRow.isEmpty())) { 107 return null; 108 } 109 110 List keyFields = getDescriptor().getPrimaryKeyFields(); 111 Vector primaryKey = new Vector(keyFields.size()); 112 113 for (int index = 0; index < keyFields.size(); index++) { 114 Object value = translationRow.get((DatabaseField)keyFields.get(index)); 115 if (value == null) { 116 return null; 117 } else { 118 primaryKey.add(value); 119 } 120 } 121 return session.getIdentityMapAccessorInstance().getFromIdentityMapWithDeferredLock(primaryKey, getReadObjectQuery().getReferenceClass(), false, getDescriptor()); 122 } 123 124 127 protected Object clone() { 128 try { 129 return super.clone(); 130 } catch (CloneNotSupportedException e) { 131 throw new InternalError (); 132 } 133 } 134 135 138 public DatabaseQueryMechanism clone(DatabaseQuery queryClone) { 139 DatabaseQueryMechanism clone = (DatabaseQueryMechanism)clone(); 140 clone.setQuery(queryClone); 141 return clone; 142 } 143 144 148 public abstract DatabaseCall cursorSelectAllRows() throws DatabaseException; 149 150 155 public boolean isEJBQLCallQueryMechanism() { 156 return false; 157 } 158 159 163 public Object buildObjectsFromRows(Vector rows) throws DatabaseException { 164 Object result = ((ReadAllQuery)getQuery()).getContainerPolicy().containerInstance(rows.size()); 165 return getDescriptor().getObjectBuilder().buildObjectsInto((ReadAllQuery)getQuery(), rows, result); 166 } 167 ; 168 public abstract Integer deleteAll() throws DatabaseException; 169 170 176 public abstract Integer deleteObject() throws DatabaseException; 177 178 184 public abstract Integer executeNoSelect() throws DatabaseException; 185 186 191 public abstract Vector executeSelect() throws DatabaseException; 192 193 200 public Object executeWrite() throws DatabaseException, OptimisticLockException { 201 WriteObjectQuery writeQuery = getWriteObjectQuery(); 202 Object object = writeQuery.getObject(); 203 CommitManager commitManager = getSession().getCommitManager(); 204 205 if (commitManager.isCommitCompleted(object) || commitManager.isCommitInPostModify(object)) { 207 return object; 208 } 209 210 if (commitManager.isCommitInPreModify(object)) { 213 shallowInsertObjectForWrite(object, writeQuery, commitManager); 214 return object; 215 } 216 217 try { 218 getSession().beginTransaction(); 219 220 if (writeQuery.getObjectChangeSet() == null) { 221 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 223 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreWriteEvent, writeQuery)); 226 } 227 } 228 writeQuery.executeCommit(); 229 230 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 232 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostWriteEvent, writeQuery)); 233 } 234 235 getSession().commitTransaction(); 236 237 commitManager.markCommitCompleted(object); 239 240 return object; 241 242 } catch (RuntimeException exception) { 243 getSession().rollbackTransaction(); 244 commitManager.markCommitCompleted(object); 245 throw exception; 246 } 247 } 248 249 256 public Object executeWriteWithChangeSet() throws DatabaseException, OptimisticLockException { 257 WriteObjectQuery writeQuery = getWriteObjectQuery(); 258 ObjectChangeSet objectChangeSet = writeQuery.getObjectChangeSet(); 259 CommitManager commitManager = getSession().getCommitManager(); 260 261 if (!objectChangeSet.hasChanges() && !objectChangeSet.hasForcedChanges()) { 264 commitManager.markCommitCompleted(objectChangeSet); 265 commitManager.markCommitCompleted(writeQuery.getObject()); 266 return writeQuery.getObject(); 267 } 268 if (commitManager.isCommitCompleted(objectChangeSet) 270 || commitManager.isCommitInPostModify(objectChangeSet)) { 271 return writeQuery.getObject(); 272 } 273 274 if (commitManager.isCommitCompleted(writeQuery.getObject()) 277 || commitManager.isCommitInPostModify(writeQuery.getObject())) { 278 return writeQuery.getObject(); 279 } 280 try { 281 getSession().beginTransaction(); 282 283 writeQuery.executeCommitWithChangeSet(); 284 285 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 287 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostWriteEvent, writeQuery)); 288 } 289 290 getSession().commitTransaction(); 291 292 commitManager.markCommitCompleted(objectChangeSet); 294 commitManager.markCommitCompleted(writeQuery.getObject()); 295 296 return writeQuery.getObject(); 297 298 } catch (RuntimeException exception) { 299 getSession().rollbackTransaction(); 300 commitManager.markCommitCompleted(objectChangeSet); 301 commitManager.markCommitCompleted(writeQuery.getObject()); 302 throw exception; 303 } 304 } 305 306 309 protected ClassDescriptor getDescriptor() { 310 return getQuery().getDescriptor(); 311 } 312 313 316 public AbstractRecord getModifyRow() { 317 if (getQuery().isModifyQuery()) { 318 return ((ModifyQuery)getQuery()).getModifyRow(); 319 } else { 320 return null; 321 } 322 } 323 324 327 public DatabaseQuery getQuery() { 328 return query; 329 } 330 331 334 protected ReadObjectQuery getReadObjectQuery() { 335 return (ReadObjectQuery)getQuery(); 336 } 337 338 343 public Expression getSelectionCriteria() { 344 return null; 345 } 346 347 350 protected AbstractSession getSession() { 351 return getQuery().getSession(); 352 } 353 354 357 protected AbstractRecord getTranslationRow() { 358 return getQuery().getTranslationRow(); 359 } 360 361 364 protected WriteObjectQuery getWriteObjectQuery() { 365 return (WriteObjectQuery)getQuery(); 366 } 367 368 371 public abstract void insertObject() throws DatabaseException; 372 373 378 public void insertObject(boolean reprepare) { 379 insertObject(); 380 } 381 382 385 public void insertObjectForWrite() { 386 WriteObjectQuery writeQuery = getWriteObjectQuery(); 387 Object object = writeQuery.getObject(); 388 DescriptorQueryManager queryManager = getDescriptor().getQueryManager(); 389 390 if ((!writeQuery.isUserDefined()) &&queryManager.hasInsertQuery() &&isExpressionQueryMechanism()) { performUserDefinedInsert(); 395 return; 396 } 397 398 CommitManager commitManager = getSession().getCommitManager(); 399 400 commitManager.markPreModifyCommitInProgress(object); 402 403 if (writeQuery.getObjectChangeSet() == null) { 404 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 406 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreInsertEvent, writeQuery)); 409 } 410 } 411 412 if (writeQuery.shouldCascadeParts()) { 414 queryManager.preInsert(writeQuery); 415 } 416 417 if (commitManager.isShallowCommitted(object)) { 420 updateForeignKeyFieldAfterInsert(); 421 } else { 422 AbstractRecord modifyRow = writeQuery.getModifyRow(); 423 if (modifyRow == null) { if (writeQuery.shouldCascadeParts()) { 425 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRow(object, getSession())); 426 } else { 427 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForShallowInsert(object, getSession())); 428 } 429 } else { 430 if (writeQuery.shouldCascadeParts()) { 431 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRow(modifyRow, object, getSession())); 432 } else { 433 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForShallowInsert(modifyRow, object, getSession())); 434 } 435 } 436 437 writeQuery.setTranslationRow(getModifyRow()); 439 if (!writeQuery.getDescriptor().isAggregateCollectionDescriptor()) { writeQuery.setPrimaryKey(getDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, getSession())); 441 } 442 addWriteLockFieldForInsert(); 443 444 int modifyRowSize = getModifyRow().size(); 447 448 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 450 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToInsertEvent, writeQuery); 451 event.setRecord(getModifyRow()); 452 getDescriptor().getEventManager().executeEvent(event); 453 } 454 455 insertObject(modifyRowSize != getModifyRow().size()); 458 459 registerObjectInIdentityMap(); 461 if (writeQuery.getObjectChangeSet() != null) { 462 ((UnitOfWorkChangeSet)writeQuery.getObjectChangeSet().getUOWChangeSet()).putNewObjectInChangesList(writeQuery.getObjectChangeSet(), getSession()); 465 } 466 } 467 468 commitManager.markPostModifyCommitInProgress(object); 469 if (writeQuery.shouldCascadeParts()) { 471 queryManager.postInsert(writeQuery); 472 } 473 474 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 476 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostInsertEvent, writeQuery)); 477 } 478 } 479 480 483 public void insertObjectForWriteWithChangeSet() { 484 WriteObjectQuery writeQuery = getWriteObjectQuery(); 485 ObjectChangeSet objectChangeSet = writeQuery.getObjectChangeSet(); 486 DescriptorQueryManager queryManager = getDescriptor().getQueryManager(); 487 CommitManager commitManager = getSession().getCommitManager(); 488 489 if ((!writeQuery.isUserDefined()) &&queryManager.hasInsertQuery() &&isExpressionQueryMechanism()) { commitManager.markPreModifyCommitInProgress(objectChangeSet); 495 performUserDefinedInsert(); 496 return; 497 } 498 499 commitManager.markPreModifyCommitInProgress(objectChangeSet); 501 commitManager.markPreModifyCommitInProgress(writeQuery.getObject()); 502 503 if (writeQuery.shouldCascadeParts()) { 505 queryManager.preInsert(writeQuery); 506 } 507 508 if (commitManager.isShallowCommitted(objectChangeSet)) { 511 updateForeignKeyFieldAfterInsert(); 512 } else { 513 AbstractRecord modifyRow = writeQuery.getModifyRow(); 514 if (modifyRow == null) { if (writeQuery.shouldCascadeParts()) { 516 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowWithChangeSet(objectChangeSet, getSession())); 517 } else { 518 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForShallowInsertWithChangeSet(objectChangeSet, getSession())); 519 } 520 } else { 521 if (writeQuery.shouldCascadeParts()) { 522 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowWithChangeSet(modifyRow, objectChangeSet, getSession())); 523 } else { 524 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForShallowInsertWithChangeSet(modifyRow, objectChangeSet, getSession())); 525 } 526 } 527 528 writeQuery.setTranslationRow(getModifyRow()); 530 if (!writeQuery.getDescriptor().isAggregateCollectionDescriptor()) { writeQuery.setPrimaryKey(objectChangeSet.getPrimaryKeys()); 532 } 533 addWriteLockFieldForInsert(); 534 535 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 537 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToInsertEvent, writeQuery); 538 event.setRecord(getModifyRow()); 539 getDescriptor().getEventManager().executeEvent(event); 540 } 541 542 insertObject(); 543 544 registerObjectInIdentityMap(); 546 if (objectChangeSet != null) { 547 ((UnitOfWorkChangeSet)objectChangeSet.getUOWChangeSet()).putNewObjectInChangesList(objectChangeSet, getSession()); 550 } 551 } 552 553 commitManager.markPostModifyCommitInProgress(objectChangeSet); 554 commitManager.markPostModifyCommitInProgress(writeQuery.getObject()); 555 if (writeQuery.shouldCascadeParts()) { 557 queryManager.postInsert(writeQuery); 558 } 559 560 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 562 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostInsertEvent, writeQuery)); 563 } 564 } 565 566 569 public boolean isCallQueryMechanism() { 570 return false; 571 } 572 573 576 public boolean isExpressionQueryMechanism() { 577 return false; 578 } 579 580 583 public boolean isQueryByExampleMechanism() { 584 return false; 585 } 586 587 590 public boolean isStatementQueryMechanism() { 591 return false; 592 } 593 594 598 protected void performUserDefinedInsert() { 599 performUserDefinedWrite(getDescriptor().getQueryManager().getInsertQuery()); 600 } 601 602 606 protected void performUserDefinedUpdate() { 607 performUserDefinedWrite(getDescriptor().getQueryManager().getUpdateQuery()); 608 } 609 610 614 protected void performUserDefinedWrite(WriteObjectQuery userDefinedWriteQuery) { 615 userDefinedWriteQuery.checkPrepare(getSession(), getTranslationRow()); 616 617 Object object = getWriteObjectQuery().getObject(); 618 WriteObjectQuery writeQuery = (WriteObjectQuery)userDefinedWriteQuery.clone(); 619 writeQuery.setObject(object); 620 writeQuery.setObjectChangeSet(getWriteObjectQuery().getObjectChangeSet()); 621 writeQuery.setCascadePolicy(getQuery().getCascadePolicy()); 622 writeQuery.setShouldMaintainCache(getQuery().shouldMaintainCache()); 623 writeQuery.setTranslationRow(getTranslationRow()); 624 writeQuery.setModifyRow(getModifyRow()); 625 writeQuery.setPrimaryKey(getWriteObjectQuery().getPrimaryKey()); 626 writeQuery.setSession(getSession()); 627 writeQuery.prepareForExecution(); 628 629 if (getWriteObjectQuery().isUpdateObjectQuery() && (getWriteObjectQuery().getObjectChangeSet() != null)) { 633 writeQuery.executeCommitWithChangeSet(); 634 } else { 635 writeQuery.executeCommit(); 636 } 637 } 638 639 646 public void prepare() throws QueryException { 647 } 649 650 654 public abstract void prepareCursorSelectAllRows() throws QueryException; 655 656 660 public abstract void prepareDeleteAll() throws QueryException; 661 662 666 public abstract void prepareDeleteObject() throws QueryException; 667 668 672 public abstract void prepareDoesExist(DatabaseField field) throws QueryException; 673 674 678 public abstract void prepareExecuteNoSelect() throws QueryException; 679 680 684 public abstract void prepareExecuteSelect() throws QueryException; 685 686 691 public void prepareForExecution() throws QueryException { 692 } 694 695 699 public abstract void prepareInsertObject() throws QueryException; 700 701 705 public abstract void prepareReportQuerySelectAllRows() throws QueryException; 706 707 710 public abstract void prepareReportQuerySubSelect() throws QueryException; 711 712 716 public abstract void prepareSelectAllRows() throws QueryException; 717 718 722 public abstract void prepareSelectOneRow() throws QueryException; 723 724 728 public abstract void prepareUpdateObject() throws QueryException; 729 730 734 public abstract void prepareUpdateAll() throws QueryException; 735 736 739 protected void registerObjectInIdentityMap() { 740 WriteObjectQuery writeQuery = getWriteObjectQuery(); 741 Object object = writeQuery.getObject(); 742 743 if (writeQuery.shouldMaintainCache()) { 744 if (getDescriptor().usesOptimisticLocking()) { 745 Object optimisticLockValue = getDescriptor().getOptimisticLockingPolicy().getValueToPutInCache(writeQuery.getModifyRow(), getSession()); 746 getSession().getIdentityMapAccessorInstance().putInIdentityMap(object, writeQuery.getPrimaryKey(), optimisticLockValue, System.currentTimeMillis(), getDescriptor()); 747 } else { 748 getSession().getIdentityMapAccessorInstance().putInIdentityMap(object, writeQuery.getPrimaryKey(), null, System.currentTimeMillis(), getDescriptor()); 749 } 750 } 751 } 752 753 757 public abstract Vector selectAllReportQueryRows() throws DatabaseException; 758 759 762 public abstract Vector selectAllRows() throws DatabaseException; 763 764 767 public abstract AbstractRecord selectOneRow() throws DatabaseException; 768 769 772 public abstract AbstractRecord selectRowForDoesExist(DatabaseField field) throws DatabaseException; 773 774 777 public void setQuery(DatabaseQuery query) { 778 this.query = query; 779 } 780 781 784 protected void shallowInsertObjectForWrite(Object object, WriteObjectQuery writeQuery, CommitManager commitManager) throws DatabaseException, OptimisticLockException { 785 boolean doesExist; 786 787 if (getSession().isUnitOfWork()) { 788 UnitOfWorkImpl uow = (UnitOfWorkImpl)getSession(); 789 doesExist = !uow.isCloneNewObject(object); 790 if (doesExist) { 791 doesExist = uow.isObjectRegistered(object); 792 } 793 } else { 794 DoesExistQuery existQuery = (DoesExistQuery)getDescriptor().getQueryManager().getDoesExistQuery().clone(); 796 existQuery.setObject(object); 797 existQuery.setPrimaryKey(writeQuery.getPrimaryKey()); 798 existQuery.setDescriptor(getDescriptor()); 799 existQuery.setTranslationRow(getTranslationRow()); 800 801 doesExist = ((Boolean )getSession().executeQuery(existQuery)).booleanValue(); 802 } 803 804 if (!doesExist) { 805 writeQuery.dontCascadeParts(); 807 insertObjectForWrite(); 808 commitManager.markShallowCommit(object); 810 } 811 } 812 813 818 protected void updateForeignKeyFieldAfterInsert() { 819 WriteObjectQuery writeQuery = getWriteObjectQuery(); 820 Object object = writeQuery.getObject(); 821 822 writeQuery.setPrimaryKey(getDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, getSession())); 823 writeQuery.setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(object, getSession())); 826 827 updateForeignKeyFieldAfterInsert(writeQuery); 828 } 829 830 833 public abstract Integer updateAll() throws DatabaseException; 834 835 839 public abstract Integer updateObject() throws DatabaseException; 840 841 846 protected abstract void updateForeignKeyFieldAfterInsert(WriteObjectQuery writeQuery); 847 848 protected void updateObjectAndRowWithReturnRow(Collection returnFields, boolean isFirstCallForInsert) { 849 WriteObjectQuery writeQuery = getWriteObjectQuery(); 850 AbstractRecord outputRow = (AbstractRecord)writeQuery.getProperties().get("output"); 851 if ((outputRow == null) || outputRow.isEmpty()) { 852 return; 853 } 854 AbstractRecord row = new DatabaseRecord(); 855 for (Iterator iterator = returnFields.iterator(); iterator.hasNext();) { 856 DatabaseField field = (DatabaseField)iterator.next(); 857 if (outputRow.containsKey(field)) { 858 row.put(field, outputRow.get(field)); 859 } 860 } 861 if (row.isEmpty()) { 862 return; 863 } 864 865 Object object = writeQuery.getObject(); 866 867 getDescriptor().getObjectBuilder().assignReturnRow(object, getSession(), row); 868 869 Vector primaryKeys = null; 870 if (isFirstCallForInsert) { 871 AbstractRecord pkToModify = new DatabaseRecord(); 872 List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); 873 for (int i = 0; i < primaryKeyFields.size(); i++) { 874 DatabaseField field = (DatabaseField)primaryKeyFields.get(i); 875 if (row.containsKey(field)) { 876 pkToModify.put(field, row.get(field)); 877 } 878 } 879 if (!pkToModify.isEmpty()) { 880 primaryKeys = getDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, getSession()); 881 writeQuery.setPrimaryKey(primaryKeys); 882 getModifyRow().putAll(pkToModify); 884 getDescriptor().getObjectBuilder().addPrimaryKeyForNonDefaultTable(getModifyRow()); 885 } 886 } 887 888 if (getSession().isUnitOfWork()) { 890 ObjectChangeSet objectChangeSet = writeQuery.getObjectChangeSet(); 891 if ((objectChangeSet == null) && (((UnitOfWorkImpl)getSession()).getUnitOfWorkChangeSet() != null)) { 892 objectChangeSet = (ObjectChangeSet)((UnitOfWorkImpl)getSession()).getUnitOfWorkChangeSet().getObjectChangeSetForClone(object); 893 } 894 if (objectChangeSet != null) { 895 updateChangeSet(getDescriptor(), objectChangeSet, row, object); 896 if (primaryKeys != null) { 897 objectChangeSet.setCacheKey(new CacheKey(primaryKeys)); 898 } 899 } 900 } 901 } 902 903 908 public void updateChangeSet(ClassDescriptor desc, ObjectChangeSet objectChangeSet, AbstractRecord row, Object object) { 909 HashSet handledMappings = new HashSet(row.size()); 910 for (int i = 0; i < row.size(); i++) { 911 DatabaseField field = (DatabaseField)row.getFields().elementAt(i); 912 Object value = row.getValues().elementAt(i); 913 updateChangeSet(desc, objectChangeSet, field, object, handledMappings); 914 } 915 } 916 917 protected void updateChangeSet(ClassDescriptor desc, ObjectChangeSet objectChangeSet, DatabaseField field, Object object) { 918 updateChangeSet(desc, objectChangeSet, field, object, null); 919 } 920 921 protected void updateChangeSet(ClassDescriptor desc, ObjectChangeSet objectChangeSet, DatabaseField field, Object object, Collection handledMappings) { 922 DatabaseMapping mapping; 923 Vector mappingVector = desc.getObjectBuilder().getReadOnlyMappingsForField(field); 924 if (mappingVector != null) { 925 for (int j = 0; j < mappingVector.size(); j++) { 926 mapping = (DatabaseMapping)mappingVector.elementAt(j); 927 updateChangeSet(mapping, objectChangeSet, field, object, handledMappings); 928 } 929 } 930 mapping = desc.getObjectBuilder().getMappingForField(field); 931 if (mapping != null) { 932 updateChangeSet(mapping, objectChangeSet, field, object, handledMappings); 933 } 934 } 935 936 protected void updateChangeSet(DatabaseMapping mapping, ObjectChangeSet objectChangeSet, DatabaseField field, Object object, Collection handledMappings) { 937 if ((handledMappings != null) && handledMappings.contains(mapping)) { 938 return; 939 } 940 if (mapping.isAggregateObjectMapping()) { 941 Object aggregate = mapping.getAttributeValueFromObject(object); 942 AggregateChangeRecord record = (AggregateChangeRecord)objectChangeSet.getChangesForAttributeNamed(mapping.getAttributeName()); 943 if (aggregate != null) { 944 if (record == null) { 945 record = new AggregateChangeRecord(objectChangeSet); 946 record.setAttribute(mapping.getAttributeName()); 947 record.setMapping(mapping); 948 objectChangeSet.addChange(record); 949 } 950 ObjectChangeSet aggregateChangeSet = (oracle.toplink.essentials.internal.sessions.ObjectChangeSet)record.getChangedObject(); 951 ClassDescriptor aggregateDescriptor = ((AggregateObjectMapping)mapping).getReferenceDescriptor(); 952 if (aggregateChangeSet == null) { 953 aggregateChangeSet = aggregateDescriptor.getObjectBuilder().createObjectChangeSet(aggregate, (oracle.toplink.essentials.internal.sessions.UnitOfWorkChangeSet)((UnitOfWorkImpl)getSession()).getUnitOfWorkChangeSet(), getSession()); 954 record.setChangedObject(aggregateChangeSet); 955 } 956 updateChangeSet(aggregateDescriptor, aggregateChangeSet, field, aggregate, handledMappings); 957 } else { 958 if (record != null) { 959 record.setChangedObject(null); 960 } 961 } 962 } else if (mapping.isDirectToFieldMapping()) { 963 Object attributeValue = mapping.getAttributeValueFromObject(object); 964 objectChangeSet.updateChangeRecordForAttribute(mapping, attributeValue); 965 } else { 966 getSession().log(SessionLog.FINEST, SessionLog.QUERY, "field_for_unsupported_mapping_returned", field, getDescriptor()); 967 } 968 } 969 970 973 protected void updateObjectAndRowWithSequenceNumber() throws DatabaseException { 974 WriteObjectQuery writeQuery = getWriteObjectQuery(); 975 Object object = writeQuery.getObject(); 976 977 Object sequenceValue = getDescriptor().getObjectBuilder().assignSequenceNumber(object, getSession()); 978 if (sequenceValue == null) { 979 return; 980 } 981 Vector primaryKeys = getDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, getSession()); 982 writeQuery.setPrimaryKey(primaryKeys); 983 DatabaseField sequenceNumberField = getDescriptor().getSequenceNumberField(); 984 985 getModifyRow().put(sequenceNumberField, sequenceValue); 987 getDescriptor().getObjectBuilder().addPrimaryKeyForNonDefaultTable(getModifyRow()); 988 if (getSession().isUnitOfWork()) { 990 ObjectChangeSet objectChangeSet = writeQuery.getObjectChangeSet(); 991 if ((objectChangeSet == null) && (((UnitOfWorkImpl)getSession()).getUnitOfWorkChangeSet() != null)) { 992 objectChangeSet = (ObjectChangeSet)((UnitOfWorkImpl)getSession()).getUnitOfWorkChangeSet().getObjectChangeSetForClone(object); 993 } 994 if (objectChangeSet != null) { 995 updateChangeSet(getDescriptor(), objectChangeSet, sequenceNumberField, object); 996 objectChangeSet.setCacheKey(new CacheKey(primaryKeys)); 997 } 998 } 999 } 1000 1001 1004 public void updateObjectForWrite() { 1005 WriteObjectQuery writeQuery = getWriteObjectQuery(); 1006 Object object = writeQuery.getObject(); 1007 DescriptorQueryManager queryManager = getDescriptor().getQueryManager(); 1008 1009 if ((!writeQuery.isUserDefined()) &&queryManager.hasUpdateQuery() &&isExpressionQueryMechanism()) { performUserDefinedUpdate(); 1014 return; 1015 } 1016 1017 getSession().getCommitManager().markPreModifyCommitInProgress(object); 1019 1020 if (writeQuery.getObjectChangeSet() == null) { 1021 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1023 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreUpdateEvent, writeQuery)); 1026 } 1027 } 1028 1029 if (writeQuery.shouldCascadeParts()) { 1031 queryManager.preUpdate(writeQuery); 1032 } 1033 1034 if ((writeQuery.isUserDefined() || writeQuery.isCallQuery()) && (!getSession().isUnitOfWork())) { 1037 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRow(object, getSession())); 1038 } else { 1039 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForUpdate(writeQuery)); 1040 } 1041 1042 if (!getModifyRow().isEmpty()) { 1043 if ((writeQuery.isUserDefined() || writeQuery.isCallQuery()) && getSession().isUnitOfWork()) { 1045 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRow(object, getSession())); 1046 } 1047 1048 if (getDescriptor().usesOptimisticLocking()) { 1050 OptimisticLockingPolicy policy = getDescriptor().getOptimisticLockingPolicy(); 1051 policy.addLockValuesToTranslationRow(writeQuery); 1052 1053 policy.updateRowAndObjectForUpdate(writeQuery, object); 1055 } 1056 1057 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1059 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToUpdateEvent, writeQuery); 1060 event.setRecord(getModifyRow()); 1061 getDescriptor().getEventManager().executeEvent(event); 1062 } 1063 1064 int rowCount = updateObject().intValue(); 1065 1066 if (rowCount < 1) { 1067 getSession().getEventManager().noRowsModified(writeQuery, object); 1068 } 1069 if (getDescriptor().usesOptimisticLocking()) { 1070 getDescriptor().getOptimisticLockingPolicy().validateUpdate(rowCount, object, writeQuery); 1071 } 1072 } 1073 1074 getSession().getCommitManager().markPostModifyCommitInProgress(object); 1075 1076 if (writeQuery.shouldCascadeParts()) { 1078 queryManager.postUpdate(writeQuery); 1079 } 1080 1081 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1083 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostUpdateEvent, writeQuery)); 1084 } 1085 } 1086 1087 1090 public void updateObjectForWriteWithChangeSet() { 1091 WriteObjectQuery writeQuery = getWriteObjectQuery(); 1092 Object object = writeQuery.getObject(); 1093 DescriptorQueryManager queryManager = getDescriptor().getQueryManager(); 1094 1095 if ((!writeQuery.isUserDefined()) &&queryManager.hasUpdateQuery() &&isExpressionQueryMechanism()) { getSession().getCommitManager().markPreModifyCommitInProgress(writeQuery.getObjectChangeSet()); 1101 performUserDefinedUpdate(); 1102 return; 1103 } 1104 1105 getSession().getCommitManager().markPreModifyCommitInProgress(object); 1107 getSession().getCommitManager().markPreModifyCommitInProgress(writeQuery.getObjectChangeSet()); 1109 1110 if (writeQuery.getObjectChangeSet().hasChanges()) { 1111 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1113 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.PreUpdateWithChangesEvent, writeQuery); 1114 getDescriptor().getEventManager().executeEvent(event); 1115 1116 UnitOfWorkChangeSet uowChangeSet = (oracle.toplink.essentials.internal.sessions.UnitOfWorkChangeSet)((UnitOfWorkImpl)writeQuery.getSession()).getUnitOfWorkChangeSet(); 1118 writeQuery.getObjectChangeSet().clear(); 1121 if(writeQuery.getDescriptor().getObjectChangePolicy().calculateChanges(object, ((UnitOfWorkImpl)event.getSession()).getBackupClone(object), uowChangeSet, writeQuery.getSession(), writeQuery.getDescriptor(), false) == null) { 1122 uowChangeSet.getAllChangeSets().remove(writeQuery.getObjectChangeSet()); 1125 } 1126 } 1127 } 1128 1129 if (writeQuery.shouldCascadeParts()) { 1131 queryManager.preUpdate(writeQuery); 1132 } 1133 1134 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRowForUpdateWithChangeSet(writeQuery)); 1137 1138 Boolean shouldModifyVersionField = writeQuery.getObjectChangeSet().shouldModifyVersionField(); 1139 1140 if (!getModifyRow().isEmpty() || (shouldModifyVersionField != null) || writeQuery.getObjectChangeSet().hasCmpPolicyForcedUpdate()) { 1141 if (writeQuery.isUserDefined() || writeQuery.isCallQuery()) { 1143 writeQuery.setModifyRow(getDescriptor().getObjectBuilder().buildRow(object, getSession())); 1144 } 1145 1146 if (getDescriptor().usesOptimisticLocking()) { 1148 OptimisticLockingPolicy policy = getDescriptor().getOptimisticLockingPolicy(); 1149 policy.addLockValuesToTranslationRow(writeQuery); 1150 1151 if (!getModifyRow().isEmpty() || (shouldModifyVersionField.booleanValue() && policy instanceof VersionLockingPolicy)) { 1152 policy.updateRowAndObjectForUpdate(writeQuery, object); 1154 } else if (!shouldModifyVersionField.booleanValue() && policy instanceof VersionLockingPolicy) { 1155 ((VersionLockingPolicy)policy).writeLockValueIntoRow(writeQuery, object); 1156 } 1157 } 1158 1159 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1161 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToUpdateEvent, writeQuery); 1162 event.setRecord(getModifyRow()); 1163 getDescriptor().getEventManager().executeEvent(event); 1164 } 1165 1166 int rowCount = updateObject().intValue(); 1167 1168 if (rowCount < 1) { 1169 getSession().getEventManager().noRowsModified(writeQuery, object); 1170 } 1171 if (getDescriptor().usesOptimisticLocking()) { 1172 getDescriptor().getOptimisticLockingPolicy().validateUpdate(rowCount, object, writeQuery); 1173 } 1174 } 1175 1176 getSession().getCommitManager().markPostModifyCommitInProgress(object); 1177 getSession().getCommitManager().markPostModifyCommitInProgress(writeQuery.getObjectChangeSet()); 1178 1179 if (writeQuery.shouldCascadeParts()) { 1181 queryManager.postUpdate(writeQuery); 1182 } 1183 1184 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 1186 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostUpdateEvent, writeQuery)); 1187 } 1188 } 1189} 1190 | Popular Tags |