1 56 57 package org.objectstyle.cayenne.access; 58 59 import java.util.ArrayList ; 60 import java.util.Arrays ; 61 import java.util.Collection ; 62 import java.util.Collections ; 63 import java.util.HashMap ; 64 import java.util.HashSet ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.Map ; 68 import java.util.Set ; 69 70 import org.apache.commons.collections.map.LinkedMap; 71 import org.apache.log4j.Level; 72 import org.objectstyle.cayenne.CayenneException; 73 import org.objectstyle.cayenne.CayenneRuntimeException; 74 import org.objectstyle.cayenne.DataObject; 75 import org.objectstyle.cayenne.PersistenceState; 76 import org.objectstyle.cayenne.access.util.BatchQueryUtils; 77 import org.objectstyle.cayenne.access.util.PrimaryKeyHelper; 78 import org.objectstyle.cayenne.map.DbAttribute; 79 import org.objectstyle.cayenne.map.DbEntity; 80 import org.objectstyle.cayenne.map.DbJoin; 81 import org.objectstyle.cayenne.map.DbRelationship; 82 import org.objectstyle.cayenne.map.EntitySorter; 83 import org.objectstyle.cayenne.map.ObjAttribute; 84 import org.objectstyle.cayenne.map.ObjEntity; 85 import org.objectstyle.cayenne.map.ObjRelationship; 86 import org.objectstyle.cayenne.query.DeleteBatchQuery; 87 import org.objectstyle.cayenne.query.InsertBatchQuery; 88 import org.objectstyle.cayenne.query.Query; 89 import org.objectstyle.cayenne.query.UpdateBatchQuery; 90 91 101 class DataContextCommitAction { 103 104 private DataContext context; 105 private Level logLevel; 106 private Map newObjectsByObjEntity; 107 private Map objectsToDeleteByObjEntity; 108 private Map objectsToUpdateByObjEntity; 109 private List objEntitiesToInsert; 110 private List objEntitiesToDelete; 111 private List objEntitiesToUpdate; 112 private List nodeHelpers; 113 private List insObjects; private List delObjects; private List updObjects; 117 DataContextCommitAction(DataContext contextToCommit) { 118 context = contextToCommit; 119 } 120 121 124 void commit(Level logLevel) throws CayenneException { 125 if (logLevel == null) { 126 logLevel = QueryLogger.DEFAULT_LOG_LEVEL; 127 } 128 129 this.logLevel = logLevel; 130 131 synchronized (context.getObjectStore()) { 133 synchronized (context.getObjectStore().getDataRowCache()) { 134 135 categorizeObjects(); 136 createPrimaryKeys(); 137 categorizeFlattenedInsertsAndCreateBatches(); 138 categorizeFlattenedDeletesAndCreateBatches(); 139 140 insObjects = new ArrayList (); 141 delObjects = new ArrayList (); 142 updObjects = new ArrayList (); 143 144 for (Iterator i = nodeHelpers.iterator(); i.hasNext();) { 145 DataNodeCommitAction nodeHelper = (DataNodeCommitAction) i.next(); 146 prepareInsertQueries(nodeHelper); 147 prepareFlattenedQueries(nodeHelper, nodeHelper 148 .getFlattenedInsertQueries()); 149 150 prepareUpdateQueries(nodeHelper); 151 152 prepareFlattenedQueries(nodeHelper, nodeHelper 153 .getFlattenedDeleteQueries()); 154 155 prepareDeleteQueries(nodeHelper); 156 } 157 158 CommitObserver observer = new CommitObserver( 159 context, 160 insObjects, 161 updObjects, 162 delObjects); 163 164 observer.setLoggingLevel(logLevel); 165 166 if (context.isTransactionEventsEnabled()) { 167 observer.registerForDataContextEvents(); 168 } 169 170 try { 171 context.fireWillCommit(); 172 173 Transaction transaction = context 174 .getParentDataDomain() 175 .createTransaction(); 176 transaction.begin(); 177 178 try { 179 Iterator i = nodeHelpers.iterator(); 180 while (i.hasNext()) { 181 DataNodeCommitAction nodeHelper = (DataNodeCommitAction) i 182 .next(); 183 List queries = nodeHelper.getQueries(); 184 185 if (queries.size() > 0) { 186 nodeHelper.getNode().performQueries(queries, 188 observer, 189 transaction); 190 } 191 } 192 193 transaction.commit(); 195 } 196 catch (Throwable th) { 197 try { 198 transaction.rollback(); 200 } 201 catch (Throwable rollbackTh) { 202 } 204 205 context.fireTransactionRolledback(); 206 throw new CayenneException("Transaction was rolledback.", th); 207 } 208 209 context.getObjectStore().objectsCommitted(); 210 context.fireTransactionCommitted(); 211 } 212 finally { 213 if (context.isTransactionEventsEnabled()) { 214 observer.unregisterFromDataContextEvents(); 215 } 216 } 217 } 218 } 219 } 220 221 private void prepareInsertQueries(DataNodeCommitAction commitHelper) 222 throws CayenneException { 223 224 List entities = commitHelper.getObjEntitiesForInsert(); 225 if (entities.isEmpty()) { 226 return; 227 } 228 229 boolean supportsGeneratedKeys = commitHelper 230 .getNode() 231 .getAdapter() 232 .supportsGeneratedKeys(); 233 List dbEntities = new ArrayList (entities.size()); 234 Map objEntitiesByDbEntity = new HashMap (entities.size()); 235 groupObjEntitiesBySpannedDbEntities(dbEntities, objEntitiesByDbEntity, entities); 236 237 EntitySorter sorter = commitHelper.getNode().getEntitySorter(); 238 sorter.sortDbEntities(dbEntities, false); 239 240 Iterator i = dbEntities.iterator(); 241 while (i.hasNext()) { 242 DbEntity dbEntity = (DbEntity) i.next(); 243 List objEntitiesForDbEntity = (List ) objEntitiesByDbEntity.get(dbEntity); 244 245 InsertBatchQuery batch = new InsertBatchQuery(dbEntity, 27); 246 batch.setLoggingLevel(logLevel); 247 248 for (Iterator j = objEntitiesForDbEntity.iterator(); j.hasNext();) { 249 ObjEntity entity = (ObjEntity) j.next(); 250 boolean isMasterDbEntity = (entity.getDbEntity() == dbEntity); 251 DbRelationship masterDependentDbRel = (isMasterDbEntity 252 ? null 253 : findMasterToDependentDbRelationship(entity.getDbEntity(), 254 dbEntity)); 255 256 List objects = (List ) newObjectsByObjEntity.get(entity.getClassName()); 257 258 if (entity.isReadOnly() && objects.size() > 0) { 260 throw attemptToCommitReadOnlyEntity(objects.get(0).getClass(), entity); 261 } 262 263 if (isMasterDbEntity) { 264 sorter.sortObjectsForEntity(entity, objects, false); 265 } 266 267 for (Iterator k = objects.iterator(); k.hasNext();) { 268 DataObject o = (DataObject) k.next(); 269 Map snapshot = BatchQueryUtils.buildSnapshotForInsert(entity, 270 o, 271 masterDependentDbRel, 272 supportsGeneratedKeys); 273 batch.add(snapshot, o.getObjectId()); 274 } 275 276 if (isMasterDbEntity) { 277 insObjects.addAll(objects); 278 } 279 } 280 commitHelper.getQueries().add(batch); 281 } 282 } 283 284 private void prepareDeleteQueries(DataNodeCommitAction commitHelper) 285 throws CayenneException { 286 287 List entities = commitHelper.getObjEntitiesForDelete(); 288 if (entities.isEmpty()) { 289 return; 290 } 291 292 List dbEntities = new ArrayList (entities.size()); 293 Map objEntitiesByDbEntity = new HashMap (entities.size()); 294 groupObjEntitiesBySpannedDbEntities(dbEntities, objEntitiesByDbEntity, entities); 295 296 EntitySorter sorter = commitHelper.getNode().getEntitySorter(); 297 sorter.sortDbEntities(dbEntities, true); 298 299 for (Iterator i = dbEntities.iterator(); i.hasNext();) { 300 DbEntity dbEntity = (DbEntity) i.next(); 301 List objEntitiesForDbEntity = (List ) objEntitiesByDbEntity.get(dbEntity); 302 Map batches = new LinkedMap(); 303 304 for (Iterator j = objEntitiesForDbEntity.iterator(); j.hasNext();) { 305 ObjEntity entity = (ObjEntity) j.next(); 306 307 boolean optimisticLocking = (ObjEntity.LOCK_TYPE_OPTIMISTIC == entity 309 .getLockType()); 310 311 List qualifierAttributes = qualifierAttributes(entity, optimisticLocking); 312 313 boolean isRootDbEntity = (entity.getDbEntity() == dbEntity); 314 DbRelationship masterDependentDbRel = (isRootDbEntity 315 ? null 316 : findMasterToDependentDbRelationship(entity.getDbEntity(), 317 dbEntity)); 318 319 List objects = (List ) objectsToDeleteByObjEntity.get(entity 320 .getClassName()); 321 322 if (entity.isReadOnly() && objects.size() > 0) { 324 throw attemptToCommitReadOnlyEntity(objects.get(0).getClass(), entity); 325 } 326 327 if (isRootDbEntity) { 328 sorter.sortObjectsForEntity(entity, objects, true); 329 } 330 331 for (Iterator k = objects.iterator(); k.hasNext();) { 332 DataObject o = (DataObject) k.next(); 333 334 Map idSnapshot = o.getObjectId().getIdSnapshot(); 336 337 if (idSnapshot == null || idSnapshot.isEmpty()) { 338 continue; 340 } 341 342 if (!isRootDbEntity && masterDependentDbRel != null) { 343 idSnapshot = masterDependentDbRel 344 .targetPkSnapshotWithSrcSnapshot(idSnapshot); 345 } 346 347 Map qualifierSnapshot = idSnapshot; 348 if (optimisticLocking) { 349 qualifierSnapshot = new HashMap (qualifierSnapshot); 351 appendOptimisticLockingAttributes(qualifierSnapshot, 352 o, 353 qualifierAttributes); 354 } 355 356 Set nullQualifierNames = new HashSet (); 358 Iterator it = qualifierSnapshot.entrySet().iterator(); 359 while (it.hasNext()) { 360 Map.Entry entry = (Map.Entry ) it.next(); 361 if (entry.getValue() == null) { 362 nullQualifierNames.add(entry.getKey()); 363 } 364 } 365 366 List batchKey = Arrays.asList(new Object [] { 367 nullQualifierNames 368 }); 369 370 DeleteBatchQuery batch = (DeleteBatchQuery) batches.get(batchKey); 371 if (batch == null) { 372 batch = new DeleteBatchQuery( 373 dbEntity, 374 qualifierAttributes, 375 nullQualifierNames, 376 27); 377 batch.setLoggingLevel(logLevel); 378 batch.setUsingOptimisticLocking(optimisticLocking); 379 batches.put(batchKey, batch); 380 } 381 382 batch.add(qualifierSnapshot); 383 384 } 385 386 if (isRootDbEntity) 387 delObjects.addAll(objects); 388 389 } 390 commitHelper.getQueries().addAll(batches.values()); 391 } 392 } 393 394 private void prepareUpdateQueries(DataNodeCommitAction commitHelper) 395 throws CayenneException { 396 List entities = commitHelper.getObjEntitiesForUpdate(); 397 if (entities.isEmpty()) { 398 return; 399 } 400 401 List dbEntities = new ArrayList (entities.size()); 402 Map objEntitiesByDbEntity = new HashMap (entities.size()); 403 groupObjEntitiesBySpannedDbEntities(dbEntities, objEntitiesByDbEntity, entities); 404 405 for (Iterator i = dbEntities.iterator(); i.hasNext();) { 406 DbEntity dbEntity = (DbEntity) i.next(); 407 List objEntitiesForDbEntity = (List ) objEntitiesByDbEntity.get(dbEntity); 408 Map batches = new LinkedMap(); 409 410 for (Iterator j = objEntitiesForDbEntity.iterator(); j.hasNext();) { 411 ObjEntity entity = (ObjEntity) j.next(); 412 413 boolean optimisticLocking = (ObjEntity.LOCK_TYPE_OPTIMISTIC == entity 415 .getLockType()); 416 417 List qualifierAttributes = qualifierAttributes(entity, optimisticLocking); 418 419 boolean isRootDbEntity = entity.getDbEntity() == dbEntity; 420 421 DbRelationship masterDependentDbRel = (isRootDbEntity) 422 ? null 423 : findMasterToDependentDbRelationship(entity.getDbEntity(), 424 dbEntity); 425 List objects = (List ) objectsToUpdateByObjEntity.get(entity 426 .getClassName()); 427 428 for (Iterator k = objects.iterator(); k.hasNext();) { 429 DataObject o = (DataObject) k.next(); 430 431 Map snapshot = BatchQueryUtils.buildSnapshotForUpdate(entity, 432 o, 433 masterDependentDbRel); 434 435 if (snapshot.isEmpty()) { 438 o.setPersistenceState(PersistenceState.COMMITTED); 439 continue; 440 } 441 442 if (entity.isReadOnly()) { 445 throw attemptToCommitReadOnlyEntity(o.getClass(), entity); 446 } 447 448 Map idSnapshot = o.getObjectId().getIdSnapshot(); 450 451 if (!isRootDbEntity && masterDependentDbRel != null) { 452 idSnapshot = masterDependentDbRel 453 .targetPkSnapshotWithSrcSnapshot(idSnapshot); 454 } 455 456 Map qualifierSnapshot = idSnapshot; 457 if (optimisticLocking) { 458 qualifierSnapshot = new HashMap (qualifierSnapshot); 460 appendOptimisticLockingAttributes(qualifierSnapshot, 461 o, 462 qualifierAttributes); 463 } 464 465 Set snapshotSet = snapshot.keySet(); 467 Set nullQualifierNames = new HashSet (); 468 Iterator it = qualifierSnapshot.entrySet().iterator(); 469 while (it.hasNext()) { 470 Map.Entry entry = (Map.Entry ) it.next(); 471 if (entry.getValue() == null) { 472 nullQualifierNames.add(entry.getKey()); 473 } 474 } 475 476 List batchKey = Arrays.asList(new Object [] { 477 snapshotSet, nullQualifierNames 478 }); 479 480 UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey); 481 if (batch == null) { 482 batch = new UpdateBatchQuery( 483 dbEntity, 484 qualifierAttributes, 485 updatedAttributes(dbEntity, snapshot), 486 nullQualifierNames, 487 10); 488 batch.setLoggingLevel(logLevel); 489 batch.setUsingOptimisticLocking(optimisticLocking); 490 batches.put(batchKey, batch); 491 } 492 493 batch.add(qualifierSnapshot, snapshot); 494 495 if (isRootDbEntity) { 496 updateId(idSnapshot, 497 o.getObjectId().getReplacementIdMap(), 498 snapshot); 499 updObjects.add(o); 500 } 501 } 502 } 503 commitHelper.getQueries().addAll(batches.values()); 504 } 505 } 506 507 510 private List qualifierAttributes(ObjEntity entity, boolean optimisticLocking) { 511 if (!optimisticLocking) { 512 return entity.getDbEntity().getPrimaryKey(); 513 } 514 515 List attributes = new ArrayList (entity.getDbEntity().getPrimaryKey()); 516 517 Iterator attributeIt = entity.getAttributes().iterator(); 518 while (attributeIt.hasNext()) { 519 ObjAttribute attribute = (ObjAttribute) attributeIt.next(); 520 521 if (attribute.isUsedForLocking()) { 522 DbAttribute dbAttribute = (DbAttribute) attribute 524 .getDbPathIterator() 525 .next(); 526 527 if (!attributes.contains(dbAttribute)) { 528 attributes.add(dbAttribute); 529 } 530 } 531 } 532 533 Iterator relationshipIt = entity.getRelationships().iterator(); 534 while (relationshipIt.hasNext()) { 535 ObjRelationship relationship = (ObjRelationship) relationshipIt.next(); 536 537 if (relationship.isUsedForLocking()) { 538 DbRelationship dbRelationship = (DbRelationship) relationship 540 .getDbRelationships() 541 .get(0); 542 543 Iterator joinsIterator = dbRelationship.getJoins().iterator(); 544 while (joinsIterator.hasNext()) { 545 DbJoin dbAttrPair = (DbJoin) joinsIterator.next(); 546 DbAttribute dbAttribute = dbAttrPair.getSource(); 547 if (!attributes.contains(dbAttribute)) { 548 attributes.add(dbAttribute); 549 } 550 } 551 } 552 } 553 554 return attributes; 555 } 556 557 563 private List updatedAttributes(DbEntity entity, Map updatedSnapshot) { 564 List attributes = new ArrayList (updatedSnapshot.size()); 565 Map entityAttributes = entity.getAttributeMap(); 566 567 Iterator it = updatedSnapshot.keySet().iterator(); 568 while (it.hasNext()) { 569 Object name = it.next(); 570 attributes.add(entityAttributes.get(name)); 571 } 572 573 return attributes; 574 } 575 576 579 private void appendOptimisticLockingAttributes( 580 Map qualifierSnapshot, 581 DataObject dataObject, 582 List qualifierAttributes) { 583 584 Map snapshot = null; 585 586 Iterator it = qualifierAttributes.iterator(); 587 while (it.hasNext()) { 588 DbAttribute attribute = (DbAttribute) it.next(); 589 String name = attribute.getName(); 590 if (!qualifierSnapshot.containsKey(name)) { 591 592 if (snapshot == null) { 594 snapshot = dataObject 595 .getDataContext() 596 .getObjectStore() 597 .getCachedSnapshot(dataObject.getObjectId()); 598 599 if (snapshot == null) { 601 throw new CayenneRuntimeException( 602 "Can't build qualifier for optimistic locking, " 603 + "no snapshot for id " 604 + dataObject.getObjectId()); 605 } 606 } 607 608 qualifierSnapshot.put(name, snapshot.get(name)); 609 } 610 } 611 } 612 613 private void createPrimaryKeys() throws CayenneException { 614 615 DataDomain domain = context.getParentDataDomain(); 616 PrimaryKeyHelper pkHelper = domain.getPrimaryKeyHelper(); 617 618 Collections.sort(objEntitiesToInsert, pkHelper.getObjEntityComparator()); 619 Iterator i = objEntitiesToInsert.iterator(); 620 while (i.hasNext()) { 621 ObjEntity currentEntity = (ObjEntity) i.next(); 622 List dataObjects = (List ) newObjectsByObjEntity.get(currentEntity 623 .getClassName()); 624 pkHelper.createPermIdsForObjEntity(currentEntity, dataObjects); 625 } 626 } 627 628 631 private void categorizeObjects() throws CayenneException { 632 this.nodeHelpers = new ArrayList (); 633 634 Iterator it = context.getObjectStore().getObjectIterator(); 635 newObjectsByObjEntity = new HashMap (); 636 objectsToDeleteByObjEntity = new HashMap (); 637 objectsToUpdateByObjEntity = new HashMap (); 638 objEntitiesToInsert = new ArrayList (); 639 objEntitiesToDelete = new ArrayList (); 640 objEntitiesToUpdate = new ArrayList (); 641 642 while (it.hasNext()) { 643 DataObject nextObject = (DataObject) it.next(); 644 int objectState = nextObject.getPersistenceState(); 645 switch (objectState) { 646 case PersistenceState.NEW: 647 objectToInsert(nextObject); 648 break; 649 case PersistenceState.DELETED: 650 objectToDelete(nextObject); 651 break; 652 case PersistenceState.MODIFIED: 653 objectToUpdate(nextObject); 654 break; 655 } 656 } 657 } 658 659 private void objectToInsert(DataObject o) throws CayenneException { 660 classifyByEntityAndNode(o, 661 newObjectsByObjEntity, 662 objEntitiesToInsert, 663 DataNodeCommitAction.INSERT); 664 } 665 666 private void objectToDelete(DataObject o) throws CayenneException { 667 classifyByEntityAndNode(o, 668 objectsToDeleteByObjEntity, 669 objEntitiesToDelete, 670 DataNodeCommitAction.DELETE); 671 } 672 673 private void objectToUpdate(DataObject o) throws CayenneException { 674 classifyByEntityAndNode(o, 675 objectsToUpdateByObjEntity, 676 objEntitiesToUpdate, 677 DataNodeCommitAction.UPDATE); 678 } 679 680 private RuntimeException attemptToCommitReadOnlyEntity( 681 Class objectClass, 682 ObjEntity entity) { 683 String className = (objectClass != null) ? objectClass.getName() : "<null>"; 684 StringBuffer message = new StringBuffer (); 685 message 686 .append("Class '") 687 .append(className) 688 .append("' maps to a read-only entity"); 689 690 if (entity != null) { 691 message.append(" '").append(entity.getName()).append("'"); 692 } 693 694 message.append(". Can't commit changes."); 695 return new CayenneRuntimeException(message.toString()); 696 } 697 698 702 private void classifyByEntityAndNode( 703 DataObject o, 704 Map objectsByObjEntity, 705 List objEntities, 706 int operationType) { 707 708 Class objEntityClass = o.getObjectId().getObjectClass(); 709 ObjEntity entity = context.getEntityResolver().lookupObjEntity(objEntityClass); 710 Collection objectsForObjEntity = (Collection ) objectsByObjEntity 711 .get(objEntityClass.getName()); 712 if (objectsForObjEntity == null) { 713 objEntities.add(entity); 714 DataNode responsibleNode = context.lookupDataNode(entity.getDataMap()); 715 716 DataNodeCommitAction commitHelper = nodeHelper(responsibleNode); 717 718 commitHelper.addToEntityList(entity, operationType); 719 objectsForObjEntity = new ArrayList (); 720 objectsByObjEntity.put(objEntityClass.getName(), objectsForObjEntity); 721 } 722 objectsForObjEntity.add(o); 723 } 724 725 private void categorizeFlattenedInsertsAndCreateBatches() { 726 Iterator i = context.getObjectStore().getFlattenedInserts().iterator(); 727 728 while (i.hasNext()) { 729 FlattenedRelationshipUpdate info = (FlattenedRelationshipUpdate) i.next(); 730 731 DataObject source = info.getSource(); 732 733 if (source.getPersistenceState() == PersistenceState.DELETED) { 735 continue; 736 } 737 738 if (info.getDestination().getPersistenceState() == PersistenceState.DELETED) { 739 continue; 740 } 741 742 DbEntity flattenedEntity = info.getJoinEntity(); 743 DataNode responsibleNode = context.lookupDataNode(flattenedEntity 744 .getDataMap()); 745 DataNodeCommitAction commitHelper = nodeHelper(responsibleNode); 746 Map batchesByDbEntity = commitHelper.getFlattenedInsertQueries(); 747 748 InsertBatchQuery relationInsertQuery = (InsertBatchQuery) batchesByDbEntity 749 .get(flattenedEntity); 750 751 if (relationInsertQuery == null) { 752 relationInsertQuery = new InsertBatchQuery(flattenedEntity, 50); 753 relationInsertQuery.setLoggingLevel(logLevel); 754 batchesByDbEntity.put(flattenedEntity, relationInsertQuery); 755 } 756 757 Map flattenedSnapshot = info.buildJoinSnapshotForInsert(); 758 relationInsertQuery.add(flattenedSnapshot); 759 } 760 } 761 762 private void categorizeFlattenedDeletesAndCreateBatches() { 763 Iterator i = context.getObjectStore().getFlattenedDeletes().iterator(); 764 765 while (i.hasNext()) { 766 FlattenedRelationshipUpdate info = (FlattenedRelationshipUpdate) i.next(); 767 768 Map sourceId = info.getSource().getObjectId().getIdSnapshot(); 770 771 if (sourceId == null) 772 continue; 773 774 Map dstId = info.getDestination().getObjectId().getIdSnapshot(); 775 if (dstId == null) 776 continue; 777 778 DbEntity flattenedEntity = info.getJoinEntity(); 779 780 DataNode responsibleNode = context.lookupDataNode(flattenedEntity 781 .getDataMap()); 782 DataNodeCommitAction commitHelper = nodeHelper(responsibleNode); 783 Map batchesByDbEntity = commitHelper.getFlattenedDeleteQueries(); 784 785 DeleteBatchQuery relationDeleteQuery = (DeleteBatchQuery) batchesByDbEntity 786 .get(flattenedEntity); 787 if (relationDeleteQuery == null) { 788 boolean optimisticLocking = false; 789 relationDeleteQuery = new DeleteBatchQuery(flattenedEntity, 50); 790 relationDeleteQuery.setUsingOptimisticLocking(optimisticLocking); 791 relationDeleteQuery.setLoggingLevel(logLevel); 792 batchesByDbEntity.put(flattenedEntity, relationDeleteQuery); 793 } 794 795 List flattenedSnapshots = info.buildJoinSnapshotsForDelete(); 796 if (!flattenedSnapshots.isEmpty()) { 797 Iterator snapsIt = flattenedSnapshots.iterator(); 798 while (snapsIt.hasNext()) { 799 relationDeleteQuery.add((Map ) snapsIt.next()); 800 } 801 } 802 } 803 } 804 805 private void prepareFlattenedQueries( 806 DataNodeCommitAction commitHelper, 807 Map flattenedBatches) { 808 809 for (Iterator i = flattenedBatches.values().iterator(); i.hasNext();) { 810 commitHelper.addToQueries((Query) i.next()); 811 } 812 } 813 814 private void updateId(Map oldID, Map replacementID, Map updatedKeys) { 816 Iterator it = updatedKeys.entrySet().iterator(); 817 818 while (it.hasNext()) { 819 Map.Entry entry = (Map.Entry ) it.next(); 820 Object key = entry.getKey(); 821 822 if (oldID.containsKey(key) && !replacementID.containsKey(key)) { 823 replacementID.put(key, entry.getValue()); 824 } 825 } 826 } 827 828 private void groupObjEntitiesBySpannedDbEntities( 829 List dbEntities, 830 Map objEntitiesByDbEntity, 831 List objEntities) { 832 833 Iterator i = objEntities.iterator(); 834 while (i.hasNext()) { 835 ObjEntity objEntity = (ObjEntity) i.next(); 836 DbEntity dbEntity = objEntity.getDbEntity(); 837 838 List objEntitiesForDbEntity = (List ) objEntitiesByDbEntity.get(dbEntity); 839 if (objEntitiesForDbEntity == null) { 840 objEntitiesForDbEntity = new ArrayList (1); 841 dbEntities.add(dbEntity); 842 objEntitiesByDbEntity.put(dbEntity, objEntitiesForDbEntity); 843 } 844 if (!objEntitiesForDbEntity.contains(objEntity)) 845 objEntitiesForDbEntity.add(objEntity); 846 for (Iterator j = objEntity.getAttributeMap().values().iterator(); j 847 .hasNext();) { 848 ObjAttribute objAttribute = (ObjAttribute) j.next(); 849 if (!objAttribute.isCompound()) 850 continue; 851 dbEntity = (DbEntity) objAttribute.getDbAttribute().getEntity(); 852 objEntitiesForDbEntity = (List ) objEntitiesByDbEntity.get(dbEntity); 853 if (objEntitiesForDbEntity == null) { 854 objEntitiesForDbEntity = new ArrayList (1); 855 dbEntities.add(dbEntity); 856 objEntitiesByDbEntity.put(dbEntity, objEntitiesForDbEntity); 857 } 858 if (!objEntitiesForDbEntity.contains(objEntity)) 859 objEntitiesForDbEntity.add(objEntity); 860 } 861 } 862 } 863 864 private DbRelationship findMasterToDependentDbRelationship( 865 DbEntity masterDbEntity, 866 DbEntity dependentDbEntity) { 867 if (masterDbEntity.equals(dependentDbEntity)) 868 return null; 869 for (Iterator i = masterDbEntity.getRelationshipMap().values().iterator(); i 870 .hasNext();) { 871 DbRelationship rel = (DbRelationship) i.next(); 872 if (dependentDbEntity.equals(rel.getTargetEntity()) && rel.isToDependentPK()) 873 return rel; 874 } 875 return null; 876 } 877 878 882 private DataNodeCommitAction nodeHelper(DataNode node) { 883 884 DataNodeCommitAction helper = null; 885 Iterator it = nodeHelpers.iterator(); 886 while (it.hasNext()) { 887 DataNodeCommitAction itHelper = (DataNodeCommitAction) it.next(); 888 if (itHelper.getNode() == node) { 889 helper = itHelper; 890 break; 891 } 892 } 893 894 if (helper == null) { 895 helper = new DataNodeCommitAction(node); 896 nodeHelpers.add(helper); 897 } 898 899 return helper; 900 } 901 } | Popular Tags |