1 19 20 package org.apache.cayenne.util; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.cayenne.dba.TypesMapping; 28 import org.apache.cayenne.map.DataMap; 29 import org.apache.cayenne.map.DbAttribute; 30 import org.apache.cayenne.map.DbEntity; 31 import org.apache.cayenne.map.DbJoin; 32 import org.apache.cayenne.map.DbRelationship; 33 import org.apache.cayenne.map.Entity; 34 import org.apache.cayenne.map.ObjAttribute; 35 import org.apache.cayenne.map.ObjEntity; 36 import org.apache.cayenne.map.ObjRelationship; 37 import org.apache.cayenne.project.NamedObjectFactory; 38 39 44 public class EntityMergeSupport { 45 46 protected DataMap map; 47 protected boolean removeMeaningfulFKs; 48 49 public EntityMergeSupport(DataMap map) { 50 this.map = map; 51 this.removeMeaningfulFKs = true; 52 } 53 54 60 public boolean synchronizeWithDbEntities(Collection objEntities) { 61 boolean changed = false; 62 Iterator it = objEntities.iterator(); 63 while (it.hasNext()) { 64 if (synchronizeWithDbEntity((ObjEntity) it.next())) { 65 changed = true; 66 } 67 } 68 69 return changed; 70 } 71 72 78 public boolean synchronizeWithDbEntity(ObjEntity entity) { 79 80 if (entity == null || entity.getDbEntity() == null) { 81 return false; 82 } 83 84 boolean changed = false; 85 86 synchronized (map) { 90 91 if (removeMeaningfulFKs) { 92 93 Iterator rait = getMeaningfulFKs(entity).iterator(); 95 while (rait.hasNext()) { 96 DbAttribute da = (DbAttribute) rait.next(); 97 ObjAttribute oa = entity.getAttributeForDbAttribute(da); 98 while (oa != null) { 99 String attrName = oa.getName(); 100 entity.removeAttribute(attrName); 101 changed = true; 102 oa = entity.getAttributeForDbAttribute(da); 103 } 104 } 105 } 106 107 List addAttributes = getAttributesToAdd(entity); 108 109 Iterator ait = addAttributes.iterator(); 111 while (ait.hasNext()) { 112 DbAttribute da = (DbAttribute) ait.next(); 113 String attrName = NameConverter.underscoredToJava(da.getName(), false); 114 115 attrName = NamedObjectFactory.createName( 117 ObjAttribute.class, 118 entity, 119 attrName); 120 121 String type = TypesMapping.getJavaBySqlType(da.getType()); 122 123 ObjAttribute oa = new ObjAttribute(attrName, type, entity); 124 oa.setDbAttribute(da); 125 entity.addAttribute(oa); 126 changed = true; 127 } 128 129 List addRelationships = getRelationshipsToAdd(entity); 130 131 Iterator rit = addRelationships.iterator(); 133 while (rit.hasNext()) { 134 DbRelationship dr = (DbRelationship) rit.next(); 135 DbEntity dbEntity = (DbEntity) dr.getTargetEntity(); 136 137 Iterator targets = map.getMappedEntities(dbEntity).iterator(); 138 if (targets.hasNext()) { 139 140 Entity mappedTarget = (Entity) targets.next(); 141 142 String relationshipName = NameConverter.underscoredToJava(dr 144 .getName(), false); 145 relationshipName = NamedObjectFactory.createName( 146 ObjRelationship.class, 147 entity, 148 relationshipName); 149 150 ObjRelationship or = new ObjRelationship(relationshipName); 151 or.addDbRelationship(dr); 152 or.setSourceEntity(entity); 153 or.setTargetEntity(mappedTarget); 154 entity.addRelationship(or); 155 changed = true; 156 } 157 } 158 } 159 160 return changed; 161 } 162 163 168 public Collection getMeaningfulFKs(ObjEntity objEntity) { 169 List fks = new ArrayList (2); 170 Iterator it = objEntity.getAttributes().iterator(); 171 while (it.hasNext()) { 172 ObjAttribute property = (ObjAttribute) it.next(); 173 DbAttribute column = property.getDbAttribute(); 174 175 if (column != null && column.isForeignKey()) { 177 fks.add(column); 178 } 179 } 180 181 return fks; 182 } 183 184 188 protected List getAttributesToAdd(ObjEntity objEntity) { 189 List missing = new ArrayList (); 190 Iterator it = objEntity.getDbEntity().getAttributes().iterator(); 191 Collection rels = objEntity.getDbEntity().getRelationships(); 192 193 while (it.hasNext()) { 194 DbAttribute dba = (DbAttribute) it.next(); 195 if (objEntity.getAttributeForDbAttribute(dba) != null) { 197 continue; 198 } 199 200 if (dba.getName() == null || dba.isPrimaryKey()) { 202 continue; 203 } 204 205 boolean isFK = false; 207 Iterator rit = rels.iterator(); 208 while (!isFK && rit.hasNext()) { 209 DbRelationship rel = (DbRelationship) rit.next(); 210 Iterator jit = rel.getJoins().iterator(); 211 while (jit.hasNext()) { 212 DbJoin join = (DbJoin) jit.next(); 213 if (join.getSource() == dba) { 214 isFK = true; 215 break; 216 } 217 } 218 } 219 220 if (isFK) { 221 continue; 222 } 223 224 missing.add(dba); 225 } 226 227 return missing; 228 } 229 230 protected List getRelationshipsToAdd(ObjEntity objEntity) { 231 List missing = new ArrayList (); 232 Iterator it = objEntity.getDbEntity().getRelationships().iterator(); 233 while (it.hasNext()) { 234 DbRelationship dbrel = (DbRelationship) it.next(); 235 if (dbrel.getName() == null) { 237 continue; 238 } 239 240 if (objEntity.getRelationshipForDbRelationship(dbrel) == null) { 241 missing.add(dbrel); 242 } 243 } 244 245 return missing; 246 } 247 248 public DataMap getMap() { 249 return map; 250 } 251 252 public void setMap(DataMap map) { 253 this.map = map; 254 } 255 256 259 public boolean isRemoveMeaningfulFKs() { 260 return removeMeaningfulFKs; 261 } 262 263 266 public void setRemoveMeaningfulFKs(boolean removeMeaningfulFKs) { 267 this.removeMeaningfulFKs = removeMeaningfulFKs; 268 } 269 } 270 | Popular Tags |