1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.access.DataDomain; 65 import org.objectstyle.cayenne.access.DataNode; 66 import org.objectstyle.cayenne.conf.Configuration; 67 import org.objectstyle.cayenne.map.Attribute; 68 import org.objectstyle.cayenne.map.DataMap; 69 import org.objectstyle.cayenne.map.DbAttribute; 70 import org.objectstyle.cayenne.map.DbEntity; 71 import org.objectstyle.cayenne.map.DbJoin; 72 import org.objectstyle.cayenne.map.DbRelationship; 73 import org.objectstyle.cayenne.map.Entity; 74 import org.objectstyle.cayenne.map.EntityResolver; 75 import org.objectstyle.cayenne.map.MappingNamespace; 76 import org.objectstyle.cayenne.map.ObjAttribute; 77 import org.objectstyle.cayenne.map.ObjEntity; 78 import org.objectstyle.cayenne.map.ObjRelationship; 79 import org.objectstyle.cayenne.map.Procedure; 80 import org.objectstyle.cayenne.map.ProcedureParameter; 81 import org.objectstyle.cayenne.map.Relationship; 82 import org.objectstyle.cayenne.query.Query; 83 import org.objectstyle.cayenne.util.Util; 84 85 88 public class ProjectUtil { 89 90 public static void setProcedureParameterName( 91 ProcedureParameter parameter, 92 String newName) { 93 94 String oldName = parameter.getName(); 95 96 if (Util.nullSafeEquals(oldName, newName)) { 98 return; 99 } 100 101 Procedure procedure = parameter.getEntity(); 102 procedure.removeCallParameter(parameter.getName()); 103 parameter.setName(newName); 104 procedure.addCallParameter(parameter); 105 } 106 107 public static void setDataMapName(DataDomain domain, DataMap map, String newName) { 108 String oldName = map.getName(); 109 110 if (Util.nullSafeEquals(oldName, newName)) { 112 return; 113 } 114 115 List nodes = new ArrayList (); 117 Iterator allNodes = domain.getDataNodes().iterator(); 118 while (allNodes.hasNext()) { 119 DataNode node = (DataNode) allNodes.next(); 120 if (node.getDataMaps().contains(map)) { 121 nodes.add(node); 122 } 123 } 124 125 domain.removeMap(oldName); 126 map.setName(newName); 127 domain.addMap(map); 128 129 Iterator relinkNodes = nodes.iterator(); 130 while (relinkNodes.hasNext()) { 131 DataNode node = (DataNode) relinkNodes.next(); 132 node.removeDataMap(oldName); 133 node.addDataMap(map); 134 } 135 } 136 137 public static void setDataDomainName( 138 Configuration configuration, 139 DataDomain domain, 140 String newName) { 141 142 String oldName = domain.getName(); 143 if (Util.nullSafeEquals(oldName, newName)) { 145 return; 146 } 147 148 domain.setName(newName); 149 configuration.removeDomain(oldName); 150 configuration.addDomain(domain); 151 } 152 153 public static void setDataNodeName(DataDomain domain, DataNode node, String newName) { 154 String oldName = node.getName(); 155 node.setName(newName); 156 domain.removeDataNode(oldName); 157 domain.addNode(node); 158 } 159 160 public static void setProcedureName(DataMap map, Procedure procedure, String newName) { 161 162 String oldName = procedure.getName(); 163 164 if (Util.nullSafeEquals(oldName, newName)) { 166 return; 167 } 168 169 procedure.setName(newName); 170 map.removeProcedure(oldName); 171 map.addProcedure(procedure); 172 173 MappingNamespace ns = map.getNamespace(); 175 if (ns instanceof EntityResolver) { 176 ((EntityResolver) ns).clearCache(); 177 } 178 } 179 180 public static void setQueryName(DataMap map, Query query, String newName) { 181 182 String oldName = query.getName(); 183 184 if (Util.nullSafeEquals(oldName, newName)) { 186 return; 187 } 188 189 query.setName(newName); 190 map.removeQuery(oldName); 191 map.addQuery(query); 192 193 MappingNamespace ns = map.getNamespace(); 195 if (ns instanceof EntityResolver) { 196 ((EntityResolver) ns).clearCache(); 197 } 198 } 199 200 public static void setObjEntityName(DataMap map, ObjEntity entity, String newName) { 201 String oldName = entity.getName(); 202 203 if (Util.nullSafeEquals(oldName, newName)) { 205 return; 206 } 207 208 entity.setName(newName); 209 map.removeObjEntity(oldName, false); 210 map.addObjEntity(entity); 211 212 MappingNamespace ns = map.getNamespace(); 214 if (ns instanceof EntityResolver) { 215 ((EntityResolver) ns).clearCache(); 216 } 217 } 218 219 222 public static void setDbEntityName(DbEntity entity, String newName) { 223 String oldName = entity.getName(); 224 225 if (Util.nullSafeEquals(oldName, newName)) { 227 return; 228 } 229 230 entity.setName(newName); 231 DataMap map = entity.getDataMap(); 232 233 if (map != null) { 234 map.removeDbEntity(oldName, false); 235 map.addDbEntity(entity); 236 237 MappingNamespace ns = map.getNamespace(); 239 if (ns instanceof EntityResolver) { 240 ((EntityResolver) ns).clearCache(); 241 } 242 } 243 } 244 245 248 public static void setAttributeName(Attribute attribute, String newName) { 249 String oldName = attribute.getName(); 250 251 attribute.setName(newName); 252 253 Entity entity = attribute.getEntity(); 254 255 if (entity != null) { 256 entity.removeAttribute(oldName); 257 entity.addAttribute(attribute); 258 } 259 } 260 261 262 public static void setRelationshipName(Entity entity, Relationship rel, String newName) { 263 264 if (rel == null || rel != entity.getRelationship(rel.getName())) { 265 return; 266 } 267 268 entity.removeRelationship(rel.getName()); 269 rel.setName(newName); 270 entity.addRelationship(rel); 271 } 272 273 277 public static void cleanObjMappings(DataMap map) { 278 Iterator ents = map.getObjEntities().iterator(); 279 while (ents.hasNext()) { 280 ObjEntity ent = (ObjEntity) ents.next(); 281 DbEntity dbEnt = ent.getDbEntity(); 282 283 if (dbEnt != null && map.getDbEntity(dbEnt.getName()) != dbEnt) { 285 clearDbMapping(ent); 286 continue; 287 } 288 289 Iterator atts = ent.getAttributes().iterator(); 291 while (atts.hasNext()) { 292 ObjAttribute att = (ObjAttribute) atts.next(); 293 DbAttribute dbAtt = att.getDbAttribute(); 294 if (dbAtt != null) { 295 if (dbEnt.getAttribute(dbAtt.getName()) != dbAtt) { 296 att.setDbAttribute(null); 297 } 298 } 299 } 300 301 Iterator rels = ent.getRelationships().iterator(); 303 while (rels.hasNext()) { 304 ObjRelationship rel = (ObjRelationship) rels.next(); 305 306 Iterator dbRels = new ArrayList (rel.getDbRelationships()).iterator(); 307 while (dbRels.hasNext()) { 308 DbRelationship dbRel = (DbRelationship) dbRels.next(); 309 Entity srcEnt = dbRel.getSourceEntity(); 310 if (srcEnt == null 311 || map.getDbEntity(srcEnt.getName()) != srcEnt 312 || srcEnt.getRelationship(dbRel.getName()) != dbRel) { 313 rel.removeDbRelationship(dbRel); 314 } 315 } 316 } 317 318 } 319 } 320 321 325 public static void clearDbMapping(ObjEntity entity) { 326 DbEntity db_entity = entity.getDbEntity(); 327 if (db_entity == null) { 328 return; 329 } 330 331 Iterator it = entity.getAttributeMap().values().iterator(); 332 while (it.hasNext()) { 333 ObjAttribute objAttr = (ObjAttribute) it.next(); 334 DbAttribute dbAttr = objAttr.getDbAttribute(); 335 if (null != dbAttr) { 336 objAttr.setDbAttribute(null); 337 } 338 } 339 340 Iterator rel_it = entity.getRelationships().iterator(); 341 while (rel_it.hasNext()) { 342 ObjRelationship obj_rel = (ObjRelationship) rel_it.next(); 343 obj_rel.clearDbRelationships(); 344 } 345 entity.setDbEntity(null); 346 } 347 348 351 public static boolean containsSourceAttribute( 352 DbRelationship relationship, 353 DbAttribute attribute) { 354 if (attribute.getEntity() != relationship.getSourceEntity()) { 355 return false; 356 } 357 358 Iterator it = relationship.getJoins().iterator(); 359 while (it.hasNext()) { 360 DbJoin join = (DbJoin) it.next(); 361 if (join.getSource() == attribute) { 362 return true; 363 } 364 } 365 366 return false; 367 } 368 369 372 public static boolean containsTargetAttribute( 373 DbRelationship relationship, 374 DbAttribute attribute) { 375 if (attribute.getEntity() != relationship.getTargetEntity()) { 376 return false; 377 } 378 379 Iterator it = relationship.getJoins().iterator(); 380 while (it.hasNext()) { 381 DbJoin join = (DbJoin) it.next(); 382 if (join.getTarget() == attribute) { 383 return true; 384 } 385 } 386 387 return false; 388 } 389 390 393 public static Collection getRelationshipsUsingAttributeAsSource(DbAttribute attribute) { 394 Entity parent = attribute.getEntity(); 395 396 if (parent == null) { 397 return Collections.EMPTY_LIST; 398 } 399 400 Collection parentRelationships = parent.getRelationships(); 401 Collection relationships = new ArrayList (parentRelationships.size()); 402 Iterator it = parentRelationships.iterator(); 403 while (it.hasNext()) { 404 DbRelationship relationship = (DbRelationship) it.next(); 405 if (ProjectUtil.containsSourceAttribute(relationship, attribute)) { 406 relationships.add(relationship); 407 } 408 } 409 return relationships; 410 } 411 412 415 public static Collection getRelationshipsUsingAttributeAsTarget(DbAttribute attribute) { 416 Entity parent = attribute.getEntity(); 417 418 if (parent == null) { 419 return Collections.EMPTY_LIST; 420 } 421 422 DataMap map = parent.getDataMap(); 423 if (map == null) { 424 return Collections.EMPTY_LIST; 425 } 426 427 Collection relationships = new ArrayList (); 428 429 Iterator it = map.getDbEntities().iterator(); 430 while (it.hasNext()) { 431 Entity entity = (Entity) it.next(); 432 if (entity == parent) { 433 continue; 434 } 435 436 Collection entityRelationships = entity.getRelationships(); 437 Iterator relationshipsIt = entityRelationships.iterator(); 438 while (relationshipsIt.hasNext()) { 439 DbRelationship relationship = (DbRelationship) relationshipsIt.next(); 440 if (ProjectUtil.containsTargetAttribute(relationship, attribute)) { 441 relationships.add(relationship); 442 } 443 } 444 } 445 return relationships; 446 } 447 } | Popular Tags |