1 19 20 package org.apache.cayenne.access; 21 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.ObjectId; 28 import org.apache.cayenne.graph.GraphChangeHandler; 29 import org.apache.cayenne.graph.GraphDiff; 30 import org.apache.cayenne.map.DbEntity; 31 import org.apache.cayenne.map.EntityResolver; 32 import org.apache.cayenne.map.ObjEntity; 33 import org.apache.cayenne.map.ObjRelationship; 34 35 42 final class DataDomainIndirectDiffBuilder implements GraphChangeHandler { 43 44 private final DataDomainFlushAction parent; 45 private final EntityResolver resolver; 46 private final Collection indirectModifications; 47 private final Collection flattenedInserts; 48 private final Collection flattenedDeletes; 49 50 DataDomainIndirectDiffBuilder(DataDomainFlushAction parent) { 51 this.parent = parent; 52 this.indirectModifications = parent.getResultIndirectlyModifiedIds(); 53 this.resolver = parent.getDomain().getEntityResolver(); 54 this.flattenedInserts = new HashSet (); 55 this.flattenedDeletes = new HashSet (); 56 } 57 58 void processIndirectChanges(GraphDiff allChanges) { 59 allChanges.apply(this); 61 62 if (!flattenedInserts.isEmpty()) { 63 Iterator it = flattenedInserts.iterator(); 64 while (it.hasNext()) { 65 FlattenedArcKey key = (FlattenedArcKey) it.next(); 66 DbEntity entity = key.getJoinEntity(); 67 parent.addFlattenedInsert(entity, key); 68 } 69 } 70 71 if (!flattenedDeletes.isEmpty()) { 72 Iterator it = flattenedDeletes.iterator(); 73 while (it.hasNext()) { 74 FlattenedArcKey key = (FlattenedArcKey) it.next(); 75 DbEntity entity = key.getJoinEntity(); 76 parent.addFlattenedDelete(entity, key); 77 } 78 } 79 } 80 81 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 82 ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName()); 83 ObjRelationship relationship = (ObjRelationship) entity.getRelationship(arcId 84 .toString()); 85 86 if (relationship.isSourceIndependentFromTargetChange()) { 87 88 if (!((ObjectId) nodeId).isTemporary()) { 89 indirectModifications.add(nodeId); 90 } 91 92 if (relationship.isFlattened()) { 93 if (relationship.isReadOnly()) { 94 throw new CayenneRuntimeException( 95 "Cannot set the read-only flattened relationship " 96 + relationship.getName()); 97 } 98 99 FlattenedArcKey key = new FlattenedArcKey( 102 (ObjectId) nodeId, 103 (ObjectId) targetNodeId, 104 relationship); 105 106 if (!flattenedDeletes.remove(key)) { 108 flattenedInserts.add(key); 109 } 110 } 111 } 112 } 113 114 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 115 116 ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName()); 117 ObjRelationship relationship = (ObjRelationship) entity.getRelationship(arcId 118 .toString()); 119 120 if (relationship.isSourceIndependentFromTargetChange()) { 121 if (!((ObjectId) nodeId).isTemporary()) { 123 indirectModifications.add(nodeId); 124 } 125 126 if (relationship.isFlattened()) { 127 if (relationship.isReadOnly()) { 128 throw new CayenneRuntimeException( 129 "Cannot unset the read-only flattened relationship " 130 + relationship.getName()); 131 } 132 133 FlattenedArcKey key = new FlattenedArcKey( 136 (ObjectId) nodeId, 137 (ObjectId) targetNodeId, 138 relationship); 139 140 if (!flattenedInserts.remove(key)) { 143 flattenedDeletes.add(key); 144 } 145 } 146 } 147 } 148 149 public void nodeIdChanged(Object nodeId, Object newId) { 150 } 152 153 public void nodeCreated(Object nodeId) { 154 } 156 157 public void nodeRemoved(Object nodeId) { 158 } 160 161 public void nodePropertyChanged( 162 Object nodeId, 163 String property, 164 Object oldValue, 165 Object newValue) { 166 } 168 } 169 | Popular Tags |