1 56 package org.objectstyle.cayenne.project; 57 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 import java.util.Map ; 61 62 import org.objectstyle.cayenne.access.DataDomain; 63 import org.objectstyle.cayenne.access.DataNode; 64 import org.objectstyle.cayenne.conf.Configuration; 65 import org.objectstyle.cayenne.dba.TypesMapping; 66 import org.objectstyle.cayenne.map.DataMap; 67 import org.objectstyle.cayenne.map.DbAttribute; 68 import org.objectstyle.cayenne.map.DbEntity; 69 import org.objectstyle.cayenne.map.DbRelationship; 70 import org.objectstyle.cayenne.map.DerivedDbAttribute; 71 import org.objectstyle.cayenne.map.DerivedDbEntity; 72 import org.objectstyle.cayenne.map.Entity; 73 import org.objectstyle.cayenne.map.ObjAttribute; 74 import org.objectstyle.cayenne.map.ObjEntity; 75 import org.objectstyle.cayenne.map.ObjRelationship; 76 import org.objectstyle.cayenne.map.Procedure; 77 import org.objectstyle.cayenne.map.ProcedureParameter; 78 import org.objectstyle.cayenne.map.Relationship; 79 import org.objectstyle.cayenne.query.Query; 80 import org.objectstyle.cayenne.query.SelectQuery; 81 82 109 public abstract class NamedObjectFactory { 110 private static final Map factories = new HashMap (); 111 112 static { 113 factories.put(DataMap.class, new DataMapFactory()); 114 factories.put(ObjEntity.class, new ObjEntityFactory()); 115 factories.put(DbEntity.class, new DbEntityFactory()); 116 factories.put(DerivedDbEntity.class, new DerivedDbEntityFactory()); 117 factories.put(ObjAttribute.class, new ObjAttributeFactory()); 118 factories.put(DbAttribute.class, new DbAttributeFactory()); 119 factories.put(DerivedDbAttribute.class, new DerivedDbAttributeFactory()); 120 factories.put(DataNode.class, new DataNodeFactory()); 121 factories.put(DbRelationship.class, new DbRelationshipFactory(null, false)); 122 factories.put(ObjRelationship.class, new ObjRelationshipFactory(null, false)); 123 factories.put(DataDomain.class, new DataDomainFactory()); 124 factories.put(Procedure.class, new ProcedureFactory()); 125 factories.put(Query.class, new SelectQueryFactory()); 126 factories.put(ProcedureParameter.class, new ProcedureParameterFactory()); 127 } 128 129 public static String createName(Class objectClass, Object namingContext) { 130 return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext); 131 } 132 133 136 public static String createName(Class objectClass, Object namingContext, String nameBase) { 137 return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext, nameBase); 138 } 139 140 148 public static Object createObject(Class objectClass, Object namingContext) { 149 return ((NamedObjectFactory) factories.get(objectClass)).makeObject( 150 namingContext); 151 } 152 153 156 public static Object createObject( 157 Class objectClass, 158 Object namingContext, 159 String nameBase) { 160 return ((NamedObjectFactory) factories.get(objectClass)).makeObject( 161 namingContext, 162 nameBase); 163 } 164 165 173 public static Relationship createRelationship( 174 Entity srcEnt, 175 Entity targetEnt, 176 boolean toMany) { 177 NamedObjectFactory factory = 178 (srcEnt instanceof ObjEntity) 179 ? new ObjRelationshipFactory(targetEnt, toMany) 180 : new DbRelationshipFactory(targetEnt, toMany); 181 return (Relationship) factory.makeObject(srcEnt); 182 } 183 184 188 protected synchronized String makeName(Object namingContext) { 189 return makeName(namingContext, nameBase()); 190 } 191 192 195 protected synchronized String makeName(Object namingContext, String nameBase) { 196 int c = 1; 197 String name = nameBase; 198 while (isNameInUse(name, namingContext)) { 199 name = nameBase + c++; 200 } 201 202 return name; 203 } 204 205 208 protected Object makeObject(Object namingContext) { 209 return makeObject(namingContext, nameBase()); 210 } 211 212 215 protected Object makeObject(Object namingContext, String nameBase) { 216 return create(makeName(namingContext, nameBase), namingContext); 217 } 218 219 220 protected abstract String nameBase(); 221 222 223 protected abstract Object create(String name, Object namingContext); 224 225 229 protected abstract boolean isNameInUse(String name, Object namingContext); 230 231 static class DataDomainFactory extends NamedObjectFactory { 233 protected String nameBase() { 234 return "UntitledDomain"; 235 } 236 237 protected Object create(String name, Object namingContext) { 238 return new DataDomain(name); 239 } 240 241 protected boolean isNameInUse(String name, Object namingContext) { 242 Configuration config = (Configuration) namingContext; 243 return config.getDomain(name) != null; 244 } 245 } 246 247 static class DataMapFactory extends NamedObjectFactory { 248 protected String nameBase() { 249 return "UntitledMap"; 250 } 251 252 protected Object create(String name, Object namingContext) { 253 return new DataMap(name); 254 } 255 256 protected boolean isNameInUse(String name, Object namingContext) { 257 if (namingContext == null) { 260 return false; 261 } 262 263 DataDomain domain = (DataDomain) namingContext; 264 return domain.getMap(name) != null; 265 } 266 } 267 268 static class ObjEntityFactory extends NamedObjectFactory { 269 protected String nameBase() { 270 return "UntitledObjEntity"; 271 } 272 273 protected Object create(String name, Object namingContext) { 274 return new ObjEntity(name); 275 } 276 277 protected boolean isNameInUse(String name, Object namingContext) { 278 DataMap map = (DataMap) namingContext; 279 return map.getObjEntity(name) != null; 280 } 281 } 282 283 static class DbEntityFactory extends NamedObjectFactory { 284 protected String nameBase() { 285 return "UntitledDbEntity"; 286 } 287 288 protected Object create(String name, Object namingContext) { 289 return new DbEntity(name); 290 } 291 292 protected boolean isNameInUse(String name, Object namingContext) { 293 DataMap map = (DataMap) namingContext; 294 return map.getDbEntity(name) != null; 295 } 296 } 297 298 static class ProcedureParameterFactory extends NamedObjectFactory { 299 protected String nameBase() { 300 return "UntitledProcedureParameter"; 301 } 302 303 protected Object create(String name, Object namingContext) { 304 return new ProcedureParameter(name); 305 } 306 307 protected boolean isNameInUse(String name, Object namingContext) { 308 309 Procedure procedure = (Procedure) namingContext; 313 Iterator it = procedure.getCallParameters().iterator(); 314 while (it.hasNext()) { 315 ProcedureParameter parameter = (ProcedureParameter) it.next(); 316 if (name.equals(parameter.getName())) { 317 return true; 318 } 319 } 320 321 return false; 322 } 323 } 324 325 static class ProcedureFactory extends NamedObjectFactory { 326 protected String nameBase() { 327 return "UntitledProcedure"; 328 } 329 330 protected Object create(String name, Object namingContext) { 331 return new Procedure(name); 332 } 333 334 protected boolean isNameInUse(String name, Object namingContext) { 335 DataMap map = (DataMap) namingContext; 336 return map.getProcedure(name) != null; 337 } 338 } 339 340 static class SelectQueryFactory extends NamedObjectFactory { 341 protected String nameBase() { 342 return "UntitledQuery"; 343 } 344 345 protected Object create(String name, Object namingContext) { 346 SelectQuery query = new SelectQuery(); 347 query.setName(name); 348 return query; 349 } 350 351 protected boolean isNameInUse(String name, Object namingContext) { 352 DataMap map = (DataMap) namingContext; 353 return map.getQuery(name) != null; 354 } 355 } 356 357 static class DerivedDbEntityFactory extends DbEntityFactory { 358 protected Object create(String name, Object namingContext) { 359 return new DerivedDbEntity(name); 360 } 361 } 362 363 static class ObjAttributeFactory extends NamedObjectFactory { 364 protected String nameBase() { 365 return "untitledAttr"; 366 } 367 368 protected Object create(String name, Object namingContext) { 369 return new ObjAttribute(name, null, (ObjEntity) namingContext); 370 } 371 372 protected boolean isNameInUse(String name, Object namingContext) { 373 Entity ent = (Entity) namingContext; 374 return ent.getAttribute(name) != null; 375 } 376 } 377 378 static class DbAttributeFactory extends ObjAttributeFactory { 379 protected Object create(String name, Object namingContext) { 380 return new DbAttribute( 381 name, 382 TypesMapping.NOT_DEFINED, 383 (DbEntity) namingContext); 384 } 385 } 386 387 static class DerivedDbAttributeFactory extends ObjAttributeFactory { 388 protected Object create(String name, Object namingContext) { 389 return new DerivedDbAttribute( 390 name, 391 TypesMapping.NOT_DEFINED, 392 (DbEntity) namingContext, 393 DerivedDbAttribute.ATTRIBUTE_TOKEN); 394 } 395 } 396 397 static class DataNodeFactory extends NamedObjectFactory { 398 protected String nameBase() { 399 return "UntitledDataNode"; 400 } 401 402 protected Object create(String name, Object namingContext) { 403 return new DataNode(name); 404 } 405 406 protected boolean isNameInUse(String name, Object namingContext) { 407 DataDomain domain = (DataDomain) namingContext; 408 return domain.getNode(name) != null; 409 } 410 } 411 412 static class ObjRelationshipFactory extends NamedObjectFactory { 413 protected Entity target; 414 protected boolean toMany; 415 416 public ObjRelationshipFactory(Entity target, boolean toMany) { 417 this.target = target; 418 this.toMany = toMany; 419 } 420 421 protected Object create(String name, Object namingContext) { 422 return new ObjRelationship(name); 423 } 424 425 protected boolean isNameInUse(String name, Object namingContext) { 426 Entity ent = (Entity) namingContext; 427 return ent.getRelationship(name) != null; 428 } 429 430 435 protected String nameBase() { 436 if (target == null) { 437 return "untitledRel"; 438 } 439 440 String name = target.getName(); 441 return (toMany) 442 ? Character.toLowerCase(name.charAt(0)) + name.substring(1) + "Array" 443 : "to" + Character.toUpperCase(name.charAt(0)) + name.substring(1); 444 } 445 } 446 447 static class DbRelationshipFactory extends ObjRelationshipFactory { 448 public DbRelationshipFactory(Entity target, boolean toMany) { 449 super(target, toMany); 450 } 451 452 protected Object create(String name, Object namingContext) { 453 return new DbRelationship(name); 454 } 455 456 461 protected String nameBase() { 462 if (target == null) { 463 return "untitledRel"; 464 } 465 466 String name = target.getName(); 467 return (toMany) ? name + "_ARRAY" : "TO_" + name; 468 } 469 } 470 } | Popular Tags |