1 19 20 package org.apache.cayenne.access; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.collections.Transformer; 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.ObjectId; 31 import org.apache.cayenne.map.DbAttribute; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.map.DbJoin; 34 import org.apache.cayenne.map.DbRelationship; 35 import org.apache.cayenne.map.ObjAttribute; 36 import org.apache.cayenne.map.ObjEntity; 37 import org.apache.cayenne.map.ObjRelationship; 38 39 45 class DataNodeSyncQualifierDescriptor { 46 47 private List attributes; 48 private List valueTransformers; 49 private boolean usingOptimisticLocking; 50 51 public boolean isUsingOptimisticLocking() { 52 return usingOptimisticLocking; 53 } 54 55 List getAttributes() { 56 return attributes; 57 } 58 59 Map createQualifierSnapshot(ObjectDiff diff) { 60 int len = attributes.size(); 61 62 Map map = new HashMap (len * 2); 63 for (int i = 0; i < len; i++) { 64 DbAttribute attribute = (DbAttribute) attributes.get(i); 65 if (!map.containsKey(attribute.getName())) { 66 67 Object value = ((Transformer) valueTransformers.get(i)).transform(diff); 68 map.put(attribute.getName(), value); 69 } 70 } 71 72 return map; 73 } 74 75 void reset(ObjEntity entity, DbEntity dbEntity) { 76 attributes = new ArrayList (3); 77 valueTransformers = new ArrayList (3); 78 usingOptimisticLocking = entity.getLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC; 79 80 if (entity.getDbEntity() == dbEntity) { 82 Iterator pkIt = entity.getDbEntity().getPrimaryKey().iterator(); 83 while (pkIt.hasNext()) { 84 final DbAttribute attribute = (DbAttribute) pkIt.next(); 85 attributes.add(attribute); 86 valueTransformers.add(new Transformer() { 87 88 public Object transform(Object input) { 89 ObjectId id = (ObjectId) ((ObjectDiff) input).getNodeId(); 90 return id.getIdSnapshot().get(attribute.getName()); 91 } 92 }); 93 } 94 } 95 else { 96 DbRelationship masterDependentDbRel = findMasterToDependentDbRelationship( 98 entity.getDbEntity(), 99 dbEntity); 100 101 if (masterDependentDbRel != null) { 102 103 Iterator joinsIterator = masterDependentDbRel.getJoins().iterator(); 104 while (joinsIterator.hasNext()) { 105 final DbJoin dbAttrPair = (DbJoin) joinsIterator.next(); 106 DbAttribute dbAttribute = dbAttrPair.getTarget(); 107 if (!attributes.contains(dbAttribute)) { 108 109 attributes.add(dbAttribute); 110 valueTransformers.add(new Transformer() { 111 112 public Object transform(Object input) { 113 ObjectId id = (ObjectId) ((ObjectDiff) input).getNodeId(); 114 return id.getIdSnapshot().get(dbAttrPair.getSourceName()); 115 } 116 }); 117 } 118 } 119 } 120 } 121 122 if (usingOptimisticLocking) { 123 124 Iterator attributeIt = entity.getAttributes().iterator(); 125 while (attributeIt.hasNext()) { 126 final ObjAttribute attribute = (ObjAttribute) attributeIt.next(); 127 128 if (attribute.isUsedForLocking()) { 129 DbAttribute dbAttribute = (DbAttribute) attribute 131 .getDbPathIterator() 132 .next(); 133 134 if (!attributes.contains(dbAttribute)) { 135 attributes.add(dbAttribute); 136 137 valueTransformers.add(new Transformer() { 138 139 public Object transform(Object input) { 140 return ((ObjectDiff) input).getSnapshotValue(attribute 141 .getName()); 142 } 143 }); 144 } 145 } 146 } 147 148 Iterator relationshipIt = entity.getRelationships().iterator(); 149 while (relationshipIt.hasNext()) { 150 final ObjRelationship relationship = (ObjRelationship) relationshipIt 151 .next(); 152 153 if (relationship.isUsedForLocking()) { 154 DbRelationship dbRelationship = (DbRelationship) relationship 156 .getDbRelationships() 157 .get(0); 158 159 Iterator joinsIterator = dbRelationship.getJoins().iterator(); 160 while (joinsIterator.hasNext()) { 161 final DbJoin dbAttrPair = (DbJoin) joinsIterator.next(); 162 DbAttribute dbAttribute = dbAttrPair.getSource(); 163 164 int index = attributes.indexOf(dbAttribute); 168 if (index >= 0 && !dbAttribute.isForeignKey()) { 169 continue; 170 } 171 172 Object transformer = new Transformer() { 173 174 public Object transform(Object input) { 175 ObjectId targetId = ((ObjectDiff) input) 176 .getArcSnapshotValue(relationship.getName()); 177 return targetId != null ? targetId.getIdSnapshot().get( 178 dbAttrPair.getTargetName()) : null; 179 } 180 }; 181 182 if (index < 0) { 183 attributes.add(dbAttribute); 184 valueTransformers.add(transformer); 185 } 186 else { 187 valueTransformers.set(index, transformer); 188 } 189 } 190 } 191 } 192 } 193 } 194 195 private DbRelationship findMasterToDependentDbRelationship( 196 DbEntity masterDbEntity, 197 DbEntity dependentDbEntity) { 198 199 Iterator it = masterDbEntity.getRelationshipMap().values().iterator(); 200 while (it.hasNext()) { 201 202 DbRelationship relationship = (DbRelationship) it.next(); 203 if (dependentDbEntity.equals(relationship.getTargetEntity()) 204 && relationship.isToDependentPK()) { 205 206 if (relationship.isToMany()) { 207 throw new CayenneRuntimeException( 208 "Only 'to one' master-detail relationships can be processed."); 209 } 210 211 return relationship; 212 } 213 } 214 215 return null; 216 } 217 } 218 | Popular Tags |