1 56 package org.objectstyle.cayenne.util; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 import org.objectstyle.cayenne.dba.TypesMapping; 64 import org.objectstyle.cayenne.map.DataMap; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.map.DbEntity; 67 import org.objectstyle.cayenne.map.DbJoin; 68 import org.objectstyle.cayenne.map.DbRelationship; 69 import org.objectstyle.cayenne.map.Entity; 70 import org.objectstyle.cayenne.map.ObjAttribute; 71 import org.objectstyle.cayenne.map.ObjEntity; 72 import org.objectstyle.cayenne.map.ObjRelationship; 73 import org.objectstyle.cayenne.project.NamedObjectFactory; 74 75 80 public class EntityMergeSupport { 81 82 protected DataMap map; 83 84 public EntityMergeSupport(DataMap map) { 85 this.map = map; 86 } 87 88 89 95 public void synchronizeWithDbEntities(Collection objEntities) { 96 Iterator it = objEntities.iterator(); 97 while (it.hasNext()) { 98 this.synchronizeWithDbEntity((ObjEntity) it.next()); 99 } 100 } 101 102 106 public void synchronizeWithDbEntity(ObjEntity entity) { 107 108 if (entity == null || entity.getDbEntity() == null) { 109 return; 110 } 111 112 synchronized (map) { 116 List removeAttributes = getAttributesToRemove(entity); 117 118 Iterator rait = removeAttributes.iterator(); 120 while (rait.hasNext()) { 121 DbAttribute da = (DbAttribute) rait.next(); 122 ObjAttribute oa = entity.getAttributeForDbAttribute(da); 123 while (oa != null){ 124 String attrName = oa.getName(); 125 entity.removeAttribute(attrName); 126 oa = entity.getAttributeForDbAttribute(da); 127 } 128 } 129 130 List addAttributes = getAttributesToAdd(entity); 131 132 Iterator ait = addAttributes.iterator(); 134 while (ait.hasNext()) { 135 DbAttribute da = (DbAttribute) ait.next(); 136 String attrName = NameConverter.undescoredToJava(da.getName(), false); 137 138 attrName = NamedObjectFactory.createName( 140 ObjAttribute.class, 141 entity, 142 attrName); 143 144 String type = TypesMapping.getJavaBySqlType(da.getType()); 145 146 ObjAttribute oa = new ObjAttribute(attrName, type, entity); 147 oa.setDbAttribute(da); 148 entity.addAttribute(oa); 149 } 150 151 List addRelationships = getRelationshipsToAdd(entity); 152 153 Iterator rit = addRelationships.iterator(); 155 while (rit.hasNext()) { 156 DbRelationship dr = (DbRelationship) rit.next(); 157 DbEntity dbEntity = (DbEntity) dr.getTargetEntity(); 158 159 Iterator targets = map.getMappedEntities(dbEntity).iterator(); 160 if (targets.hasNext()) { 161 162 Entity mappedTarget = (Entity) targets.next(); 163 164 String relationshipName = NameConverter.undescoredToJava(dr.getName(), false); 166 relationshipName = NamedObjectFactory.createName( 167 ObjRelationship.class, 168 entity, 169 relationshipName); 170 171 ObjRelationship or = new ObjRelationship(relationshipName); 172 or.addDbRelationship(dr); 173 or.setSourceEntity(entity); 174 or.setTargetEntity(mappedTarget); 175 entity.addRelationship(or); 176 } 177 } 178 } 179 } 180 181 187 protected List getAttributesToRemove(ObjEntity objEntity){ 188 List removeList = new ArrayList (); 189 Iterator it = objEntity.getDbEntity().getRelationships().iterator(); 190 while (it.hasNext()) { 191 DbRelationship dbrel = (DbRelationship) it.next(); 192 193 if (dbrel.getName() == null) { 195 continue; 196 } 197 198 Iterator srcAttIterator = dbrel.getSourceAttributes().iterator(); 200 while(srcAttIterator.hasNext()){ 201 removeList.add(srcAttIterator.next()); 202 } 203 } 204 205 return removeList; 206 } 207 208 212 protected List getAttributesToAdd(ObjEntity objEntity) { 213 List missing = new ArrayList (); 214 Iterator it = objEntity.getDbEntity().getAttributes().iterator(); 215 Collection rels = objEntity.getDbEntity().getRelationships(); 216 217 while (it.hasNext()) { 218 DbAttribute dba = (DbAttribute) it.next(); 219 if (objEntity.getAttributeForDbAttribute(dba) != null) { 221 continue; 222 } 223 224 if (dba.getName() == null || dba.isPrimaryKey()) { 226 continue; 227 } 228 229 boolean isFK = false; 231 Iterator rit = rels.iterator(); 232 while (!isFK && rit.hasNext()) { 233 DbRelationship rel = (DbRelationship) rit.next(); 234 Iterator jit = rel.getJoins().iterator(); 235 while (jit.hasNext()) { 236 DbJoin join = (DbJoin) jit.next(); 237 if (join.getSource() == dba) { 238 isFK = true; 239 break; 240 } 241 } 242 } 243 244 if (isFK) { 245 continue; 246 } 247 248 missing.add(dba); 249 } 250 251 return missing; 252 } 253 254 protected List getRelationshipsToAdd(ObjEntity objEntity) { 255 List missing = new ArrayList (); 256 Iterator it = objEntity.getDbEntity().getRelationships().iterator(); 257 while (it.hasNext()) { 258 DbRelationship dbrel = (DbRelationship) it.next(); 259 if (dbrel.getName() == null) { 261 continue; 262 } 263 264 if (objEntity.getRelationshipForDbRelationship(dbrel) == null) { 265 missing.add(dbrel); 266 } 267 } 268 269 return missing; 270 } 271 272 public DataMap getMap() { 273 return map; 274 } 275 276 public void setMap(DataMap map) { 277 this.map = map; 278 } 279 } | Popular Tags |