KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.SortedMap JavaDoc;
63 import java.util.TreeMap JavaDoc;
64
65 import org.apache.commons.collections.Transformer;
66 import org.objectstyle.cayenne.CayenneRuntimeException;
67 import org.objectstyle.cayenne.exp.Expression;
68 import org.objectstyle.cayenne.exp.ExpressionException;
69 import org.objectstyle.cayenne.exp.ExpressionFactory;
70 import org.objectstyle.cayenne.map.event.AttributeEvent;
71 import org.objectstyle.cayenne.map.event.EntityEvent;
72 import org.objectstyle.cayenne.map.event.ObjAttributeListener;
73 import org.objectstyle.cayenne.map.event.ObjEntityListener;
74 import org.objectstyle.cayenne.map.event.ObjRelationshipListener;
75 import org.objectstyle.cayenne.map.event.RelationshipEvent;
76 import org.objectstyle.cayenne.util.Util;
77 import org.objectstyle.cayenne.util.XMLEncoder;
78
79 /**
80  * ObjEntity is a mapping descriptor for a DataObject Java class.
81  * It contains the information about the Java class itself, as well
82  * as its mapping to the DbEntity layer.
83  *
84  * @author Misha Shengaout
85  * @author Andrei Adamchik
86  */

87 public class ObjEntity extends Entity implements ObjEntityListener,
88                                                 ObjAttributeListener,
89                                                 ObjRelationshipListener {
90     final public static int LOCK_TYPE_NONE = 0;
91     final public static int LOCK_TYPE_OPTIMISTIC = 1;
92
93     protected String JavaDoc superClassName;
94     protected String JavaDoc className;
95     protected String JavaDoc dbEntityName;
96     protected String JavaDoc superEntityName;
97     protected Expression qualifier;
98     protected boolean readOnly;
99     protected int lockType = LOCK_TYPE_NONE;
100     
101     protected String JavaDoc clientClassName;
102     protected String JavaDoc clientSuperClassName;
103
104     public ObjEntity() {
105         super();
106     }
107
108     public ObjEntity(String JavaDoc name) {
109         this();
110         this.setName(name);
111     }
112
113     /**
114      * Prints itself as XML to the provided XMLEncoder.
115      *
116      * @since 1.1
117      */

118     public void encodeAsXML(XMLEncoder encoder) {
119         encoder.print("<obj-entity name=\"");
120         encoder.print(getName());
121
122         // additionally validate that superentity exists
123
if (getSuperEntityName() != null && getSuperEntity() != null) {
124             encoder.print("\" superEntityName=\"");
125             encoder.print(getSuperEntityName());
126         }
127
128         if (getClassName() != null) {
129             encoder.print("\" className=\"");
130             encoder.print(getClassName());
131         }
132         
133         if (getClientClassName() != null) {
134             encoder.print("\" clientClassName=\"");
135             encoder.print(getClientClassName());
136         }
137
138         if (isReadOnly()) {
139             encoder.print("\" readOnly=\"true");
140         }
141
142         if (getDeclaredLockType() == LOCK_TYPE_OPTIMISTIC) {
143             encoder.print("\" lock-type=\"optimistic");
144         }
145
146         if (getSuperEntityName() == null && getDbEntity() != null) {
147             encoder.print("\" dbEntityName=\"");
148             encoder.print(Util.encodeXmlAttribute(getDbEntityName()));
149         }
150
151         if (getSuperEntityName() == null && getSuperClassName() != null) {
152             encoder.print("\" superClassName=\"");
153             encoder.print(getSuperClassName());
154         }
155         
156         if (getSuperEntityName() == null && getClientSuperClassName() != null) {
157             encoder.print("\" clientSuperClassName=\"");
158             encoder.print(getClientSuperClassName());
159         }
160
161         encoder.println("\">");
162         encoder.indent(1);
163
164         if (qualifier != null) {
165             encoder.print("<qualifier>");
166             qualifier.encodeAsXML(encoder);
167             encoder.println("</qualifier>");
168         }
169
170         // store attributes
171
encoder.print(getDeclaredAttributes());
172
173         encoder.indent(-1);
174         encoder.println("</obj-entity>");
175     }
176
177     /**
178      * Returns Java class of persistent objects described by this entity.
179      * Casts any thrown exceptions into CayenneRuntimeException.
180      *
181      * @since 1.0.7
182      */

183     public Class JavaDoc getJavaClass(ClassLoader JavaDoc classLoader) {
184         if(this.getClassName() == null) {
185             return null;
186         }
187         
188         try {
189             // tolerate null class loader
190
if (classLoader == null) {
191                 return Class.forName(this.getClassName());
192             }
193             else {
194                 return classLoader.loadClass(this.getClassName());
195             }
196         }
197         catch (ClassNotFoundException JavaDoc e) {
198             throw new CayenneRuntimeException(
199                 "Failed to load class for name '"
200                     + this.getClassName()
201                     + "': "
202                     + e.getMessage(),
203                 e);
204         }
205     }
206
207     /**
208      * Returns the type of lock used by this ObjEntity. If this
209      * entity is not locked, this method would look in a super entity
210      * recursively, until it finds a lock somewhere in the inheritance
211      * hierarchy.
212      *
213      * @since 1.1
214      */

215     public int getLockType() {
216         // if this entity has an explicit lock,
217
// no need to lookup inheritance hierarchy
218
if (lockType != LOCK_TYPE_NONE) {
219             return lockType;
220         }
221
222         ObjEntity superEntity = getSuperEntity();
223         return (superEntity != null) ? superEntity.getLockType() : lockType;
224     }
225
226     /**
227      * Returns the type of lock used by this ObjEntity, regardless of
228      * what locking type is used by super entities.
229      *
230      * @since 1.1
231      */

232     public int getDeclaredLockType() {
233         return lockType;
234     }
235
236     /**
237      * Sets the type of lock used by this ObjEntity.
238      *
239      * @since 1.1
240      */

241     public void setDeclaredLockType(int i) {
242         lockType = i;
243     }
244
245     /**
246      * Returns a qualifier that imposes a restriction on what objects
247      * belong to this entity. Returned qualifier is the one declared
248      * in this entity, and does not include qualifiers declared in
249      * super entities.
250      *
251      * @since 1.1
252      */

253     public Expression getDeclaredQualifier() {
254         return qualifier;
255     }
256
257     /**
258      * Returns an entity name for a parent entity in the inheritance hierarchy.
259      *
260      * @since 1.1
261      */

262     public String JavaDoc getSuperEntityName() {
263         return superEntityName;
264     }
265
266     /**
267      * Sets a qualifier that imposes a limit on what objects
268      * belong to this entity.
269      *
270      * @since 1.1
271      */

272     public void setDeclaredQualifier(Expression qualifier) {
273         this.qualifier = qualifier;
274     }
275
276     /**
277      * Sets an entity name for a parent entity in the inheritance hierarchy.
278      *
279      * @since 1.1
280      */

281     public void setSuperEntityName(String JavaDoc superEntityName) {
282         this.superEntityName = superEntityName;
283     }
284
285     /**
286      * Returns the name of DataObject class described by this entity.
287      */

288     public String JavaDoc getClassName() {
289         return className;
290     }
291
292     /**
293      * Sets the name of the DataObject class described by this entity.
294      */

295     public void setClassName(String JavaDoc className) {
296         this.className = className;
297     }
298     
299
300     /**
301      * Returns the name of ClientDataObject class described by this entity.
302      *
303      * @since 1.2
304      */

305     public String JavaDoc getClientClassName() {
306         return clientClassName;
307     }
308
309     /**
310      * Sets the name of the ClientDataObject class described by this entity.
311      *
312      * @since 1.2
313      */

314     public void setClientClassName(String JavaDoc clientClassName) {
315         this.clientClassName = clientClassName;
316     }
317     
318
319     /**
320      * Returns a fully-qualified name of the super class of the DataObject class.
321      * This value is used as a hint for class generation. If the entity inherits from
322      * another entity, a superclass is the class of that entity.
323      */

324     public String JavaDoc getSuperClassName() {
325         ObjEntity superEntity = getSuperEntity();
326         return (superEntity != null) ? superEntity.getClassName() : superClassName;
327     }
328
329     /**
330      * Sets a fully-qualified name of the super class of the DataObject class.
331      * This value is used as a hint for class generation.
332      *
333      * <p><i>An attempt to set superclass on an inherited entity has no effect, since
334      * a class of the super entity is always used as a superclass.</i></p>
335      */

336     public void setSuperClassName(String JavaDoc superClassName) {
337         this.superClassName = superClassName;
338     }
339     
340     /**
341      * Returns a fully-qualified name of the client-side super class of the DataObject
342      * class. This value is used as a hint for class generation. If the entity inherits
343      * from another entity, a superclass is the class of that entity.
344      *
345      * @since 1.2
346      */

347     public String JavaDoc getClientSuperClassName() {
348         ObjEntity superEntity = getSuperEntity();
349         return (superEntity != null)
350                 ? superEntity.getClientSuperClassName()
351                 : clientSuperClassName;
352     }
353
354     /**
355      * Sets a fully-qualified name of the client-side super class of the ClientDataObject
356      * class. This value is used as a hint for class generation.
357      * <p>
358      * <i>An attempt to set superclass on an inherited entity has no effect, since a class
359      * of the super entity is always used as a superclass. </i>
360      * </p>
361      *
362      * @since 1.2
363      */

364     public void setClientSuperClassName(String JavaDoc clientSuperClassName) {
365         this.clientSuperClassName = clientSuperClassName;
366     }
367
368     /**
369      * Returns a "super" entity in the entity inheritance hierarchy.
370      *
371      * @since 1.1
372      */

373     public ObjEntity getSuperEntity() {
374         return (superEntityName != null)
375             ? getNonNullNamespace().getObjEntity(superEntityName)
376             : null;
377     }
378
379     /**
380      * Returns a DbEntity associated with this ObjEntity.
381      */

382     public DbEntity getDbEntity() {
383
384         // since 1.2 - allow overriding DbEntity in the inheritance hierarchy...
385
if (dbEntityName != null) {
386             return getNonNullNamespace().getDbEntity(dbEntityName);
387         }
388
389         ObjEntity superEntity = getSuperEntity();
390         if (superEntity != null) {
391             return superEntity.getDbEntity();
392         }
393
394         return null;
395     }
396
397     /**
398      * Sets the DbEntity used by this ObjEntity.
399      * <p>
400      * <i>Setting DbEntity on an inherited entity has no effect, since a class of the
401      * super entity is always used as a superclass. </i>
402      * </p>
403      */

404     public void setDbEntity(DbEntity dbEntity) {
405         this.dbEntityName = (dbEntity != null) ? dbEntity.getName() : null;
406     }
407
408     /**
409      * Returns a named attribute that either belongs to this ObjEntity or is
410      * inherited. Returns null if no matching attribute is found.
411      */

412     public Attribute getAttribute(String JavaDoc name) {
413         Attribute attribute = super.getAttribute(name);
414         if (attribute != null) {
415             return attribute;
416         }
417
418         if (superEntityName == null) {
419             return null;
420         }
421
422         ObjEntity superEntity = getSuperEntity();
423         return (superEntity != null) ? superEntity.getAttribute(name) : null;
424     }
425
426     /**
427      * Returns a SortedMap of all attributes that either belong to this ObjEntity
428      * or inherited.
429      */

430     public SortedMap JavaDoc getAttributeMap() {
431         if (superEntityName == null) {
432             return super.getAttributeMap();
433         }
434
435         SortedMap JavaDoc attributeMap = new TreeMap JavaDoc();
436         appendAttributes(attributeMap);
437         return attributeMap;
438     }
439
440     /**
441      * Recursively appends all attributes in the entity inheritance hierarchy.
442      */

443     final void appendAttributes(Map JavaDoc map) {
444         map.putAll(super.getAttributeMap());
445
446         ObjEntity superEntity = getSuperEntity();
447         if (superEntity != null) {
448             superEntity.appendAttributes(map);
449         }
450     }
451
452     /**
453      * Returns a Collection of all attributes that either belong to
454      * this ObjEntity or inherited.
455      */

456     public Collection JavaDoc getAttributes() {
457         if (superEntityName == null) {
458             return super.getAttributes();
459         }
460
461         return getAttributeMap().values();
462     }
463
464     /**
465      * Returns a Collection of all attributes that belong to
466      * this ObjEntity, excluding inherited attributes.
467      *
468      * @since 1.1
469      */

470     public Collection JavaDoc getDeclaredAttributes() {
471         return super.getAttributes();
472     }
473
474     /**
475      * Returns a named Relationship that either belongs to this ObjEntity or is
476      * inherited. Returns null if no matching attribute is found.
477      */

478     public Relationship getRelationship(String JavaDoc name) {
479         Relationship relationship = super.getRelationship(name);
480         if (relationship != null) {
481             return relationship;
482         }
483
484         if (superEntityName == null) {
485             return null;
486         }
487
488         ObjEntity superEntity = getSuperEntity();
489         return (superEntity != null) ? superEntity.getRelationship(name) : null;
490     }
491
492     public SortedMap JavaDoc getRelationshipMap() {
493         if (superEntityName == null) {
494             return super.getRelationshipMap();
495         }
496
497         SortedMap JavaDoc relationshipMap = new TreeMap JavaDoc();
498         appendRelationships(relationshipMap);
499         return relationshipMap;
500     }
501
502     /**
503      * Recursively appends all relationships in the entity inheritance hierarchy.
504      */

505     final void appendRelationships(Map JavaDoc map) {
506         map.putAll(super.getRelationshipMap());
507
508         ObjEntity superEntity = getSuperEntity();
509         if (superEntity != null) {
510             superEntity.appendRelationships(map);
511         }
512     }
513
514     public Collection JavaDoc getRelationships() {
515         if (superEntityName == null) {
516             return super.getRelationships();
517         }
518
519         return getRelationshipMap().values();
520     }
521
522     /**
523      * Returns a Collection of all relationships that belong to
524      * this ObjEntity, excluding inherited attributes.
525      *
526      * @since 1.1
527      */

528     public Collection JavaDoc getDeclaredRelationships() {
529         return super.getRelationships();
530     }
531
532     /**
533      * Returns ObjAttribute of this entity that maps to <code>dbAttribute</code>
534      * parameter. Returns null if no such attribute is found.
535      */

536     public ObjAttribute getAttributeForDbAttribute(DbAttribute dbAttribute) {
537         Iterator JavaDoc it = getAttributeMap().entrySet().iterator();
538         while (it.hasNext()) {
539             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
540             ObjAttribute objAttr = (ObjAttribute) entry.getValue();
541             if (objAttr.getDbAttribute() == dbAttribute)
542                 return objAttr;
543         }
544         return null;
545     }
546
547     /**
548      * Returns ObjRelationship of this entity that maps to
549      * <code>dbRelationship</code> parameter. Returns null if no
550      * such relationship is found.
551      */

552     public ObjRelationship getRelationshipForDbRelationship(DbRelationship dbRelationship) {
553         Iterator JavaDoc it = getRelationshipMap().entrySet().iterator();
554         while (it.hasNext()) {
555             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
556             ObjRelationship objRel = (ObjRelationship) entry.getValue();
557
558             List JavaDoc relList = objRel.getDbRelationships();
559             if (relList.size() != 1) {
560                 continue;
561             }
562
563             if (relList.get(0) == dbRelationship) {
564                 return objRel;
565             }
566         }
567         return null;
568     }
569
570     /**
571      * Clears all the mapping between this obj entity and its current db entity.
572      * Clears mapping between entities, attributes and relationships.
573      */

574     public void clearDbMapping() {
575         if (dbEntityName == null)
576             return;
577
578         Iterator JavaDoc it = getAttributeMap().values().iterator();
579         while (it.hasNext()) {
580             ObjAttribute objAttr = (ObjAttribute) it.next();
581             DbAttribute dbAttr = objAttr.getDbAttribute();
582             if (null != dbAttr) {
583                 objAttr.setDbAttribute(null);
584             }
585         }
586
587         Iterator JavaDoc rels = this.getRelationships().iterator();
588         while (rels.hasNext()) {
589             ((ObjRelationship) rels.next()).clearDbRelationships();
590         }
591
592         dbEntityName = null;
593     }
594
595     /**
596      * Returns <code>true</code> if this ObjEntity represents
597      * a set of read-only objects.
598      *
599      * @return boolean
600      */

601     public boolean isReadOnly() {
602         return readOnly;
603     }
604
605     public void setReadOnly(boolean readOnly) {
606         this.readOnly = readOnly;
607     }
608
609     /**
610      * Returns true if this entity directly or indirectly inherits
611      * from a given entity, false otherwise.
612      *
613      * @since 1.1
614      */

615     public boolean isSubentityOf(ObjEntity entity) {
616         if (entity == null) {
617             return false;
618         }
619
620         if (entity == this) {
621             return false;
622         }
623
624         ObjEntity superEntity = getSuperEntity();
625         if (superEntity == entity) {
626             return true;
627         }
628
629         return (superEntity != null) ? superEntity.isSubentityOf(entity) : false;
630     }
631
632     public Iterator JavaDoc resolvePathComponents(Expression pathExp)
633         throws ExpressionException {
634
635         // resolve DB_PATH if we can
636
if (pathExp.getType() == Expression.DB_PATH) {
637             if (getDbEntity() == null) {
638                 throw new ExpressionException(
639                     "Can't resolve DB_PATH '" + pathExp + "', DbEntity is not set.");
640             }
641
642             return getDbEntity().resolvePathComponents(pathExp);
643         }
644
645         if (pathExp.getType() == Expression.OBJ_PATH) {
646             return new PathIterator((String JavaDoc) pathExp.getOperand(0));
647         }
648
649         throw new ExpressionException(
650             "Invalid expression type: '"
651                 + pathExp.expName()
652                 + "', OBJ_PATH is expected.");
653     }
654
655     /**
656      * Transforms an Expression to an analogous expression in terms of the underlying
657      * DbEntity.
658      *
659      * @since 1.1
660      */

661     public Expression translateToDbPath(Expression expression) {
662
663         if (expression == null) {
664             return null;
665         }
666
667         if (getDbEntity() == null) {
668             throw new CayenneRuntimeException(
669                 "Can't translate expression to DB_PATH, no DbEntity for '"
670                     + getName()
671                     + "'.");
672         }
673
674         // converts all OBJ_PATH expressions to DB_PATH expressions
675
// and pass control to the DB entity
676
return expression.transform(new DBPathConverter());
677     }
678
679     /**
680      * Transforms an Expression rooted in this entity to an analogous expression
681      * rooted in related entity.
682      *
683      * @since 1.1
684      */

685     public Expression translateToRelatedEntity(
686         Expression expression,
687         String JavaDoc relationshipPath) {
688
689         if (expression == null) {
690             return null;
691         }
692
693         if (relationshipPath == null) {
694             return expression;
695         }
696
697         if (getDbEntity() == null) {
698             throw new CayenneRuntimeException(
699                 "Can't transform expression, no DbEntity for '" + getName() + "'.");
700         }
701
702         // converts all OBJ_PATH expressions to DB_PATH expressions
703
// and pass control to the DB entity
704

705         DBPathConverter transformer = new DBPathConverter();
706
707         String JavaDoc dbPath = transformer.toDbPath(resolvePathComponents(relationshipPath));
708         Expression dbClone = expression.transform(transformer);
709
710         return getDbEntity().translateToRelatedEntity(dbClone, dbPath);
711     }
712
713     final class DBPathConverter implements Transformer {
714         // TODO: make it a public method - resolveDBPathComponents or something...
715
// seems generally useful
716
String JavaDoc toDbPath(Iterator JavaDoc objectPathComponents) {
717             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
718             while (objectPathComponents.hasNext()) {
719                 Object JavaDoc component = objectPathComponents.next();
720
721                 Iterator JavaDoc dbSubpath;
722
723                 if (component instanceof ObjRelationship) {
724                     dbSubpath =
725                         ((ObjRelationship) component).getDbRelationships().iterator();
726                 }
727                 else if (component instanceof ObjAttribute) {
728                     dbSubpath = ((ObjAttribute) component).getDbPathIterator();
729                 }
730                 else {
731                     throw new CayenneRuntimeException(
732                         "Unknown path component: " + component);
733                 }
734
735                 while (dbSubpath.hasNext()) {
736                     MapObject subComponent = (MapObject) dbSubpath.next();
737                     if (buf.length() > 0) {
738                         buf.append(Entity.PATH_SEPARATOR);
739                     }
740
741                     buf.append(subComponent.getName());
742                 }
743             }
744
745             return buf.toString();
746         }
747
748         public Object JavaDoc transform(Object JavaDoc input) {
749
750             if (!(input instanceof Expression)) {
751                 return input;
752             }
753
754             Expression expression = (Expression) input;
755
756             if (expression.getType() != Expression.OBJ_PATH) {
757                 return input;
758             }
759
760             // convert obj_path to db_path
761

762             String JavaDoc converted = toDbPath(resolvePathComponents(expression));
763             Expression exp = ExpressionFactory.expressionOfType(Expression.DB_PATH);
764             exp.setOperand(0, converted);
765             return exp;
766         }
767     }
768
769     /**
770      * Returns the name of the underlying DbEntity.
771      *
772      * @since 1.1
773      */

774     public String JavaDoc getDbEntityName() {
775         return dbEntityName;
776     }
777
778     /**
779      * Sets the name of underlying DbEntity.
780      *
781      * @since 1.1
782      */

783     public void setDbEntityName(String JavaDoc string) {
784         dbEntityName = string;
785     }
786     
787     /**
788      * ObjEntity property changed.
789      * May be name, attribute or relationship added or removed, etc.
790      * Attribute and relationship property changes are handled in
791      * respective listeners.
792      * @since 1.2
793      */

794     public void objEntityChanged(EntityEvent e){
795         if ((e == null) || (e.getEntity() != this)) {
796             // not our concern
797
return;
798         }
799         
800         // handle entity name changes
801
if (e.getId() == EntityEvent.CHANGE && e.isNameChange()){
802             String JavaDoc oldName = e.getOldName();
803             String JavaDoc newName = e.getNewName();
804             
805             DataMap map = getDataMap();
806             if (map != null) {
807                ObjEntity oe = (ObjEntity) e.getEntity();
808                Iterator JavaDoc rit = oe.getRelationships().iterator();
809                while (rit.hasNext()){
810                    ObjRelationship or = (ObjRelationship) rit.next();
811                    or = or.getReverseRelationship();
812                    if (null != or && or.targetEntityName.equals(oldName)){
813                        or.targetEntityName = newName;
814                    }
815                }
816             }
817         }
818     }
819     
820     /** New entity has been created/added.*/
821     public void objEntityAdded(EntityEvent e){
822         // does nothing currently
823
}
824     
825     /** Entity has been removed.*/
826     public void objEntityRemoved(EntityEvent e){
827         // does nothing currently
828
}
829
830     /** Attribute property changed. */
831     public void objAttributeChanged(AttributeEvent e){
832         // does nothing currently
833
}
834     
835     /** New attribute has been created/added.*/
836     public void objAttributeAdded(AttributeEvent e){
837         // does nothing currently
838
}
839     
840     /** Attribute has been removed.*/
841     public void objAttributeRemoved(AttributeEvent e){
842         // does nothing currently
843
}
844
845     /** Relationship property changed. */
846     public void objRelationshipChanged(RelationshipEvent e){
847         // does nothing currently
848
}
849
850     /** Relationship has been created/added. */
851     public void objRelationshipAdded(RelationshipEvent e){
852         // does nothing currently
853
}
854
855     /** Relationship has been removed. */
856     public void objRelationshipRemoved(RelationshipEvent e){
857         // does nothing currently
858
}
859     
860 }
861
Popular Tags