1 19 20 package org.apache.cayenne.access; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import org.apache.cayenne.Persistent; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.map.EntitySorter; 34 import org.apache.cayenne.query.DeleteBatchQuery; 35 import org.apache.cayenne.reflect.ClassDescriptor; 36 37 41 class DataDomainDeleteBucket extends DataDomainSyncBucket { 42 43 DataDomainDeleteBucket(DataDomainFlushAction parent) { 44 super(parent); 45 } 46 47 void postprocess() { 48 49 if (!objectsByDescriptor.isEmpty()) { 50 51 Collection deletedIds = parent.getResultDeletedIds(); 52 53 Iterator it = objectsByDescriptor.values().iterator(); 54 while (it.hasNext()) { 55 Iterator objects = ((Collection ) it.next()).iterator(); 56 while (objects.hasNext()) { 57 Persistent object = (Persistent) objects.next(); 58 deletedIds.add(object.getObjectId()); 59 } 60 } 61 } 62 } 63 64 void appendQueriesInternal(Collection queries) { 65 66 DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor(); 67 68 EntitySorter sorter = parent.getDomain().getEntitySorter(); 69 sorter.sortDbEntities(dbEntities, true); 70 71 for (Iterator i = dbEntities.iterator(); i.hasNext();) { 72 DbEntity dbEntity = (DbEntity) i.next(); 73 List objEntitiesForDbEntity = (List ) descriptorsByDbEntity.get(dbEntity); 74 Map batches = new LinkedHashMap (); 75 76 for (Iterator j = objEntitiesForDbEntity.iterator(); j.hasNext();) { 77 ClassDescriptor descriptor = (ClassDescriptor) j.next(); 78 79 qualifierBuilder.reset(descriptor.getEntity(), dbEntity); 80 81 boolean isRootDbEntity = (descriptor.getEntity().getDbEntity() == dbEntity); 82 83 List objects = (List ) objectsByDescriptor.get(descriptor); 86 87 if (objects.isEmpty()) { 88 continue; 89 } 90 91 checkReadOnly(descriptor.getEntity()); 92 93 if (isRootDbEntity) { 94 sorter.sortObjectsForEntity(descriptor.getEntity(), objects, true); 95 } 96 97 for (Iterator k = objects.iterator(); k.hasNext();) { 98 Persistent o = (Persistent) k.next(); 99 ObjectDiff diff = parent.objectDiff(o.getObjectId()); 100 101 Map qualifierSnapshot = qualifierBuilder 102 .createQualifierSnapshot(diff); 103 104 Set nullQualifierNames = new HashSet (); 106 Iterator it = qualifierSnapshot.entrySet().iterator(); 107 while (it.hasNext()) { 108 Map.Entry entry = (Map.Entry ) it.next(); 109 if (entry.getValue() == null) { 110 nullQualifierNames.add(entry.getKey()); 111 } 112 } 113 114 List batchKey = Arrays.asList(new Object [] { 115 nullQualifierNames 116 }); 117 118 DeleteBatchQuery batch = (DeleteBatchQuery) batches.get(batchKey); 119 if (batch == null) { 120 batch = new DeleteBatchQuery(dbEntity, qualifierBuilder 121 .getAttributes(), nullQualifierNames, 27); 122 batch.setUsingOptimisticLocking(qualifierBuilder 123 .isUsingOptimisticLocking()); 124 batches.put(batchKey, batch); 125 } 126 127 batch.add(qualifierSnapshot); 128 } 129 } 130 131 queries.addAll(batches.values()); 132 } 133 } 134 } 135 | Popular Tags |