1 19 20 package org.apache.cayenne.project; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.apache.cayenne.access.DataDomain; 27 import org.apache.cayenne.access.DataNode; 28 import org.apache.cayenne.conf.Configuration; 29 import org.apache.cayenne.dba.TypesMapping; 30 import org.apache.cayenne.map.DataMap; 31 import org.apache.cayenne.map.DbAttribute; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.map.DbRelationship; 34 import org.apache.cayenne.map.DerivedDbAttribute; 35 import org.apache.cayenne.map.DerivedDbEntity; 36 import org.apache.cayenne.map.Entity; 37 import org.apache.cayenne.map.ObjAttribute; 38 import org.apache.cayenne.map.ObjEntity; 39 import org.apache.cayenne.map.ObjRelationship; 40 import org.apache.cayenne.map.Procedure; 41 import org.apache.cayenne.map.ProcedureParameter; 42 import org.apache.cayenne.map.Relationship; 43 import org.apache.cayenne.query.Query; 44 import org.apache.cayenne.query.SelectQuery; 45 46 73 public abstract class NamedObjectFactory { 74 private static final Map factories = new HashMap (); 75 76 static { 77 factories.put(DataMap.class, new DataMapFactory()); 78 factories.put(ObjEntity.class, new ObjEntityFactory()); 79 factories.put(DbEntity.class, new DbEntityFactory()); 80 factories.put(DerivedDbEntity.class, new DerivedDbEntityFactory()); 81 factories.put(ObjAttribute.class, new ObjAttributeFactory()); 82 factories.put(DbAttribute.class, new DbAttributeFactory()); 83 factories.put(DerivedDbAttribute.class, new DerivedDbAttributeFactory()); 84 factories.put(DataNode.class, new DataNodeFactory()); 85 factories.put(DbRelationship.class, new DbRelationshipFactory(null, false)); 86 factories.put(ObjRelationship.class, new ObjRelationshipFactory(null, false)); 87 factories.put(DataDomain.class, new DataDomainFactory()); 88 factories.put(Procedure.class, new ProcedureFactory()); 89 factories.put(Query.class, new SelectQueryFactory()); 90 factories.put(ProcedureParameter.class, new ProcedureParameterFactory()); 91 } 92 93 public static String createName(Class objectClass, Object namingContext) { 94 return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext); 95 } 96 97 100 public static String createName(Class objectClass, Object namingContext, String nameBase) { 101 return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext, nameBase); 102 } 103 104 112 public static Object createObject(Class objectClass, Object namingContext) { 113 return ((NamedObjectFactory) factories.get(objectClass)).makeObject( 114 namingContext); 115 } 116 117 120 public static Object createObject( 121 Class objectClass, 122 Object namingContext, 123 String nameBase) { 124 return ((NamedObjectFactory) factories.get(objectClass)).makeObject( 125 namingContext, 126 nameBase); 127 } 128 129 137 public static Relationship createRelationship( 138 Entity srcEnt, 139 Entity targetEnt, 140 boolean toMany) { 141 NamedObjectFactory factory = 142 (srcEnt instanceof ObjEntity) 143 ? new ObjRelationshipFactory(targetEnt, toMany) 144 : new DbRelationshipFactory(targetEnt, toMany); 145 return (Relationship) factory.makeObject(srcEnt); 146 } 147 148 152 protected synchronized String makeName(Object namingContext) { 153 return makeName(namingContext, nameBase()); 154 } 155 156 159 protected synchronized String makeName(Object namingContext, String nameBase) { 160 int c = 1; 161 String name = nameBase; 162 while (isNameInUse(name, namingContext)) { 163 name = nameBase + c++; 164 } 165 166 return name; 167 } 168 169 172 protected Object makeObject(Object namingContext) { 173 return makeObject(namingContext, nameBase()); 174 } 175 176 179 protected Object makeObject(Object namingContext, String nameBase) { 180 return create(makeName(namingContext, nameBase), namingContext); 181 } 182 183 184 protected abstract String nameBase(); 185 186 187 protected abstract Object create(String name, Object namingContext); 188 189 193 protected abstract boolean isNameInUse(String name, Object namingContext); 194 195 static class DataDomainFactory extends NamedObjectFactory { 197 protected String nameBase() { 198 return "UntitledDomain"; 199 } 200 201 protected Object create(String name, Object namingContext) { 202 return new DataDomain(name); 203 } 204 205 protected boolean isNameInUse(String name, Object namingContext) { 206 Configuration config = (Configuration) namingContext; 207 return config.getDomain(name) != null; 208 } 209 } 210 211 static class DataMapFactory extends NamedObjectFactory { 212 protected String nameBase() { 213 return "UntitledMap"; 214 } 215 216 protected Object create(String name, Object namingContext) { 217 return new DataMap(name); 218 } 219 220 protected boolean isNameInUse(String name, Object namingContext) { 221 if (namingContext == null) { 224 return false; 225 } 226 227 DataDomain domain = (DataDomain) namingContext; 228 return domain.getMap(name) != null; 229 } 230 } 231 232 static class ObjEntityFactory extends NamedObjectFactory { 233 protected String nameBase() { 234 return "UntitledObjEntity"; 235 } 236 237 protected Object create(String name, Object namingContext) { 238 return new ObjEntity(name); 239 } 240 241 protected boolean isNameInUse(String name, Object namingContext) { 242 DataMap map = (DataMap) namingContext; 243 return map.getObjEntity(name) != null; 244 } 245 } 246 247 static class DbEntityFactory extends NamedObjectFactory { 248 protected String nameBase() { 249 return "UntitledDbEntity"; 250 } 251 252 protected Object create(String name, Object namingContext) { 253 return new DbEntity(name); 254 } 255 256 protected boolean isNameInUse(String name, Object namingContext) { 257 DataMap map = (DataMap) namingContext; 258 return map.getDbEntity(name) != null; 259 } 260 } 261 262 static class ProcedureParameterFactory extends NamedObjectFactory { 263 protected String nameBase() { 264 return "UntitledProcedureParameter"; 265 } 266 267 protected Object create(String name, Object namingContext) { 268 return new ProcedureParameter(name); 269 } 270 271 protected boolean isNameInUse(String name, Object namingContext) { 272 273 Procedure procedure = (Procedure) namingContext; 277 Iterator it = procedure.getCallParameters().iterator(); 278 while (it.hasNext()) { 279 ProcedureParameter parameter = (ProcedureParameter) it.next(); 280 if (name.equals(parameter.getName())) { 281 return true; 282 } 283 } 284 285 return false; 286 } 287 } 288 289 static class ProcedureFactory extends NamedObjectFactory { 290 protected String nameBase() { 291 return "UntitledProcedure"; 292 } 293 294 protected Object create(String name, Object namingContext) { 295 return new Procedure(name); 296 } 297 298 protected boolean isNameInUse(String name, Object namingContext) { 299 DataMap map = (DataMap) namingContext; 300 return map.getProcedure(name) != null; 301 } 302 } 303 304 static class SelectQueryFactory extends NamedObjectFactory { 305 protected String nameBase() { 306 return "UntitledQuery"; 307 } 308 309 protected Object create(String name, Object namingContext) { 310 SelectQuery query = new SelectQuery(); 311 query.setName(name); 312 return query; 313 } 314 315 protected boolean isNameInUse(String name, Object namingContext) { 316 DataMap map = (DataMap) namingContext; 317 return map.getQuery(name) != null; 318 } 319 } 320 321 static class DerivedDbEntityFactory extends DbEntityFactory { 322 protected Object create(String name, Object namingContext) { 323 return new DerivedDbEntity(name); 324 } 325 } 326 327 static class ObjAttributeFactory extends NamedObjectFactory { 328 protected String nameBase() { 329 return "untitledAttr"; 330 } 331 332 protected Object create(String name, Object namingContext) { 333 return new ObjAttribute(name, null, (ObjEntity) namingContext); 334 } 335 336 protected boolean isNameInUse(String name, Object namingContext) { 337 Entity ent = (Entity) namingContext; 338 return ent.getAttribute(name) != null; 339 } 340 } 341 342 static class DbAttributeFactory extends ObjAttributeFactory { 343 protected Object create(String name, Object namingContext) { 344 return new DbAttribute( 345 name, 346 TypesMapping.NOT_DEFINED, 347 (DbEntity) namingContext); 348 } 349 } 350 351 static class DerivedDbAttributeFactory extends ObjAttributeFactory { 352 protected Object create(String name, Object namingContext) { 353 return new DerivedDbAttribute( 354 name, 355 TypesMapping.NOT_DEFINED, 356 (DbEntity) namingContext, 357 DerivedDbAttribute.ATTRIBUTE_TOKEN); 358 } 359 } 360 361 static class DataNodeFactory extends NamedObjectFactory { 362 protected String nameBase() { 363 return "UntitledDataNode"; 364 } 365 366 protected Object create(String name, Object namingContext) { 367 return new DataNode(name); 368 } 369 370 protected boolean isNameInUse(String name, Object namingContext) { 371 DataDomain domain = (DataDomain) namingContext; 372 return domain.getNode(name) != null; 373 } 374 } 375 376 static class ObjRelationshipFactory extends NamedObjectFactory { 377 protected Entity target; 378 protected boolean toMany; 379 380 public ObjRelationshipFactory(Entity target, boolean toMany) { 381 this.target = target; 382 this.toMany = toMany; 383 } 384 385 protected Object create(String name, Object namingContext) { 386 return new ObjRelationship(name); 387 } 388 389 protected boolean isNameInUse(String name, Object namingContext) { 390 Entity ent = (Entity) namingContext; 391 return ent.getRelationship(name) != null; 392 } 393 394 399 protected String nameBase() { 400 if (target == null) { 401 return "untitledRel"; 402 } 403 404 String name = target.getName(); 405 return (toMany) 406 ? Character.toLowerCase(name.charAt(0)) + name.substring(1) + "Array" 407 : "to" + Character.toUpperCase(name.charAt(0)) + name.substring(1); 408 } 409 } 410 411 static class DbRelationshipFactory extends ObjRelationshipFactory { 412 public DbRelationshipFactory(Entity target, boolean toMany) { 413 super(target, toMany); 414 } 415 416 protected Object create(String name, Object namingContext) { 417 return new DbRelationship(name); 418 } 419 420 425 protected String nameBase() { 426 if (target == null) { 427 return "untitledRel"; 428 } 429 430 String name = target.getName(); 431 return (toMany) ? name + "_ARRAY" : "TO_" + name; 432 } 433 } 434 } 435 | Popular Tags |