KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > project > NamedObjectFactory


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.project;
57
58 import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.Map JavaDoc;
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 /**
83  * Factory class that generates various Cayenne objects with
84  * default names that are unique in their corresponding context.
85  * Supports creation of the following
86  * objects:
87  * <ul>
88  * <li>DataMap</li>
89  * <li>ObjEntity</li>
90  * <li>ObjAttribute</li>
91  * <li>ObjRelationship</li>
92  * <li>DbEntity</li>
93  * <li>DerivedDbEntity</li>
94  * <li>DbAttribute</li>
95  * <li>DerivedDbAttribute</li>
96  * <li>DbRelationship</li>
97  * <li>DataNode</li>
98  * <li>DataDomain</li>
99  * <li>Query</li>
100  * <li>Procedure</li>
101  * <li>ProcedureParameter</li>
102  * </ul>
103  *
104  * This is a helper class used mostly by GUI and database
105  * reengineering classes.
106  *
107  * @author Andrei Adamchik
108  */

109 public abstract class NamedObjectFactory {
110     private static final Map JavaDoc factories = new HashMap JavaDoc();
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 JavaDoc createName(Class JavaDoc objectClass, Object JavaDoc namingContext) {
130         return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext);
131     }
132     
133     /**
134      * @since 1.0.5
135      */

136     public static String JavaDoc createName(Class JavaDoc objectClass, Object JavaDoc namingContext, String JavaDoc nameBase) {
137         return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext, nameBase);
138     }
139
140     /**
141      * Creates an object using an appropriate factory class.
142      * If no factory is found for the object, NullPointerException is
143      * thrown.
144      *
145      * <p><i>Note that newly created object is not added to the parent.
146      * This behavior can be changed later.</i></p>
147      */

148     public static Object JavaDoc createObject(Class JavaDoc objectClass, Object JavaDoc namingContext) {
149         return ((NamedObjectFactory) factories.get(objectClass)).makeObject(
150             namingContext);
151     }
152
153     /**
154      * @since 1.0.5
155      */

156     public static Object JavaDoc createObject(
157         Class JavaDoc objectClass,
158         Object JavaDoc namingContext,
159         String JavaDoc nameBase) {
160         return ((NamedObjectFactory) factories.get(objectClass)).makeObject(
161             namingContext,
162             nameBase);
163     }
164
165     /**
166      * Creates a relationship using an appropriate factory class.
167      * If no factory is found for the object, NullPointerException is
168      * thrown.
169      *
170      * <p><i>Note that newly created object is not added to the parent.
171      * This behavior can be changed later.</i></p>
172      */

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     /**
185      * Creates a unique name for the new object and constructs
186      * this object.
187      */

188     protected synchronized String JavaDoc makeName(Object JavaDoc namingContext) {
189         return makeName(namingContext, nameBase());
190     }
191     
192     /**
193      * @since 1.0.5
194      */

195     protected synchronized String JavaDoc makeName(Object JavaDoc namingContext, String JavaDoc nameBase) {
196         int c = 1;
197         String JavaDoc name = nameBase;
198         while (isNameInUse(name, namingContext)) {
199             name = nameBase + c++;
200         }
201         
202         return name;
203     }
204
205     /**
206      * Creates a unique name for the new object and constructs this object.
207      */

208     protected Object JavaDoc makeObject(Object JavaDoc namingContext) {
209         return makeObject(namingContext, nameBase());
210     }
211     
212     /**
213      * @since 1.0.5
214      */

215     protected Object JavaDoc makeObject(Object JavaDoc namingContext, String JavaDoc nameBase) {
216         return create(makeName(namingContext, nameBase), namingContext);
217     }
218
219     /** Returns a base default name, like "UntitledEntity", etc. */
220     protected abstract String JavaDoc nameBase();
221
222     /** Internal factory method. Invoked after the name is figured out. */
223     protected abstract Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext);
224
225     /**
226      * Checks if the name is already taken by another sibling
227      * in the same context.
228      */

229     protected abstract boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext);
230
231     // concrete factories
232
static class DataDomainFactory extends NamedObjectFactory {
233         protected String JavaDoc nameBase() {
234             return "UntitledDomain";
235         }
236
237         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
238             return new DataDomain(name);
239         }
240
241         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
242             Configuration config = (Configuration) namingContext;
243             return config.getDomain(name) != null;
244         }
245     }
246
247     static class DataMapFactory extends NamedObjectFactory {
248         protected String JavaDoc nameBase() {
249             return "UntitledMap";
250         }
251
252         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
253             return new DataMap(name);
254         }
255
256         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
257             // null context is a situation when DataMap is a
258
// top level object of the project
259
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 JavaDoc nameBase() {
270             return "UntitledObjEntity";
271         }
272
273         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
274             return new ObjEntity(name);
275         }
276
277         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
278             DataMap map = (DataMap) namingContext;
279             return map.getObjEntity(name) != null;
280         }
281     }
282
283     static class DbEntityFactory extends NamedObjectFactory {
284         protected String JavaDoc nameBase() {
285             return "UntitledDbEntity";
286         }
287
288         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
289             return new DbEntity(name);
290         }
291
292         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
293             DataMap map = (DataMap) namingContext;
294             return map.getDbEntity(name) != null;
295         }
296     }
297
298     static class ProcedureParameterFactory extends NamedObjectFactory {
299         protected String JavaDoc nameBase() {
300             return "UntitledProcedureParameter";
301         }
302
303         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
304             return new ProcedureParameter(name);
305         }
306
307         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
308             
309             // it doesn't matter if we create a parameter with
310
// a duplicate name.. parameters are positional anyway..
311
// still try to use unique names for visual consistency
312
Procedure procedure = (Procedure) namingContext;
313             Iterator JavaDoc 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 JavaDoc nameBase() {
327             return "UntitledProcedure";
328         }
329
330         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
331             return new Procedure(name);
332         }
333
334         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
335             DataMap map = (DataMap) namingContext;
336             return map.getProcedure(name) != null;
337         }
338     }
339     
340     static class SelectQueryFactory extends NamedObjectFactory {
341         protected String JavaDoc nameBase() {
342             return "UntitledQuery";
343         }
344
345         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
346             SelectQuery query = new SelectQuery();
347             query.setName(name);
348             return query;
349         }
350
351         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
352             DataMap map = (DataMap) namingContext;
353             return map.getQuery(name) != null;
354         }
355     }
356
357     static class DerivedDbEntityFactory extends DbEntityFactory {
358         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
359             return new DerivedDbEntity(name);
360         }
361     }
362
363     static class ObjAttributeFactory extends NamedObjectFactory {
364         protected String JavaDoc nameBase() {
365             return "untitledAttr";
366         }
367
368         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
369             return new ObjAttribute(name, null, (ObjEntity) namingContext);
370         }
371
372         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
373             Entity ent = (Entity) namingContext;
374             return ent.getAttribute(name) != null;
375         }
376     }
377
378     static class DbAttributeFactory extends ObjAttributeFactory {
379         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc 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 JavaDoc nameBase() {
399             return "UntitledDataNode";
400         }
401
402         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
403             return new DataNode(name);
404         }
405
406         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
422             return new ObjRelationship(name);
423         }
424
425         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
426             Entity ent = (Entity) namingContext;
427             return ent.getRelationship(name) != null;
428         }
429
430         /**
431          * Returns generated name for the ObjRelationships.
432          * For to-one case and entity name "xxxx" it generates name "toXxxx".
433          * For to-many case and entity name "Xxxx" it generates name "xxxxArray".
434          */

435         protected String JavaDoc nameBase() {
436             if (target == null) {
437                 return "untitledRel";
438             }
439
440             String JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
453             return new DbRelationship(name);
454         }
455
456         /**
457          * Returns generated name for the DbRelationships.
458          * For to-one case it generates name "TO_XXXX".
459          * For to-many case it generates name "XXXX_ARRAY".
460          */

461         protected String JavaDoc nameBase() {
462             if (target == null) {
463                 return "untitledRel";
464             }
465
466             String JavaDoc name = target.getName();
467             return (toMany) ? name + "_ARRAY" : "TO_" + name;
468         }
469     }
470 }
Popular Tags