KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
47  * Factory class that generates various Cayenne objects with
48  * default names that are unique in their corresponding context.
49  * Supports creation of the following
50  * objects:
51  * <ul>
52  * <li>DataMap</li>
53  * <li>ObjEntity</li>
54  * <li>ObjAttribute</li>
55  * <li>ObjRelationship</li>
56  * <li>DbEntity</li>
57  * <li>DerivedDbEntity</li>
58  * <li>DbAttribute</li>
59  * <li>DerivedDbAttribute</li>
60  * <li>DbRelationship</li>
61  * <li>DataNode</li>
62  * <li>DataDomain</li>
63  * <li>Query</li>
64  * <li>Procedure</li>
65  * <li>ProcedureParameter</li>
66  * </ul>
67  *
68  * This is a helper class used mostly by GUI and database
69  * reengineering classes.
70  *
71  * @author Andrus Adamchik
72  */

73 public abstract class NamedObjectFactory {
74     private static final Map JavaDoc factories = new HashMap JavaDoc();
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 JavaDoc createName(Class JavaDoc objectClass, Object JavaDoc namingContext) {
94         return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext);
95     }
96     
97     /**
98      * @since 1.0.5
99      */

100     public static String JavaDoc createName(Class JavaDoc objectClass, Object JavaDoc namingContext, String JavaDoc nameBase) {
101         return ((NamedObjectFactory) factories.get(objectClass)).makeName(namingContext, nameBase);
102     }
103
104     /**
105      * Creates an object using an appropriate factory class.
106      * If no factory is found for the object, NullPointerException is
107      * thrown.
108      *
109      * <p><i>Note that newly created object is not added to the parent.
110      * This behavior can be changed later.</i></p>
111      */

112     public static Object JavaDoc createObject(Class JavaDoc objectClass, Object JavaDoc namingContext) {
113         return ((NamedObjectFactory) factories.get(objectClass)).makeObject(
114             namingContext);
115     }
116
117     /**
118      * @since 1.0.5
119      */

120     public static Object JavaDoc createObject(
121         Class JavaDoc objectClass,
122         Object JavaDoc namingContext,
123         String JavaDoc nameBase) {
124         return ((NamedObjectFactory) factories.get(objectClass)).makeObject(
125             namingContext,
126             nameBase);
127     }
128
129     /**
130      * Creates a relationship using an appropriate factory class.
131      * If no factory is found for the object, NullPointerException is
132      * thrown.
133      *
134      * <p><i>Note that newly created object is not added to the parent.
135      * This behavior can be changed later.</i></p>
136      */

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     /**
149      * Creates a unique name for the new object and constructs
150      * this object.
151      */

152     protected synchronized String JavaDoc makeName(Object JavaDoc namingContext) {
153         return makeName(namingContext, nameBase());
154     }
155     
156     /**
157      * @since 1.0.5
158      */

159     protected synchronized String JavaDoc makeName(Object JavaDoc namingContext, String JavaDoc nameBase) {
160         int c = 1;
161         String JavaDoc name = nameBase;
162         while (isNameInUse(name, namingContext)) {
163             name = nameBase + c++;
164         }
165         
166         return name;
167     }
168
169     /**
170      * Creates a unique name for the new object and constructs this object.
171      */

172     protected Object JavaDoc makeObject(Object JavaDoc namingContext) {
173         return makeObject(namingContext, nameBase());
174     }
175     
176     /**
177      * @since 1.0.5
178      */

179     protected Object JavaDoc makeObject(Object JavaDoc namingContext, String JavaDoc nameBase) {
180         return create(makeName(namingContext, nameBase), namingContext);
181     }
182
183     /** Returns a base default name, like "UntitledEntity", etc. */
184     protected abstract String JavaDoc nameBase();
185
186     /** Internal factory method. Invoked after the name is figured out. */
187     protected abstract Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext);
188
189     /**
190      * Checks if the name is already taken by another sibling
191      * in the same context.
192      */

193     protected abstract boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext);
194
195     // concrete factories
196
static class DataDomainFactory extends NamedObjectFactory {
197         protected String JavaDoc nameBase() {
198             return "UntitledDomain";
199         }
200
201         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
202             return new DataDomain(name);
203         }
204
205         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
206             Configuration config = (Configuration) namingContext;
207             return config.getDomain(name) != null;
208         }
209     }
210
211     static class DataMapFactory extends NamedObjectFactory {
212         protected String JavaDoc nameBase() {
213             return "UntitledMap";
214         }
215
216         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
217             return new DataMap(name);
218         }
219
220         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
221             // null context is a situation when DataMap is a
222
// top level object of the project
223
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 JavaDoc nameBase() {
234             return "UntitledObjEntity";
235         }
236
237         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
238             return new ObjEntity(name);
239         }
240
241         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
242             DataMap map = (DataMap) namingContext;
243             return map.getObjEntity(name) != null;
244         }
245     }
246
247     static class DbEntityFactory extends NamedObjectFactory {
248         protected String JavaDoc nameBase() {
249             return "UntitledDbEntity";
250         }
251
252         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
253             return new DbEntity(name);
254         }
255
256         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
257             DataMap map = (DataMap) namingContext;
258             return map.getDbEntity(name) != null;
259         }
260     }
261
262     static class ProcedureParameterFactory extends NamedObjectFactory {
263         protected String JavaDoc nameBase() {
264             return "UntitledProcedureParameter";
265         }
266
267         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
268             return new ProcedureParameter(name);
269         }
270
271         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
272             
273             // it doesn't matter if we create a parameter with
274
// a duplicate name.. parameters are positional anyway..
275
// still try to use unique names for visual consistency
276
Procedure procedure = (Procedure) namingContext;
277             Iterator JavaDoc 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 JavaDoc nameBase() {
291             return "UntitledProcedure";
292         }
293
294         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
295             return new Procedure(name);
296         }
297
298         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
299             DataMap map = (DataMap) namingContext;
300             return map.getProcedure(name) != null;
301         }
302     }
303     
304     static class SelectQueryFactory extends NamedObjectFactory {
305         protected String JavaDoc nameBase() {
306             return "UntitledQuery";
307         }
308
309         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
310             SelectQuery query = new SelectQuery();
311             query.setName(name);
312             return query;
313         }
314
315         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
316             DataMap map = (DataMap) namingContext;
317             return map.getQuery(name) != null;
318         }
319     }
320
321     static class DerivedDbEntityFactory extends DbEntityFactory {
322         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
323             return new DerivedDbEntity(name);
324         }
325     }
326
327     static class ObjAttributeFactory extends NamedObjectFactory {
328         protected String JavaDoc nameBase() {
329             return "untitledAttr";
330         }
331
332         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
333             return new ObjAttribute(name, null, (ObjEntity) namingContext);
334         }
335
336         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
337             Entity ent = (Entity) namingContext;
338             return ent.getAttribute(name) != null;
339         }
340     }
341
342     static class DbAttributeFactory extends ObjAttributeFactory {
343         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc 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 JavaDoc nameBase() {
363             return "UntitledDataNode";
364         }
365
366         protected Object JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
367             return new DataNode(name);
368         }
369
370         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
386             return new ObjRelationship(name);
387         }
388
389         protected boolean isNameInUse(String JavaDoc name, Object JavaDoc namingContext) {
390             Entity ent = (Entity) namingContext;
391             return ent.getRelationship(name) != null;
392         }
393
394         /**
395          * Returns generated name for the ObjRelationships.
396          * For to-one case and entity name "xxxx" it generates name "toXxxx".
397          * For to-many case and entity name "Xxxx" it generates name "xxxxArray".
398          */

399         protected String JavaDoc nameBase() {
400             if (target == null) {
401                 return "untitledRel";
402             }
403
404             String JavaDoc 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 JavaDoc create(String JavaDoc name, Object JavaDoc namingContext) {
417             return new DbRelationship(name);
418         }
419
420         /**
421          * Returns generated name for the DbRelationships.
422          * For to-one case it generates name "TO_XXXX".
423          * For to-many case it generates name "XXXX_ARRAY".
424          */

425         protected String JavaDoc nameBase() {
426             if (target == null) {
427                 return "untitledRel";
428             }
429
430             String JavaDoc name = target.getName();
431             return (toMany) ? name + "_ARRAY" : "TO_" + name;
432         }
433     }
434 }
435
Popular Tags