1 19 20 package org.apache.cayenne.access; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.apache.cayenne.ObjectId; 27 import org.apache.cayenne.access.DataDomainSyncBucket.PropagatedValueFactory; 28 import org.apache.cayenne.graph.GraphChangeHandler; 29 import org.apache.cayenne.graph.GraphDiff; 30 import org.apache.cayenne.map.Attribute; 31 import org.apache.cayenne.map.DbEntity; 32 import org.apache.cayenne.map.DbJoin; 33 import org.apache.cayenne.map.DbRelationship; 34 import org.apache.cayenne.map.ObjAttribute; 35 import org.apache.cayenne.map.ObjEntity; 36 import org.apache.cayenne.map.ObjRelationship; 37 38 44 class DataDomainDBDiffBuilder implements GraphChangeHandler { 45 46 private ObjEntity objEntity; 47 private DbEntity dbEntity; 48 49 private Map currentPropertyDiff; 51 private Map currentArcDiff; 52 private Object currentId; 53 54 57 void reset(ObjEntity objEntity, DbEntity dbEntity) { 58 this.objEntity = objEntity; 59 this.dbEntity = dbEntity; 60 } 61 62 66 private void reset() { 67 currentPropertyDiff = null; 68 currentArcDiff = null; 69 currentId = null; 70 } 71 72 75 Map buildDBDiff(GraphDiff singleObjectDiff) { 76 77 reset(); 78 singleObjectDiff.apply(this); 79 80 if (currentPropertyDiff == null && currentArcDiff == null && currentId == null) { 81 return null; 82 } 83 84 Map dbDiff = new HashMap (); 85 86 appendSimpleProperties(dbDiff); 87 appendForeignKeys(dbDiff); 88 appendPrimaryKeys(dbDiff); 89 90 return dbDiff.isEmpty() ? null : dbDiff; 91 } 92 93 private void appendSimpleProperties(Map dbDiff) { 94 if (currentPropertyDiff != null) { 96 Iterator it = currentPropertyDiff.entrySet().iterator(); 97 while (it.hasNext()) { 98 Map.Entry entry = (Map.Entry ) it.next(); 99 ObjAttribute attribute = (ObjAttribute) objEntity.getAttribute(entry 100 .getKey() 101 .toString()); 102 103 Attribute dbAttribute = dbEntity.getAttribute(attribute 106 .getDbAttributeName()); 107 dbDiff.put(dbAttribute.getName(), entry.getValue()); 108 } 109 } 110 } 111 112 private void appendForeignKeys(Map dbDiff) { 113 if (currentArcDiff != null) { 115 Iterator it = currentArcDiff.entrySet().iterator(); 116 while (it.hasNext()) { 117 Map.Entry entry = (Map.Entry ) it.next(); 118 ObjRelationship relation = (ObjRelationship) objEntity 119 .getRelationship(entry.getKey().toString()); 120 121 DbRelationship dbRelation = (DbRelationship) relation 122 .getDbRelationships() 123 .get(0); 124 125 ObjectId targetId = (ObjectId) entry.getValue(); 126 Iterator joins = dbRelation.getJoins().iterator(); 127 while (joins.hasNext()) { 128 DbJoin join = (DbJoin) joins.next(); 129 Object value = (targetId != null) ? new PropagatedValueFactory( 130 targetId, 131 join.getTargetName()) : null; 132 133 dbDiff.put(join.getSourceName(), value); 134 } 135 } 136 } 137 } 138 139 private void appendPrimaryKeys(Map dbDiff) { 140 141 if (currentId != null) { 144 dbDiff.putAll(((ObjectId) currentId).getIdSnapshot()); 145 } 146 } 147 148 152 public void nodePropertyChanged( 153 Object nodeId, 154 String property, 155 Object oldValue, 156 Object newValue) { 157 159 if (currentPropertyDiff == null) { 160 currentPropertyDiff = new HashMap (); 161 } 162 163 currentPropertyDiff.put(property, newValue); 164 } 165 166 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 167 168 ObjRelationship relationship = (ObjRelationship) objEntity.getRelationship(arcId 169 .toString()); 170 if (!relationship.isSourceIndependentFromTargetChange()) { 171 if (currentArcDiff == null) { 172 currentArcDiff = new HashMap (); 173 } 174 currentArcDiff.put(arcId, targetNodeId); 175 } 176 } 177 178 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 179 180 ObjRelationship relationship = (ObjRelationship) objEntity.getRelationship(arcId 181 .toString()); 182 if (!relationship.isSourceIndependentFromTargetChange()) { 183 184 if (currentArcDiff == null) { 185 currentArcDiff = new HashMap (); 186 currentArcDiff.put(arcId, null); 187 } 188 else if (targetNodeId.equals(currentArcDiff.get(arcId))) { 191 currentArcDiff.put(arcId, null); 192 } 193 } 194 } 195 196 public void nodeCreated(Object nodeId) { 197 this.currentId = nodeId; 199 } 200 201 public void nodeRemoved(Object nodeId) { 202 } 204 205 public void nodeIdChanged(Object nodeId, Object newId) { 206 } 208 } 209 | Popular Tags |