KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > map > DataMap


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.map;
57
58 import java.io.PrintWriter JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Collection JavaDoc;
61 import java.util.Collections JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Map JavaDoc;
64 import java.util.SortedMap JavaDoc;
65
66 import org.apache.commons.lang.builder.ToStringBuilder;
67 import org.objectstyle.cayenne.map.event.AttributeEvent;
68 import org.objectstyle.cayenne.map.event.DbEntityListener;
69 import org.objectstyle.cayenne.map.event.DbAttributeListener;
70 import org.objectstyle.cayenne.map.event.DbRelationshipListener;
71 import org.objectstyle.cayenne.map.event.EntityEvent;
72 import org.objectstyle.cayenne.map.event.ObjEntityListener;
73 import org.objectstyle.cayenne.map.event.ObjAttributeListener;
74 import org.objectstyle.cayenne.map.event.ObjRelationshipListener;
75 import org.objectstyle.cayenne.map.event.RelationshipEvent;
76 import org.objectstyle.cayenne.project.Project;
77 import org.objectstyle.cayenne.query.Query;
78 import org.objectstyle.cayenne.util.CayenneMap;
79 import org.objectstyle.cayenne.util.Util;
80 import org.objectstyle.cayenne.util.XMLEncoder;
81 import org.objectstyle.cayenne.util.XMLSerializable;
82
83 /**
84  * Stores a collection of related mapping objects that describe database and object layers
85  * of an application. DataMap contains DbEntities mapping database tables, ObjEntities -
86  * mapping persistent Java classes, Procedures - mapping database stored procedures.
87  *
88  * @author Michael Shengaout
89  * @author Andrei Adamchik
90  * @author Craig Miskell
91  */

92 public class DataMap implements XMLSerializable, MappingNamespace,
93                                 DbEntityListener, DbAttributeListener, DbRelationshipListener,
94                                 ObjEntityListener, ObjAttributeListener, ObjRelationshipListener
95 {
96
97     /**
98      * Defines the name of the property for default DB schema.
99      *
100      * @since 1.1
101      */

102     public static final String JavaDoc DEFAULT_SCHEMA_PROPERTY = "defaultSchema";
103
104     /**
105      * Defines the name of the property for default Java class package.
106      *
107      * @since 1.1
108      */

109     public static final String JavaDoc DEFAULT_PACKAGE_PROPERTY = "defaultPackage";
110
111     /**
112      * Defines the name of the property for default DB schema.
113      *
114      * @since 1.1
115      */

116     public static final String JavaDoc DEFAULT_SUPERCLASS_PROPERTY = "defaultSuperclass";
117
118     /**
119      * Defines the name of the property for default DB schema.
120      *
121      * @since 1.1
122      */

123     public static final String JavaDoc DEFAULT_LOCK_TYPE_PROPERTY = "defaultLockType";
124
125     protected String JavaDoc name;
126     protected String JavaDoc location;
127     protected MappingNamespace namespace;
128
129     protected String JavaDoc defaultSchema;
130     protected String JavaDoc defaultPackage;
131     protected String JavaDoc defaultSuperclass;
132     protected int defaultLockType;
133
134     // ====================================================
135
// ObjEntities
136
// ====================================================
137
private CayenneMap objEntityMap = new CayenneMap(this);
138
139     // read-through reference for public access
140
private SortedMap JavaDoc objEntityMapRef = Collections.unmodifiableSortedMap(objEntityMap);
141
142     // read-through reference for public access to the ObjEntities
143
private Collection JavaDoc objEntityValuesRef = Collections
144             .unmodifiableCollection(objEntityMap.values());
145
146     // ====================================================
147
// DbEntities
148
// ====================================================
149
private CayenneMap dbEntityMap = new CayenneMap(this);
150
151     // read-through reference for public access
152
private SortedMap JavaDoc dbEntityMapRef = Collections.unmodifiableSortedMap(dbEntityMap);
153
154     // read-through reference for public access
155
private Collection JavaDoc dbEntityValuesRef = Collections.unmodifiableCollection(dbEntityMap
156             .values());
157
158     // ====================================================
159
// Procedures
160
// ====================================================
161
private CayenneMap procedureMap = new CayenneMap(this);
162
163     // read-through reference for public access
164
private SortedMap JavaDoc procedureMapRef = Collections.unmodifiableSortedMap(procedureMap);
165
166     // read-through reference for public access
167
private Collection JavaDoc procedureValuesRef = Collections
168             .unmodifiableCollection(procedureMap.values());
169
170     // ====================================================
171
// Queries
172
// ====================================================
173
private CayenneMap queryMap = new CayenneMap(this);
174
175     // read-through reference for public access
176
private SortedMap JavaDoc queryMapRef = Collections.unmodifiableSortedMap(queryMap);
177
178     // read-through reference for public access
179
private Collection JavaDoc queriesValuesRef = Collections.unmodifiableCollection(queryMap
180             .values());
181
182     /**
183      * Creates a new DataMap.
184      */

185     public DataMap() {
186         // must do this to setup defaults
187
initWithProperties(Collections.EMPTY_MAP);
188     }
189
190     /**
191      * Creates a new named DataMap.
192      */

193     public DataMap(String JavaDoc mapName) {
194         this();
195         this.setName(mapName);
196     }
197
198     /**
199      * Performs DataMap initialization from a set of properties, using defaults for the
200      * missing properties.
201      *
202      * @since 1.1
203      */

204     public void initWithProperties(Map JavaDoc properties) {
205         // must init defaults even if properties are empty
206
if (properties == null) {
207             properties = Collections.EMPTY_MAP;
208         }
209
210         Object JavaDoc lockType = properties.get(DEFAULT_LOCK_TYPE_PROPERTY);
211         Object JavaDoc packageName = properties.get(DEFAULT_PACKAGE_PROPERTY);
212         Object JavaDoc schema = properties.get(DEFAULT_SCHEMA_PROPERTY);
213         Object JavaDoc superclass = properties.get(DEFAULT_SUPERCLASS_PROPERTY);
214
215         this.defaultLockType = "optimistic".equals(lockType)
216                 ? ObjEntity.LOCK_TYPE_OPTIMISTIC
217                 : ObjEntity.LOCK_TYPE_NONE;
218
219         this.defaultPackage = (packageName != null) ? packageName.toString() : null;
220         this.defaultSchema = (schema != null) ? schema.toString() : null;
221         this.defaultSuperclass = (superclass != null) ? superclass.toString() : null;
222     }
223
224     /**
225      * Prints itself as a well-formed complete XML document. In comparison,
226      * {@link #encodeAsXML(XMLEncoder)}stores DataMap assuming it is a part of a bigger
227      * document.
228      *
229      * @since 1.1
230      */

231     public void encodeAsXML(PrintWriter JavaDoc pw) {
232         XMLEncoder encoder = new XMLEncoder(pw, "\t");
233         encoder.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
234         encodeAsXML(encoder);
235     }
236
237     /**
238      * Prints itself as XML to the provided PrintWriter.
239      *
240      * @since 1.1
241      */

242     public void encodeAsXML(XMLEncoder encoder) {
243         encoder.print("<data-map project-version=\"");
244         encoder.print(String.valueOf(Project.CURRENT_PROJECT_VERSION));
245         encoder.println("\">");
246
247         encoder.indent(1);
248
249         // properties
250
if (defaultLockType == ObjEntity.LOCK_TYPE_OPTIMISTIC) {
251             encoder.printProperty(DEFAULT_LOCK_TYPE_PROPERTY, "optimistic");
252         }
253
254         if (!Util.isEmptyString(defaultPackage)) {
255             encoder.printProperty(DEFAULT_PACKAGE_PROPERTY, defaultPackage);
256         }
257
258         if (!Util.isEmptyString(defaultSchema)) {
259             encoder.printProperty(DEFAULT_SCHEMA_PROPERTY, defaultSchema);
260         }
261
262         if (!Util.isEmptyString(defaultSuperclass)) {
263             encoder.printProperty(DEFAULT_SUPERCLASS_PROPERTY, defaultSuperclass);
264         }
265
266         // procedures
267
encoder.print(getProcedureMap());
268
269         // DbEntities
270
boolean hasDerived = false;
271         Iterator JavaDoc dbEntities = getDbEntityMap().entrySet().iterator();
272         while (dbEntities.hasNext()) {
273             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) dbEntities.next();
274             DbEntity dbe = (DbEntity) entry.getValue();
275
276             // skip derived, store them after regular DbEntities
277
if (dbe instanceof DerivedDbEntity) {
278                 hasDerived = true;
279             }
280             else {
281                 dbe.encodeAsXML(encoder);
282             }
283         }
284
285         // DerivedDbEntities
286
if (hasDerived) {
287             Iterator JavaDoc derivedDbEntities = getDbEntityMap().entrySet().iterator();
288             while (derivedDbEntities.hasNext()) {
289                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) derivedDbEntities.next();
290                 DbEntity dbe = (DbEntity) entry.getValue();
291
292                 // only store derived...
293
if (dbe instanceof DerivedDbEntity) {
294                     dbe.encodeAsXML(encoder);
295                 }
296             }
297         }
298
299         // others...
300
encoder.print(getObjEntityMap());
301         encodeDBRelationshipsAsXML(getDbEntityMap(), encoder);
302         encodeOBJRelationshipsAsXML(getObjEntityMap(), encoder);
303         encoder.print(getQueryMap());
304
305         encoder.indent(-1);
306         encoder.println("</data-map>");
307     }
308
309     // stores relationships of for the map of entities
310
private final void encodeDBRelationshipsAsXML(Map JavaDoc entityMap, XMLEncoder encoder) {
311         Iterator JavaDoc it = entityMap.entrySet().iterator();
312         while (it.hasNext()) {
313             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
314             Entity entity = (Entity) entry.getValue();
315             encoder.print(entity.getRelationships());
316         }
317     }
318
319     // stores relationships of for the map of entities
320
private final void encodeOBJRelationshipsAsXML(Map JavaDoc entityMap, XMLEncoder encoder) {
321         Iterator JavaDoc it = entityMap.entrySet().iterator();
322         while (it.hasNext()) {
323             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
324             ObjEntity entity = (ObjEntity) entry.getValue();
325             encoder.print(entity.getDeclaredRelationships());
326         }
327     }
328
329     public String JavaDoc toString() {
330         return new ToStringBuilder(this).append("name", getName()).toString();
331     }
332
333     /**
334      * Returns "name" property value.
335      */

336     public String JavaDoc getName() {
337         return name;
338     }
339
340     public void setName(String JavaDoc name) {
341         this.name = (name != null ? name : "unnamed");
342     }
343
344     /**
345      * Adds all Object and DB entities and Queries from another map to this map. Overwrites all
346      * existing entities and queries with the new ones.
347      * <p>
348      * <i>TODO: will need to implement advanced merge that allows different policies for
349      * overwriting entities / queries. </i>
350      * </p>
351      */

352     public void mergeWithDataMap(DataMap map) {
353         Iterator JavaDoc dbs = new ArrayList JavaDoc(map.getDbEntities()).iterator();
354         while (dbs.hasNext()) {
355             DbEntity ent = (DbEntity) dbs.next();
356             this.removeDbEntity(ent.getName());
357             this.addDbEntity(ent);
358         }
359
360         Iterator JavaDoc objs = new ArrayList JavaDoc(map.getObjEntities()).iterator();
361         while (objs.hasNext()) {
362             ObjEntity ent = (ObjEntity) objs.next();
363             this.removeObjEntity(ent.getName());
364             this.addObjEntity(ent);
365         }
366         
367         Iterator JavaDoc queries = new ArrayList JavaDoc(map.getQueries()).iterator();
368         while (queries.hasNext()) {
369             Query query = (Query) queries.next();
370             this.removeQuery(query.getName());
371             this.addQuery(query);
372         }
373     }
374
375     /**
376      * Returns "location" property value. Location is abstract and can depend on how the
377      * DataMap was loaded. E.g. location can be a File on the filesystem or a location
378      * within a JAR.
379      */

380     public String JavaDoc getLocation() {
381         return location;
382     }
383
384     /**
385      * Sets "location" property.
386      */

387     public void setLocation(String JavaDoc location) {
388         this.location = location;
389     }
390
391     /**
392      * Returns a sorted unmodifiable map of ObjEntities contained in this DataMap, keyed
393      * by ObjEntity name.
394      */

395     public SortedMap JavaDoc getObjEntityMap() {
396         return objEntityMapRef;
397     }
398
399     /**
400      * Returns a sorted unmodifiable map of DbEntities contained in this DataMap, keyed by
401      * DbEntity name.
402      */

403     public SortedMap JavaDoc getDbEntityMap() {
404         return dbEntityMapRef;
405     }
406
407     /**
408      * Returns a named query associated with this DataMap.
409      *
410      * @since 1.1
411      */

412     public Query getQuery(String JavaDoc queryName) {
413         Query query = (Query) queryMap.get(queryName);
414         if (query != null) {
415             return query;
416         }
417
418         return namespace != null ? namespace.getQuery(queryName) : null;
419     }
420
421     /**
422      * Stores a query under its name.
423      *
424      * @since 1.1
425      */

426     public void addQuery(Query query) {
427         if (query == null) {
428             throw new NullPointerException JavaDoc("Can't add null query.");
429         }
430
431         if (query.getName() == null) {
432             throw new NullPointerException JavaDoc("Query name can't be null.");
433         }
434
435         queryMap.put(query.getName(), query);
436     }
437
438     /**
439      * Removes a named query from the DataMap.
440      *
441      * @since 1.1
442      */

443     public void removeQuery(String JavaDoc queryName) {
444         queryMap.remove(queryName);
445     }
446
447     /**
448      * @since 1.1
449      */

450     public void clearQueries() {
451         queryMap.clear();
452     }
453
454     /**
455      * @since 1.1
456      */

457     public SortedMap JavaDoc getQueryMap() {
458         return queryMapRef;
459     }
460
461     /**
462      * @since 1.1
463      */

464     public Collection JavaDoc getQueries() {
465         return queriesValuesRef;
466     }
467
468     /**
469      * Adds a new ObjEntity to this DataMap.
470      */

471     public void addObjEntity(ObjEntity objEntity) {
472         if (objEntity.getName() == null) {
473             throw new NullPointerException JavaDoc("Attempt to add ObjEntity with no name.");
474         }
475
476         objEntityMap.put(objEntity.getName(), objEntity);
477     }
478
479     /**
480      * Adds a new DbEntity to this DataMap.
481      */

482     public void addDbEntity(DbEntity dbEntity) {
483         if (dbEntity.getName() == null) {
484             throw new NullPointerException JavaDoc("Attempt to add DbEntity with no name.");
485         }
486
487         dbEntityMap.put(dbEntity.getName(), dbEntity);
488     }
489
490     /**
491      * Returns a list of ObjEntities stored in this DataMap.
492      */

493     public Collection JavaDoc getObjEntities() {
494         return objEntityValuesRef;
495     }
496
497     /**
498      * Returns all ObjEntities in this DataMap, including entities from dependent maps if
499      * <code>includeDeps</code> is <code>true</code>.
500      *
501      * @deprecated since 1.2 use getObjEntities().
502      */

503     public Collection JavaDoc getObjEntities(boolean includeDeps) {
504         return getObjEntities();
505     }
506
507     /**
508      * Returns all DbEntities in this DataMap.
509      */

510     public Collection JavaDoc getDbEntities() {
511         return dbEntityValuesRef;
512     }
513
514     /**
515      * Returns DbEntity matching the <code>name</code> parameter. No dependencies will
516      * be searched.
517      */

518     public DbEntity getDbEntity(String JavaDoc dbEntityName) {
519         DbEntity entity = (DbEntity) dbEntityMap.get(dbEntityName);
520
521         if (entity != null) {
522             return entity;
523         }
524
525         return namespace != null ? namespace.getDbEntity(dbEntityName) : null;
526     }
527
528     /**
529      * Returns an ObjEntity for a DataObject class name.
530      *
531      * @since 1.1
532      */

533     public ObjEntity getObjEntityForJavaClass(String JavaDoc javaClassName) {
534         if (javaClassName == null) {
535             return null;
536         }
537
538         Iterator JavaDoc it = getObjEntityMap().entrySet().iterator();
539         while (it.hasNext()) {
540             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
541             ObjEntity entity = (ObjEntity) entry.getValue();
542             if (javaClassName.equals(entity.getClassName())) {
543                 return entity;
544             }
545         }
546
547         return null;
548     }
549
550     /**
551      * Returns an ObjEntity for a given name. If it is not found in this DataMap, it will
552      * search a parent EntityNamespace.
553      */

554     public ObjEntity getObjEntity(String JavaDoc objEntityName) {
555         ObjEntity entity = (ObjEntity) objEntityMap.get(objEntityName);
556         if (entity != null) {
557             return entity;
558         }
559
560         return namespace != null ? namespace.getObjEntity(objEntityName) : null;
561     }
562
563     /**
564      * Returns a list of ObjEntities mapped to the given DbEntity.
565      */

566     public Collection JavaDoc getMappedEntities(DbEntity dbEntity) {
567         if (dbEntity == null) {
568             return Collections.EMPTY_LIST;
569         }
570
571         Collection JavaDoc allEntities = (namespace != null)
572                 ? namespace.getObjEntities()
573                 : objEntityValuesRef;
574
575         if (allEntities.isEmpty()) {
576             return Collections.EMPTY_LIST;
577         }
578
579         Collection JavaDoc result = new ArrayList JavaDoc();
580         Iterator JavaDoc iter = allEntities.iterator();
581         while (iter.hasNext()) {
582             ObjEntity objEnt = (ObjEntity) iter.next();
583             if (objEnt.getDbEntity() == dbEntity) {
584                 result.add(objEnt);
585             }
586         }
587
588         return result;
589     }
590
591     /**
592      * "Dirty" remove of the DbEntity from the data map.
593      */

594     public void removeDbEntity(String JavaDoc dbEntityName) {
595         removeDbEntity(dbEntityName, false);
596     }
597
598     /**
599      * Removes DbEntity from the DataMap. If <code>clearDependencies</code> is true, all
600      * DbRelationships that reference this entity are also removed. ObjEntities that rely
601      * on this entity are cleaned up.
602      *
603      * @since 1.1
604      */

605     public void removeDbEntity(String JavaDoc dbEntityName, boolean clearDependencies) {
606         DbEntity dbEntityToDelete = (DbEntity) dbEntityMap.remove(dbEntityName);
607
608         if (dbEntityToDelete != null && clearDependencies) {
609             Iterator JavaDoc dbEnts = this.getDbEntities().iterator();
610             while (dbEnts.hasNext()) {
611                 DbEntity dbEnt = (DbEntity) dbEnts.next();
612                 // take a copy since we're going to modifiy the entity
613
Iterator JavaDoc rels = new ArrayList JavaDoc(dbEnt.getRelationships()).iterator();
614                 while (rels.hasNext()) {
615                     DbRelationship rel = (DbRelationship) rels.next();
616                     if (dbEntityName.equals(rel.getTargetEntityName())) {
617                         dbEnt.removeRelationship(rel.getName());
618                     }
619                 }
620             }
621
622             // Remove all obj relationships referencing removed DbRelationships.
623
Iterator JavaDoc objEnts = this.getObjEntities().iterator();
624             while (objEnts.hasNext()) {
625                 ObjEntity objEnt = (ObjEntity) objEnts.next();
626                 if (objEnt.getDbEntity() == dbEntityToDelete) {
627                     objEnt.clearDbMapping();
628                 }
629                 else {
630                     Iterator JavaDoc iter = objEnt.getRelationships().iterator();
631                     while (iter.hasNext()) {
632                         ObjRelationship rel = (ObjRelationship) iter.next();
633                         Iterator JavaDoc dbRels = rel.getDbRelationships().iterator();
634                         while (dbRels.hasNext()) {
635                             DbRelationship dbRel = (DbRelationship) dbRels.next();
636                             if (dbRel.getTargetEntity() == dbEntityToDelete) {
637                                 rel.clearDbRelationships();
638                                 break;
639                             }
640                         }
641                     }
642                 }
643             }
644         }
645     }
646
647     /**
648      * "Dirty" remove of the ObjEntity from the data map.
649      */

650     public void removeObjEntity(String JavaDoc objEntityName) {
651         removeObjEntity(objEntityName, false);
652     }
653
654     /**
655      * Removes ObjEntity from the DataMap. If <code>clearDependencies</code> is true,
656      * all ObjRelationships that reference this entity are also removed.
657      *
658      * @since 1.1
659      */

660     public void removeObjEntity(String JavaDoc objEntityName, boolean clearDependencies) {
661         ObjEntity entity = (ObjEntity) objEntityMap.remove(objEntityName);
662
663         if (entity != null && clearDependencies) {
664             Iterator JavaDoc entities = this.getObjEntityMap().values().iterator();
665             while (entities.hasNext()) {
666                 ObjEntity ent = (ObjEntity) entities.next();
667                 // take a copy since we're going to modifiy the entity
668
Iterator JavaDoc rels = new ArrayList JavaDoc(ent.getRelationships()).iterator();
669                 while (rels.hasNext()) {
670                     ObjRelationship rel = (ObjRelationship) rels.next();
671                     if (objEntityName.equals(rel.getTargetEntityName())
672                             || objEntityName.equals(rel.getTargetEntityName())) {
673                         ent.removeRelationship(rel.getName());
674                     }
675                 }
676             }
677         }
678     }
679
680     /**
681      * Returns stored procedures associated with this DataMap.
682      */

683     public Collection JavaDoc getProcedures() {
684         return procedureValuesRef;
685     }
686
687     /**
688      * Returns a Procedure for a given name or null if no such procedure exists. If
689      * Procedure is not found in this DataMap, a parent EntityNamcespace is searched.
690      */

691     public Procedure getProcedure(String JavaDoc procedureName) {
692         Procedure procedure = (Procedure) procedureMap.get(procedureName);
693         if (procedure != null) {
694             return procedure;
695         }
696
697         return namespace != null ? namespace.getProcedure(procedureName) : null;
698     }
699
700     /**
701      * Adds stored procedure to the list of procedures. If there is another procedure
702      * registered under the same name, throws an IllegalArgumentException.
703      */

704     public void addProcedure(Procedure procedure) {
705         if (procedure.getName() == null) {
706             throw new NullPointerException JavaDoc("Attempt to add procedure with no name.");
707         }
708
709         procedureMap.put(procedure.getName(), procedure);
710     }
711
712     public void removeProcedure(String JavaDoc name) {
713         procedureMap.remove(name);
714     }
715
716     /**
717      * Returns a sorted unmodifiable map of Procedures in this DataMap keyed by name.
718      */

719     public SortedMap JavaDoc getProcedureMap() {
720         return procedureMapRef;
721     }
722
723     /**
724      * Returns a parent namespace where this DataMap resides. Parent EntityNamespace is
725      * used to establish relationships with entities in other DataMaps.
726      *
727      * @since 1.1
728      */

729     public MappingNamespace getNamespace() {
730         return namespace;
731     }
732
733     /**
734      * Sets a parent namespace where this DataMap resides. Parent EntityNamespace is used
735      * to establish relationships with entities in other DataMaps.
736      *
737      * @since 1.1
738      */

739     public void setNamespace(MappingNamespace namespace) {
740         this.namespace = namespace;
741     }
742
743     /**
744      * @since 1.1
745      */

746     public int getDefaultLockType() {
747         return defaultLockType;
748     }
749
750     /**
751      * @since 1.1
752      */

753     public void setDefaultLockType(int defaultLockType) {
754         this.defaultLockType = defaultLockType;
755     }
756     
757     /**
758      * Returns default server package with ".client" suffix.
759      *
760      * @since 1.2
761      */

762     public String JavaDoc getDefaultClientPackage() {
763         return Util.isEmptyString(getDefaultPackage()) ? null : getDefaultPackage()
764                 + ".client";
765     }
766
767     /**
768      * @since 1.1
769      */

770     public String JavaDoc getDefaultPackage() {
771         return defaultPackage;
772     }
773
774     /**
775      * @since 1.1
776      */

777     public void setDefaultPackage(String JavaDoc defaultPackage) {
778         this.defaultPackage = defaultPackage;
779     }
780
781     /**
782      * @since 1.1
783      */

784     public String JavaDoc getDefaultSchema() {
785         return defaultSchema;
786     }
787
788     /**
789      * @since 1.1
790      */

791     public void setDefaultSchema(String JavaDoc defaultSchema) {
792         this.defaultSchema = defaultSchema;
793     }
794
795     /**
796      * @since 1.1
797      */

798     public String JavaDoc getDefaultSuperclass() {
799         return defaultSuperclass;
800     }
801
802     /**
803      * @since 1.1
804      */

805     public void setDefaultSuperclass(String JavaDoc defaultSuperclass) {
806         this.defaultSuperclass = defaultSuperclass;
807     }
808
809     /**
810      * DbEntity property changed.
811      * May be name, attribute or relationship added or removed, etc.
812      * Attribute and relationship property changes are handled in
813      * respective listeners.
814      * @since 1.2
815      */

816     public void dbEntityChanged(EntityEvent e){
817         Entity entity = e.getEntity();
818         if (entity instanceof DbEntity){
819             ((DbEntity)entity).dbEntityChanged(e);
820             
821             // finish up the name change here because we
822
// do not have direct access to the dbEntityMap
823
if (e.isNameChange()){
824                 // remove the entity from the map with the old name
825
dbEntityMap.remove(e.getOldName());
826
827                 // add the entity back in with the new name
828
dbEntityMap.put(e.getNewName(), entity);
829                 
830                 // important - clear parent namespace:
831
MappingNamespace ns = getNamespace();
832                 if (ns instanceof EntityResolver) {
833                     ((EntityResolver) ns).clearCache();
834                 }
835             }
836         }
837     }
838    
839     /** New entity has been created/added.*/
840     public void dbEntityAdded(EntityEvent e){
841         // does nothing currently
842
}
843     /** Entity has been removed.*/
844     public void dbEntityRemoved(EntityEvent e){
845         // does nothing currently
846
}
847
848     /** Attribute property changed. */
849     public void dbAttributeChanged(AttributeEvent e){
850         Entity entity = e.getEntity();
851         if (entity instanceof DbEntity){
852             ((DbEntity)entity).dbAttributeChanged(e);
853         }
854     }
855     
856     /** New attribute has been created/added.*/
857     public void dbAttributeAdded(AttributeEvent e){
858         // does nothing currently
859
}
860     
861     /** Attribute has been removed.*/
862     public void dbAttributeRemoved(AttributeEvent e){
863         // does nothing currently
864
}
865
866     /** Relationship property changed. */
867     public void dbRelationshipChanged(RelationshipEvent e){
868         Entity entity = e.getEntity();
869         if (entity instanceof DbEntity){
870             ((DbEntity)entity).dbRelationshipChanged(e);
871         }
872     }
873     
874     /** Relationship has been created/added.*/
875     public void dbRelationshipAdded(RelationshipEvent e){
876         // does nothing currently
877
}
878     
879     /** Relationship has been removed.*/
880     public void dbRelationshipRemoved(RelationshipEvent e){
881         // does nothing currently
882
}
883
884     /**
885      * ObjEntity property changed.
886      * May be name, attribute or relationship added or removed, etc.
887      * Attribute and relationship property changes are handled in
888      * respective listeners.
889      * @since 1.2
890      */

891     public void objEntityChanged(EntityEvent e){
892         Entity entity = e.getEntity();
893         if (entity instanceof ObjEntity){
894             ((ObjEntity)entity).objEntityChanged(e);
895
896             // finish up the name change here because we
897
// do not have direct access to the objEntityMap
898
if (e.isNameChange()){
899                 // remove the entity from the map with the old name
900
objEntityMap.remove(e.getOldName());
901
902                 // add the entity back in with the new name
903
objEntityMap.put(e.getNewName(), entity);
904                 
905                 // important - clear parent namespace:
906
MappingNamespace ns = getNamespace();
907                 if (ns instanceof EntityResolver) {
908                     ((EntityResolver) ns).clearCache();
909                 }
910             }
911         }
912     }
913     
914     /** New entity has been created/added.*/
915     public void objEntityAdded(EntityEvent e){
916         // does nothing currently
917
}
918     
919     /** Entity has been removed.*/
920     public void objEntityRemoved(EntityEvent e){
921         // does nothing currently
922
}
923
924     /** Attribute property changed. */
925     public void objAttributeChanged(AttributeEvent e){
926         // does nothing currently
927
}
928     
929     /** New attribute has been created/added.*/
930     public void objAttributeAdded(AttributeEvent e){
931         // does nothing currently
932
}
933     
934     /** Attribute has been removed.*/
935     public void objAttributeRemoved(AttributeEvent e){
936         // does nothing currently
937
}
938
939     /** Relationship property changed. */
940     public void objRelationshipChanged(RelationshipEvent e){
941         // does nothing currently
942
}
943
944     /** Relationship has been created/added. */
945     public void objRelationshipAdded(RelationshipEvent e){
946         // does nothing currently
947
}
948
949     /** Relationship has been removed. */
950     public void objRelationshipRemoved(RelationshipEvent e){
951         // does nothing currently
952
}
953 }
Popular Tags