1 19 20 package org.apache.cayenne.access; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.LinkedHashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.apache.cayenne.PersistenceState; 33 import org.apache.cayenne.Persistent; 34 import org.apache.cayenne.map.DbAttribute; 35 import org.apache.cayenne.map.DbEntity; 36 import org.apache.cayenne.map.ObjEntity; 37 import org.apache.cayenne.query.UpdateBatchQuery; 38 import org.apache.cayenne.reflect.ClassDescriptor; 39 40 44 class DataDomainUpdateBucket extends DataDomainSyncBucket { 45 46 DataDomainUpdateBucket(DataDomainFlushAction parent) { 47 super(parent); 48 } 49 50 void appendQueriesInternal(Collection queries) { 51 52 DataDomainDBDiffBuilder diffBuilder = new DataDomainDBDiffBuilder(); 53 DataNodeSyncQualifierDescriptor qualifierBuilder = new DataNodeSyncQualifierDescriptor(); 54 55 for (Iterator i = dbEntities.iterator(); i.hasNext();) { 56 DbEntity dbEntity = (DbEntity) i.next(); 57 List objEntitiesForDbEntity = (List ) descriptorsByDbEntity.get(dbEntity); 58 Map batches = new LinkedHashMap (); 59 60 for (Iterator j = objEntitiesForDbEntity.iterator(); j.hasNext();) { 61 ClassDescriptor descriptor = (ClassDescriptor) j.next(); 62 ObjEntity entity = descriptor.getEntity(); 63 64 diffBuilder.reset(entity, dbEntity); 65 qualifierBuilder.reset(entity, dbEntity); 66 boolean isRootDbEntity = entity.getDbEntity() == dbEntity; 67 68 List objects = (List ) objectsByDescriptor.get(descriptor); 69 70 for (Iterator k = objects.iterator(); k.hasNext();) { 71 Persistent o = (Persistent) k.next(); 72 ObjectDiff diff = parent.objectDiff(o.getObjectId()); 73 74 Map snapshot = diffBuilder.buildDBDiff(diff); 75 76 if (snapshot == null) { 78 79 if (isRootDbEntity) { 80 k.remove(); 81 o.setPersistenceState(PersistenceState.COMMITTED); 82 } 83 84 continue; 85 } 86 87 checkReadOnly(entity); 90 91 Map qualifierSnapshot = qualifierBuilder 92 .createQualifierSnapshot(diff); 93 94 Set snapshotSet = snapshot.keySet(); 96 Set nullQualifierNames = new HashSet (); 97 Iterator it = qualifierSnapshot.entrySet().iterator(); 98 while (it.hasNext()) { 99 Map.Entry entry = (Map.Entry ) it.next(); 100 if (entry.getValue() == null) { 101 nullQualifierNames.add(entry.getKey()); 102 } 103 } 104 105 List batchKey = Arrays.asList(new Object [] { 106 snapshotSet, nullQualifierNames 107 }); 108 109 UpdateBatchQuery batch = (UpdateBatchQuery) batches.get(batchKey); 110 if (batch == null) { 111 batch = new UpdateBatchQuery( 112 dbEntity, 113 qualifierBuilder.getAttributes(), 114 updatedAttributes(dbEntity, snapshot), 115 nullQualifierNames, 116 10); 117 118 batch.setUsingOptimisticLocking(qualifierBuilder 119 .isUsingOptimisticLocking()); 120 batches.put(batchKey, batch); 121 } 122 123 batch.add(qualifierSnapshot, snapshot, o.getObjectId()); 124 125 if (isRootDbEntity) { 127 Map replacementId = o.getObjectId().getReplacementIdMap(); 128 129 Iterator pkIt = dbEntity.getPrimaryKey().iterator(); 130 while (pkIt.hasNext()) { 131 String name = ((DbAttribute) pkIt.next()).getName(); 132 if (snapshot.containsKey(name) 133 && !replacementId.containsKey(name)) { 134 replacementId.put(name, snapshot.get(name)); 135 } 136 } 137 } 138 } 139 } 140 141 queries.addAll(batches.values()); 142 } 143 } 144 145 148 private List updatedAttributes(DbEntity entity, Map updatedSnapshot) { 149 List attributes = new ArrayList (updatedSnapshot.size()); 150 Map entityAttributes = entity.getAttributeMap(); 151 152 Iterator it = updatedSnapshot.keySet().iterator(); 153 while (it.hasNext()) { 154 Object name = it.next(); 155 attributes.add(entityAttributes.get(name)); 156 } 157 158 return attributes; 159 } 160 } 161 | Popular Tags |