KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > metadata > MetadataDescriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.metadata;
23
24 import java.lang.reflect.Type JavaDoc;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 // WIP - should use the validator.
33
import oracle.toplink.essentials.exceptions.ValidationException;
34
35 import oracle.toplink.essentials.descriptors.ClassDescriptor;
36 import oracle.toplink.essentials.descriptors.RelationalDescriptor;
37 import oracle.toplink.essentials.descriptors.VersionLockingPolicy;
38
39 import oracle.toplink.essentials.internal.ejb.cmp3.base.CMP3Policy;
40
41 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor;
42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.MetadataAccessor;
43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.RelationshipAccessor;
44
45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataColumn;
46 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns;
47
48 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners.MetadataEntityListener;
49
50 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLConstants;
51
52 import oracle.toplink.essentials.internal.helper.DatabaseField;
53 import oracle.toplink.essentials.internal.helper.DatabaseTable;
54
55 import oracle.toplink.essentials.mappings.DatabaseMapping;
56
57 /**
58  * Common metatata descriptor for the annotation and xml processors. This class
59  * is a wrap on an actual TopLink descriptor.
60  *
61  * @author Guy Pelletier
62  * @since TopLink EJB 3.0 Reference Implementation
63  */

64 public class MetadataDescriptor {
65     protected ClassAccessor m_accessor;
66     protected ClassDescriptor m_descriptor;
67     
68     protected Boolean JavaDoc m_usesPropertyAccess;
69     
70     protected boolean m_ignoreIDs;
71     protected boolean m_ignoreTables;
72     protected boolean m_isCascadePersist;
73     protected boolean m_ignoreAnnotations; // XML metadata complete
74
protected boolean m_ignoreInheritance;
75     protected boolean m_usesSingleTableInheritanceStrategy;
76     
77     protected String JavaDoc m_xmlSchema; // XML metadata
78
protected String JavaDoc m_xmlCatalog; // XML metadata
79
protected String JavaDoc m_xmlAccess; // XML metadata
80
protected String JavaDoc m_primaryTableName;
81     protected String JavaDoc m_embeddedIdAttributeName;
82     
83     protected List JavaDoc<String JavaDoc> m_idAttributeNames;
84     protected List JavaDoc<String JavaDoc> m_orderByAttributeNames;
85     protected List JavaDoc<String JavaDoc> m_idOrderByAttributeNames;
86     protected List JavaDoc<MetadataDescriptor> m_aggregateDescriptors;
87     
88     protected Map JavaDoc<String JavaDoc, Type JavaDoc> m_pkClassIDs;
89     protected Map JavaDoc<String JavaDoc, MetadataAccessor> m_accessors;
90     protected Map JavaDoc<String JavaDoc, MetadataColumn> m_attributeOverrides;
91     protected Map JavaDoc<String JavaDoc, MetadataJoinColumns> m_associationOverrides;
92     protected Map JavaDoc<String JavaDoc, MetadataAccessor> m_manyToManyAccessors;
93     protected Map JavaDoc<String JavaDoc, RelationshipAccessor> m_relationshipAccessors;
94     
95     /**
96      * INTERNAL:
97      */

98     public MetadataDescriptor(Class JavaDoc javaClass) {
99         init();
100         m_descriptor = new RelationalDescriptor();
101         m_descriptor.setExistenceChecking("Check database");
102         m_descriptor.setAlias("");
103         setJavaClass(javaClass);
104     }
105      
106      /**
107       * INTERNAL:
108       */

109     public void addAccessor(MetadataAccessor accessor) {
110         m_accessors.put(accessor.getAttributeName(), accessor);
111          
112         if (accessor.isRelationship()) {
113             m_relationshipAccessors.put(accessor.getAttributeName(), (RelationshipAccessor) accessor);
114         }
115          
116         // Store ManyToMany relationships so that we may look at attribute
117
// names when defaulting join columns for bi and uni directional M-M's.
118
if (accessor.isManyToMany()) {
119             m_manyToManyAccessors.put(accessor.getReferenceClassName(), accessor);
120         }
121     }
122      
123     /**
124      * INTERNAL:
125      */

126     public void addAggregateDescriptor(MetadataDescriptor aggregateDescriptor) {
127         m_aggregateDescriptors.add(aggregateDescriptor);
128     }
129     
130     /**
131      * INTERNAL:
132      */

133      public void addAssociationOverride(String JavaDoc attributeName, MetadataJoinColumns joinColumns) {
134         m_associationOverrides.put(attributeName, joinColumns);
135      }
136     
137     /**
138      * INTERNAL:
139      */

140     public void addAttributeOverride(MetadataColumn column) {
141         m_attributeOverrides.put(column.getAttributeName(), column);
142     }
143
144     /**
145      * INTERNAL:
146      */

147     public void addClassIndicator(Class JavaDoc entityClass, String JavaDoc value) {
148         if (m_accessor.isInheritanceSubclass()) {
149             m_accessor.getInheritanceRootDescriptor().addClassIndicator(entityClass, value);
150         } else {
151             m_descriptor.getInheritancePolicy().addClassNameIndicator(entityClass.getName(), value);
152         }
153     }
154     
155     /**
156      * INTERNAL:
157      */

158     public void addDefaultEventListener(MetadataEntityListener listener) {
159         m_descriptor.getEventManager().addDefaultEventListener(listener);
160     }
161     
162     /**
163      * INTERNAL:
164      */

165     public void addEntityListenerEventListener(MetadataEntityListener listener) {
166         m_descriptor.getEventManager().addEntityListenerEventListener(listener);
167     }
168     
169     /**
170      * INTERNAL:
171      */

172     public void addIdAttributeName(String JavaDoc idAttributeName) {
173         m_idAttributeNames.add(idAttributeName);
174     }
175     
176     /**
177      * INTERNAL:
178      */

179     public void addMapping(DatabaseMapping mapping) {
180         m_descriptor.addMapping(mapping);
181     }
182     
183     /**
184      * INTERNAL:
185      */

186     public void addMultipleTableForeignKeyField(DatabaseField pkField, DatabaseField fkField) {
187         m_descriptor.addMultipleTableForeignKeyField(pkField, fkField);
188     }
189     
190     /**
191      * INTERNAL:
192      */

193     public void addMultipleTablePrimaryKeyField(DatabaseField pkField, DatabaseField fkField) {
194         m_descriptor.addMultipleTablePrimaryKeyField(pkField, fkField);
195     }
196     
197     /**
198      * INTERNAL:
199      * We store these to validate the primary class when processing
200      * the entity class.
201      */

202     public void addPKClassId(String JavaDoc attributeName, Type JavaDoc type) {
203         m_pkClassIDs.put(attributeName, type);
204     }
205     
206     /**
207      * INTERNAL:
208      */

209     public void addPrimaryKeyField(DatabaseField field) {
210         m_descriptor.addPrimaryKeyField(field);
211     }
212     
213     /**
214      * INTERNAL:
215      */

216     public void addTable(DatabaseTable table) {
217         m_descriptor.addTable(table);
218     }
219     
220     /**
221      * INTERNAL:
222      */

223     public boolean excludeSuperclassListeners() {
224         return m_descriptor.getEventManager().excludeSuperclassListeners();
225     }
226     
227     /**
228      * INTERNAL:
229      * This method will first check for an accessor with name equal to
230      * fieldOrPropertyName (that is, assumes it is a field name). If no accessor
231      * is found than it assumes fieldOrPropertyName is a property name and
232      * converts it to its corresponding field name and looks for the accessor
233      * again. If still no accessor is found and this descriptor metadata is
234      * and an inheritance sublcass, than it will then look on the root metadata
235      * descriptor. Null is returned otherwise.
236      */

237     public MetadataAccessor getAccessorFor(String JavaDoc fieldOrPropertyName) {
238         MetadataAccessor accessor = m_accessors.get(fieldOrPropertyName);
239         
240         if (accessor == null) {
241             // Perhaps we have a property name ...
242
accessor = m_accessors.get(MetadataHelper.getAttributeNameFromMethodName(fieldOrPropertyName));
243            
244             // If still no accessor and we are an inheritance subclass, check
245
// the root descriptor now.
246
if (accessor == null && m_accessor.isInheritanceSubclass()) {
247                 accessor = m_accessor.getInheritanceRootDescriptor().getAccessorFor(fieldOrPropertyName);
248             }
249         }
250         
251         return accessor;
252     }
253     
254     /**
255      * INTERNAL:
256      */

257     public String JavaDoc getAlias() {
258         return m_descriptor.getAlias();
259     }
260     
261     /**
262      * INTERNAL:
263      */

264     public MetadataJoinColumns getAssociationOverrideFor(String JavaDoc attributeName) {
265         return m_associationOverrides.get(attributeName);
266     }
267     
268     /**
269      * INTERNAL:
270      */

271     public MetadataColumn getAttributeOverrideFor(String JavaDoc attributeName) {
272         return m_attributeOverrides.get(attributeName);
273     }
274
275     /**
276      * INTERNAL:
277      */

278     public DatabaseField getClassIndicatorField() {
279         if (m_accessor.getInheritanceRootDescriptor() != null) {
280             return m_accessor.getInheritanceRootDescriptor().getDescriptor().getInheritancePolicy().getClassIndicatorField();
281         } else {
282             if (getDescriptor().hasInheritance()) {
283                 return getDescriptor().getInheritancePolicy().getClassIndicatorField();
284             } else {
285                 return null;
286             }
287         }
288     }
289     
290     /**
291      * INTERNAL:
292      * The default table name is the descriptor alias, unless this descriptor
293      * metadata is an inheritance subclass with a SINGLE_TABLE strategy. Then
294      * it is the table name of the root descriptor metadata.
295      */

296     public String JavaDoc getDefaultTableName() {
297         String JavaDoc defaultTableName = getAlias().toUpperCase();
298         
299         if (m_accessor.isInheritanceSubclass()) {
300             if (m_accessor.getInheritanceRootDescriptor().usesSingleTableInheritanceStrategy()) {
301                 defaultTableName = m_accessor.getInheritanceRootDescriptor().getPrimaryTableName();
302             }
303         }
304         
305         return defaultTableName;
306     }
307     
308     /**
309      * INTERNAL:
310      */

311     public ClassDescriptor getDescriptor() {
312         return m_descriptor;
313     }
314     
315     /**
316      * INTERNAL:
317      */

318     public String JavaDoc getCatalog() {
319         return m_xmlCatalog;
320     }
321     
322     /**
323      * INTERNAL:
324      */

325     public ClassAccessor getClassAccessor() {
326         return m_accessor;
327     }
328     
329     /**
330      * INTERNAL:
331      */

332     public String JavaDoc getEmbeddedIdAttributeName() {
333         return m_embeddedIdAttributeName;
334     }
335     
336     /**
337      * INTERNAL:
338      * Return the primary key attribute name for this entity.
339      */

340     public String JavaDoc getIdAttributeName() {
341         if (getIdAttributeNames().isEmpty()) {
342             if (m_accessor.isInheritanceSubclass()) {
343                 return m_accessor.getInheritanceRootDescriptor().getIdAttributeName();
344             } else {
345                 return "";
346             }
347         } else {
348             return (String JavaDoc) getIdAttributeNames().get(0);
349         }
350     }
351     
352     /**
353      * INTERNAL:
354      * Return the id attribute names declared on this descriptor metadata.
355      */

356     public List JavaDoc<String JavaDoc> getIdAttributeNames() {
357         return m_idAttributeNames;
358     }
359     
360     /**
361      * INTERNAL:
362      * Return the primary key attribute names for this entity. If there are no
363      * id attribute names set then we are either:
364      * 1) an inheritance subclass, get the id attribute names from the root
365      * of the inheritance structure.
366      * 2) we have an embedded id. Get the id attribute names from the embedded
367      * descriptor metadata, which is equal the attribute names of all the
368      * direct to field mappings on that descriptor metadata. Currently does
369      * not traverse nested embeddables.
370      */

371     public List JavaDoc<String JavaDoc> getIdOrderByAttributeNames() {
372         if (m_idOrderByAttributeNames.isEmpty()) {
373             if (m_idAttributeNames.isEmpty()) {
374                 if (m_accessor.isInheritanceSubclass()) {
375                     // Get the id attribute names from our root parent.
376
m_idOrderByAttributeNames = m_accessor.getInheritanceRootDescriptor().getIdAttributeNames();
377                 } else {
378                     // We must have a composite primary key as a result of an embedded id.
379
m_idOrderByAttributeNames = getAccessorFor(getEmbeddedIdAttributeName()).getReferenceDescriptor().getOrderByAttributeNames();
380                 }
381             } else {
382                 m_idOrderByAttributeNames = m_idAttributeNames;
383             }
384         }
385             
386         return m_idOrderByAttributeNames;
387     }
388     
389     
390     /**
391      * INTERNAL:
392      */

393     public MetadataAccessor getManyToManyAccessor(String JavaDoc className) {
394         return m_manyToManyAccessors.get(className);
395     }
396     
397     /**
398      * INTERNAL:
399      * This will return the attribute names for all the direct to field mappings
400      * on this descriptor metadata. This method will typically be called when an
401      * @Embedded or @EmbeddedId attribute has been specified in an @OrderBy.
402      */

403     public List JavaDoc<String JavaDoc> getOrderByAttributeNames() {
404         if (m_orderByAttributeNames.isEmpty()) {
405             for (DatabaseMapping mapping : getMappings()) {
406                 if (mapping.isDirectToFieldMapping()) {
407                     m_orderByAttributeNames.add(mapping.getAttributeName());
408                 }
409             }
410         }
411         
412         return m_orderByAttributeNames;
413     }
414
415     /**
416      * INTERNAL:
417      */

418     public Class JavaDoc getJavaClass() {
419         return m_descriptor.getJavaClass();
420     }
421     
422     /**
423      * INTERNAL:
424      */

425     public String JavaDoc getJavaClassName() {
426         return m_descriptor.getJavaClassName();
427     }
428     
429     /**
430      * INTERNAL:
431      */

432     public DatabaseMapping getMappingForAttributeName(String JavaDoc attributeName) {
433         MetadataAccessor accessor = (MetadataAccessor) getAccessorFor(attributeName);
434         
435         if (accessor != null) {
436             // If the accessor is a relationship accessor than it may or may
437
// not have been processed yet. Fast track its processing if it
438
// needs to be. The process call will do nothing if it has already
439
// been processed.
440
if (accessor.isRelationship()) {
441                 ((RelationshipAccessor) accessor).processRelationship();
442             }
443             
444             return m_descriptor.getMappingForAttributeName(attributeName);
445         }
446         
447         // We didn't find a mapping on this descriptor, check our aggregate
448
// descriptors now.
449
for (MetadataDescriptor aggregateDmd : m_aggregateDescriptors) {
450             DatabaseMapping mapping = aggregateDmd.getMappingForAttributeName(attributeName);
451             
452             if (mapping != null) {
453                 return mapping;
454             }
455         }
456         
457         // We didn't find a mapping on the aggregate descriptors. If we are an
458
// inheritance subclass, check for a mapping on the inheritance root
459
// descriptor metadata.
460
if (m_accessor.isInheritanceSubclass()) {
461             return m_accessor.getInheritanceRootDescriptor().getMappingForAttributeName(attributeName);
462         }
463         
464         // Found nothing ... return null.
465
return null;
466     }
467     
468     /**
469      * INTERNAL:
470      */

471     public List JavaDoc<DatabaseMapping> getMappings() {
472         return m_descriptor.getMappings();
473     }
474     
475     /**
476      * INTERNAL:
477      */

478     public String JavaDoc getPKClassName() {
479         String JavaDoc pkClassName = null;
480         
481         if (m_descriptor.hasCMPPolicy()) {
482             pkClassName = ((CMP3Policy) m_descriptor.getCMPPolicy()).getPKClassName();
483         }
484         
485         return pkClassName;
486     }
487     
488     /**
489      * INTERNAL:
490      * Method to return the primary key field for the given descriptor
491      * metadata. Assumes there is one and only one.
492      */

493     public String JavaDoc getPrimaryKeyFieldName() {
494         return ((DatabaseField)(getPrimaryKeyFields().iterator().next())).getName();
495     }
496     
497     /**
498      * INTERNAL:
499      * Return the primary key fields for this descriptor metadata. If this is
500      * an inheritance subclass and it has no primary key fields, then grab the
501      * primary key fields from the root.
502      */

503     public List JavaDoc getPrimaryKeyFields() {
504         List JavaDoc primaryKeyFields = m_descriptor.getPrimaryKeyFields();
505         
506         if (primaryKeyFields.isEmpty() && m_accessor.isInheritanceSubclass()) {
507             primaryKeyFields = m_accessor.getInheritanceRootDescriptor().getPrimaryKeyFields();
508         }
509         
510         return primaryKeyFields;
511     }
512     
513     /**
514      * INTERNAL:
515      */

516     public String JavaDoc getPrimaryTableName() {
517         if (m_primaryTableName == null && m_accessor.isInheritanceSubclass()) {
518             return m_accessor.getInheritanceRootDescriptor().getPrimaryTableName();
519         } else {
520             if (m_descriptor.isAggregateDescriptor()) {
521                 // Aggregate descriptors don't have table names.
522
return "";
523             }
524             
525             return m_primaryTableName;
526         }
527     }
528     
529     /**
530      * INTERNAL:
531      */

532     public Collection JavaDoc<RelationshipAccessor> getRelationshipAccessors() {
533         return m_relationshipAccessors.values();
534     }
535     
536     /**
537      * INTERNAL:
538      */

539     public String JavaDoc getSchema() {
540         return m_xmlSchema;
541     }
542     
543     /**
544      * INTERNAL:
545      */

546     public DatabaseField getSequenceNumberField() {
547         return m_descriptor.getSequenceNumberField();
548     }
549     
550     /**
551      * INTERNAL:
552      */

553     public boolean hasAssociationOverrideFor(String JavaDoc attributeName) {
554         return m_associationOverrides.containsKey(attributeName);
555     }
556     
557     /**
558      * INTERNAL:
559      */

560     public boolean hasAttributeOverrideFor(String JavaDoc attributeName) {
561         return m_attributeOverrides.containsKey(attributeName);
562     }
563     
564     /**
565      * INTERNAL:
566      */

567     public boolean hasCompositePrimaryKey() {
568         return getPrimaryKeyFields().size() > 1 || getPKClassName() != null;
569     }
570     
571     /**
572      * INTERNAL:
573      */

574     public boolean hasEmbeddedIdAttribute() {
575         return m_embeddedIdAttributeName != null;
576     }
577     
578     /**
579      * INTERNAL:
580      */

581     public boolean hasInheritance() {
582         return m_descriptor.hasInheritance();
583     }
584     
585     /**
586      * INTERNAL:
587      */

588     public boolean hasManyToManyAccessorFor(String JavaDoc className) {
589         return m_manyToManyAccessors.containsKey(className);
590     }
591
592     /**
593      * INTERNAL:
594      */

595     public boolean hasMappingForAttributeName(String JavaDoc attributeName) {
596         return m_descriptor.getMappingForAttributeName(attributeName) != null;
597     }
598     
599     /**
600      * INTERNAL:
601      * Return true is the descriptor has primary key fields set.
602      */

603     public boolean hasPrimaryKeyFields() {
604         return m_descriptor.getPrimaryKeyFields().size() > 0;
605     }
606     
607     /**
608      * INTERNAL:
609      */

610     public boolean ignoreIDs() {
611         return m_ignoreIDs;
612     }
613     
614     /**
615      * INTERNAL:
616      */

617     public boolean ignoreInheritance() {
618         return m_ignoreInheritance;
619     }
620     
621     /**
622      * INTERNAL:
623      */

624     public boolean ignoreTables() {
625         return m_ignoreTables;
626     }
627     
628     /**
629      * INTERNAL:
630      */

631     private void init() {
632         m_xmlAccess = "";
633         m_xmlSchema = "";
634         m_xmlCatalog = "";
635         
636         m_ignoreIDs = false;
637         m_ignoreTables = false;
638         m_ignoreInheritance = false;
639         m_isCascadePersist = false;
640         m_ignoreAnnotations = false;
641         
642         m_idAttributeNames = new ArrayList JavaDoc<String JavaDoc>();
643         m_orderByAttributeNames = new ArrayList JavaDoc<String JavaDoc>();
644         m_idOrderByAttributeNames = new ArrayList JavaDoc<String JavaDoc>();
645         m_aggregateDescriptors = new ArrayList JavaDoc<MetadataDescriptor>();
646         
647         m_pkClassIDs = new HashMap JavaDoc<String JavaDoc, Type JavaDoc>();
648         m_accessors = new HashMap JavaDoc<String JavaDoc, MetadataAccessor>();
649         m_attributeOverrides = new HashMap JavaDoc<String JavaDoc, MetadataColumn>();
650         m_manyToManyAccessors = new HashMap JavaDoc<String JavaDoc, MetadataAccessor>();
651         m_associationOverrides = new HashMap JavaDoc<String JavaDoc, MetadataJoinColumns>();
652         m_relationshipAccessors = new HashMap JavaDoc<String JavaDoc, RelationshipAccessor>();
653     }
654     
655     /**
656      * INTERNAL:
657      * Indicates that cascade-persist should be applied to all relationship
658      * mappings for this entity.
659      */

660     public boolean isCascadePersist() {
661         return m_isCascadePersist;
662     }
663     
664     /**
665      * INTERNAL:
666      * Indicates that we found an XML field access type for this metadata
667      * descriptor.
668      */

669     public boolean isXmlFieldAccess() {
670         return m_xmlAccess.equals(XMLConstants.FIELD);
671     }
672     
673     /**
674      * INTERNAL:
675      * Indicates that we found an XML property access type for this metadata
676      * descriptor.
677      */

678     public boolean isXmlPropertyAccess() {
679         return m_xmlAccess.equals(XMLConstants.PROPERTY);
680     }
681     
682     /**
683      * INTERNAL:
684      */

685     public boolean pkClassWasNotValidated() {
686         return ! m_pkClassIDs.isEmpty();
687     }
688
689     /**
690      * INTERNAL:
691      */

692     public void setAccess(String JavaDoc access) {
693         m_xmlAccess = access;
694     }
695     
696     /**
697      * INTERNAL:
698      */

699     public void setAlias(String JavaDoc alias) {
700         m_descriptor.setAlias(alias);
701     }
702     
703     /**
704      * INTERNAL:
705      */

706     public void setCatalog(String JavaDoc xmlCatalog) {
707         m_xmlCatalog = xmlCatalog;
708     }
709
710     /**
711      * INTERNAL:
712      */

713     public void setClassAccessor(ClassAccessor accessor) {
714         m_accessor = accessor;
715     }
716     
717     /**
718      * INTERNAL:
719      */

720     public void setClassIndicatorField(DatabaseField field) {
721         m_descriptor.getInheritancePolicy().setClassIndicatorField(field);
722     }
723     
724     /**
725      * INTERNAL:
726      */

727     public void setDescriptor(ClassDescriptor descriptor) {
728         m_descriptor = descriptor;
729     }
730     
731     /**
732      * INTERNAL:
733      */

734     public void setDescriptorIsEmbeddable() {
735         m_descriptor.descriptorIsAggregate();
736     }
737     
738     /**
739      * INTERNAL:
740      */

741     public void setEmbeddedIdAttributeName(String JavaDoc embeddedIdAttributeName) {
742         m_embeddedIdAttributeName = embeddedIdAttributeName;
743     }
744     
745     /**
746      * INTERNAL:
747      */

748     public void setEntityEventListener(MetadataEntityListener listener) {
749         m_descriptor.getEventManager().setEntityEventListener(listener);
750     }
751     
752     /**
753      * INTERNAL:
754      */

755     public void setExcludeDefaultListeners(boolean excludeDefaultListeners) {
756         m_descriptor.getEventManager().setExcludeDefaultListeners(excludeDefaultListeners);
757     }
758     
759     /**
760      * INTERNAL:
761      */

762     public void setExcludeSuperclassListeners(boolean excludeSuperclassListeners) {
763         m_descriptor.getEventManager().setExcludeSuperclassListeners(excludeSuperclassListeners);
764     }
765     
766     /**
767      * INTERNAL:
768      */

769     public void setIgnoreFlags() {
770         m_ignoreInheritance = m_descriptor.hasInheritance();
771         m_ignoreTables = m_descriptor.getTableNames().size() > 0;
772         m_ignoreIDs = m_descriptor.getPrimaryKeyFieldNames().size() > 0;
773     }
774     
775     /**
776      * INTERNAL:
777      * Stored on the root class of an inheritance hierarchy.
778      */

779     public void setInheritanceStrategy(String JavaDoc inheritanceStrategy) {
780         if (inheritanceStrategy.equals(MetadataConstants.TABLE_PER_CLASS)) {
781             throw ValidationException.tablePerClassInheritanceNotSupported(getJavaClass());
782         }
783         
784         m_usesSingleTableInheritanceStrategy = (inheritanceStrategy.equals(MetadataConstants.SINGLE_TABLE));
785     }
786     
787     /**
788      * INTERNAL:
789      * Indicates that cascade-persist should be added to the set of cascade
790      * values for all relationship mappings.
791      */

792     public void setIsCascadePersist(boolean isCascadePersist) {
793         m_isCascadePersist = isCascadePersist;
794     }
795
796     /**
797      * INTERNAL:
798      * Used to set this descriptors java class.
799      */

800     public void setJavaClass(Class JavaDoc javaClass) {
801         m_descriptor.setJavaClass(javaClass);
802         m_descriptor.setJavaClassName(javaClass.getName());
803     }
804     
805     /**
806      * INTERNAL:
807      */

808     public void setOptimisticLockingPolicy(VersionLockingPolicy policy) {
809         policy.storeInObject();
810         m_descriptor.setOptimisticLockingPolicy(policy);
811     }
812     
813     /**
814      * INTERNAL:
815      * Set the inheritance parent class for this class.
816      */

817     public void setParentClass(Class JavaDoc parent) {
818         m_descriptor.getInheritancePolicy().setParentClassName(parent.getName());
819     }
820     
821     /**
822      * INTERNAL:
823      */

824     public void setPKClass(Class JavaDoc pkClass) {
825         setPKClass(pkClass.getName());
826     }
827     
828     /**
829      * INTERNAL:
830      */

831     public void setPKClass(String JavaDoc pkClassName) {
832         CMP3Policy policy = new CMP3Policy();
833         policy.setPrimaryKeyClassName(pkClassName);
834         m_descriptor.setCMPPolicy(policy);
835     }
836     
837     /**
838      * INTERNAL:
839      */

840     public void setPrimaryTable(DatabaseTable primaryTable) {
841         addTable(primaryTable);
842         
843         m_primaryTableName = primaryTable.getQualifiedName();
844     }
845
846     /**
847      * INTERNAL:
848      */

849     public void setSchema(String JavaDoc xmlSchema) {
850         m_xmlSchema = xmlSchema;
851     }
852     
853     /**
854      * INTERNAL:
855      */

856     public void setSequenceNumberField(DatabaseField field) {
857         m_descriptor.setSequenceNumberField(field);
858     }
859     
860     /**
861      * INTERNAL:
862      */

863     public void setSequenceNumberName(String JavaDoc name) {
864         m_descriptor.setSequenceNumberName(name);
865     }
866     
867     /**
868      * INTERNAL:
869      * Indicates that all annotations should be ignored, and only default values
870      * set by the annotations processor.
871      */

872     public void setIgnoreAnnotations(boolean ignoreAnnotations) {
873         m_ignoreAnnotations = ignoreAnnotations;
874     }
875     
876     /**
877      * INTERNAL:
878      * Sets the strategy on the descriptor's inheritance policy to SINGLE_TABLE.
879      * The default is JOINED.
880      */

881     public void setSingleTableInheritanceStrategy() {
882         m_descriptor.getInheritancePolicy().setSingleTableStrategy();
883     }
884
885     /**
886      * INTERNAL:
887      * Indicates whether or not annotations should be ignored, i.e. only default
888      * values processed.
889      */

890     public boolean ignoreAnnotations() {
891         return m_ignoreAnnotations;
892     }
893     
894     /**
895      * INTERNAL:
896      */

897     public boolean usesOptimisticLocking() {
898         return m_descriptor.usesOptimisticLocking();
899     }
900     
901     /**
902      * INTERNAL:
903      * Returns true if this class uses property access. In an inheritance
904      * hierarchy, the subclasses inherit their access type from the parent.
905      * The metadata helper method caches the class access types for
906      * efficiency.
907      */

908     public boolean usesPropertyAccess() {
909         if (m_accessor.isInheritanceSubclass()) {
910             return m_accessor.getInheritanceRootDescriptor().usesPropertyAccess();
911         } else {
912             if (m_usesPropertyAccess == null) {
913                 if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getFields(getJavaClass())) || isXmlFieldAccess()) {
914                     if (isXmlPropertyAccess()) {
915                         // WIP - throw an exception.
916
}
917                 
918                     // We have persistence annotations defined on a field from
919
// the entity or field access has been set via XML, set the
920
// access to FIELD.
921
m_usesPropertyAccess = new Boolean JavaDoc(false);
922                 } else if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getDeclaredMethods(getJavaClass())) || isXmlPropertyAccess()) {
923                     if (isXmlFieldAccess()) {
924                         // WIP - throw an exception.
925
}
926                 
927                     // We have persistence annotations defined on a method from
928
// the entity or method access has been set via XML, set the
929
// access to PROPERTY.
930
m_usesPropertyAccess = new Boolean JavaDoc(true);
931                 } else {
932                     for (ClassAccessor mappedSuperclass : getClassAccessor().getMappedSuperclasses()) {
933                         if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getFields(mappedSuperclass.getJavaClass()))) {
934                             // We have persistence annotations defined on a
935
// field from a mapped superclass, set the access
936
// to FIELD.
937
m_usesPropertyAccess = new Boolean JavaDoc(false);
938                             break;
939                         } else if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getDeclaredMethods(mappedSuperclass.getJavaClass()))) {
940                             // We have persistence annotations defined on a
941
// method from a mapped superclass, set the access
942
// to FIELD.
943
m_usesPropertyAccess = new Boolean JavaDoc(true);
944                             break;
945                         }
946                     }
947                 
948                     // We still found nothing ... we should throw an exception
949
// here, but for now, set the access to PROPERTY. The user
950
// will eventually get an exception saying there is no
951
// primary key set if property access is not actually the
952
// case.
953
if (m_usesPropertyAccess == null) {
954                         m_usesPropertyAccess = new Boolean JavaDoc(true);
955                     }
956                 }
957             }
958         
959             return m_usesPropertyAccess;
960         }
961     }
962
963     /**
964      * This method has been added so that a the access-type can be set
965      * while processing a class like Embeddable as it inherits the access-type
966      * from the referencing entity.
967      *
968      * @param usesPropertyAccess
969      */

970     public void setUsesPropertyAccess(Boolean JavaDoc usesPropertyAccess) {
971         this.m_usesPropertyAccess = usesPropertyAccess;
972     }
973
974     /**
975      * INTERNAL:
976      * Indicates if the strategy on the descriptor's inheritance policy is
977      * SINGLE_TABLE or JOINED.
978      */

979     public boolean usesSingleTableInheritanceStrategy() {
980         return m_usesSingleTableInheritanceStrategy;
981     }
982     
983     /**
984      * INTERNAL:
985      * This method is used only to validate id fields that were found on a
986      * pk class were also found on the entity.
987      */

988     public void validatePKClassId(String JavaDoc attributeName, Type JavaDoc type) {
989         if (m_pkClassIDs.containsKey(attributeName)) {
990             Type JavaDoc expectedType = m_pkClassIDs.get(attributeName);
991             
992             if (type == expectedType) {
993                 m_pkClassIDs.remove(attributeName);
994             } else {
995                 throw ValidationException.invalidCompositePKAttribute(getJavaClass(), getPKClassName(), attributeName, expectedType, type);
996             }
997         }
998     }
999 }
1000
Popular Tags