KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > ExtendedMappings


1 //$Id: ExtendedMappings.java,v 1.20 2005/07/22 00:42:33 epbernard Exp $
2
package org.hibernate.cfg;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Properties JavaDoc;
8 import javax.persistence.Embeddable;
9 import javax.persistence.Entity;
10 import javax.persistence.EmbeddableSuperclass;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.hibernate.MappingException;
15 import org.hibernate.mapping.IdGenerator;
16 import org.hibernate.mapping.Join;
17 import org.hibernate.mapping.PersistentClass;
18 import org.hibernate.mapping.Table;
19
20 /**
21  * Allow annotation related mappings
22  *
23  * at least for named generators
24  *
25  * @author Emmanuel Bernard
26  */

27 public class ExtendedMappings extends Mappings {
28     
29     private static final Log log = LogFactory.getLog(ExtendedMappings.class);
30     
31     private final Map JavaDoc<String JavaDoc, IdGenerator> namedGenerators;
32     private final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Join>> joins;
33     private final Map JavaDoc<Class JavaDoc, AnnotatedClassType> classTypes;
34     private final Map JavaDoc<String JavaDoc, Properties JavaDoc> generatorTables;
35     private final Map JavaDoc<Table, List JavaDoc<String JavaDoc[]>> tableUniqueConstraints;
36     private final Map JavaDoc<String JavaDoc, String JavaDoc> mappedByResolver;
37     private final Map JavaDoc<String JavaDoc, String JavaDoc> propertyRefResolver;
38
39     ExtendedMappings(
40             Map JavaDoc classes, Map JavaDoc collections, Map JavaDoc tables, Map JavaDoc queries, Map JavaDoc sqlqueries, Map JavaDoc sqlResultSetMappings,
41             Map JavaDoc imports,
42             List JavaDoc secondPasses, List JavaDoc propertyReferences, NamingStrategy namingStrategy, Map JavaDoc typeDefs,
43             Map JavaDoc filterDefinitions, Map JavaDoc namedGenerators, Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Join>> joins, Map JavaDoc<Class JavaDoc,
44             AnnotatedClassType> classTypes, Map JavaDoc extendsQueue, Map JavaDoc<String JavaDoc, Properties JavaDoc> generatorTables,
45             Map JavaDoc<Table, List JavaDoc<String JavaDoc[]>> tableUniqueConstraints, Map JavaDoc<String JavaDoc, String JavaDoc> mappedByResolver,
46             Map JavaDoc<String JavaDoc, String JavaDoc> propertyRefResolver
47             ) {
48         super(
49             classes,
50             collections,
51             tables,
52             queries,
53             sqlqueries,
54             sqlResultSetMappings,
55             imports,
56             secondPasses,
57             propertyReferences,
58             namingStrategy,
59             typeDefs,
60             filterDefinitions,
61             extendsQueue);
62         this.namedGenerators = namedGenerators;
63         this.joins = joins;
64         this.classTypes = classTypes;
65         this.generatorTables = generatorTables;
66         this.tableUniqueConstraints = tableUniqueConstraints;
67         this.mappedByResolver = mappedByResolver;
68         this.propertyRefResolver = propertyRefResolver;
69     }
70     
71     public void addGenerator(IdGenerator generator) throws MappingException {
72         Object JavaDoc old = namedGenerators.put( generator.getName(), generator );
73         if ( old!=null ) log.warn( "duplicate generator name: " + generator.getName() );
74     }
75     
76     public void addJoins(PersistentClass persistentClass, Map JavaDoc<String JavaDoc, Join> joins) throws MappingException {
77         Object JavaDoc old = this.joins.put( persistentClass.getEntityName(), joins );
78         if ( old!=null ) log.warn( "duplicate joins for class: " + persistentClass.getEntityName() );
79     }
80
81     public AnnotatedClassType addClassType(Class JavaDoc clazz) {
82         AnnotatedClassType type;
83         if ( clazz.isAnnotationPresent(Entity.class) ) {
84             type = AnnotatedClassType.ENTITY;
85         }
86         else if (clazz.isAnnotationPresent(Embeddable.class) ) {
87             type = AnnotatedClassType.EMBEDDABLE;
88         }
89         else if (clazz.isAnnotationPresent(EmbeddableSuperclass.class) ) {
90             type = AnnotatedClassType.EMBEDDABLE_SUPERCLASS;
91         }
92         else {
93             type = AnnotatedClassType.NONE;
94         }
95         classTypes.put(clazz, type);
96         return type;
97     }
98
99     /**
100      * get and maintain a cache of class type.
101      * A class can be an entity, a embedded objet or nothing.
102      */

103     public AnnotatedClassType getClassType(Class JavaDoc clazz) {
104         AnnotatedClassType type = classTypes.get(clazz);
105         if (type == null) {
106             return addClassType(clazz);
107         }
108         else {
109             return type;
110         }
111     }
112
113     public IdGenerator getGenerator(String JavaDoc name) {
114         return getGenerator(name, null);
115     }
116     
117     public Map JavaDoc<String JavaDoc, Join> getJoins(String JavaDoc persistentClass) {
118         return joins.get(persistentClass);
119     }
120     
121     /**
122      * Try to find the generator from the localGenerators
123      * and then from the global generator list
124      *
125      * @param name generator name
126      * @param localGenerators local generators to find to
127      * @return the appropriate idgenerator or null if not found
128      */

129     public IdGenerator getGenerator(String JavaDoc name, Map JavaDoc<String JavaDoc, IdGenerator> localGenerators) {
130         if (localGenerators != null) {
131             IdGenerator result = localGenerators.get(name);
132             if (result != null) return result;
133         }
134         return namedGenerators.get(name);
135     }
136
137     public void addGeneratorTable(String JavaDoc name, Properties JavaDoc params) {
138         Object JavaDoc old = generatorTables.put(name, params);
139         if ( old!=null ) log.warn( "duplicate generator table: " + name);
140     }
141
142     public Properties JavaDoc getGeneratorTableProperties(String JavaDoc name, Map JavaDoc<String JavaDoc, Properties JavaDoc> localGeneratorTables) {
143         if (localGeneratorTables != null) {
144             Properties JavaDoc result = localGeneratorTables.get(name);
145             if (result != null) return result;
146         }
147         return generatorTables.get(name);
148     }
149
150     public void addUniqueConstraints(Table table, List JavaDoc uniqueConstraints) {
151         List JavaDoc oldConstraints = tableUniqueConstraints.get(table);
152         if (oldConstraints == null) {
153             oldConstraints = new ArrayList JavaDoc();
154             tableUniqueConstraints.put(table, oldConstraints);
155         }
156         oldConstraints.addAll(uniqueConstraints);
157     }
158
159     public Map JavaDoc<Table, List JavaDoc<String JavaDoc[]>> getTableUniqueConstraints() {
160         return tableUniqueConstraints;
161     }
162
163     public void addMappedBy(String JavaDoc entityName, String JavaDoc propertyName, String JavaDoc inversePropertyName) {
164         mappedByResolver.put( entityName + "." + propertyName, inversePropertyName );
165     }
166
167     public String JavaDoc getFromMappedBy(String JavaDoc entityName, String JavaDoc propertyName) {
168         return mappedByResolver.get(entityName + "." + propertyName);
169     }
170
171     public void addPropertyReferencedAssociation(String JavaDoc entityName, String JavaDoc propertyName, String JavaDoc propertyRef) {
172         propertyRefResolver.put( entityName + "." + propertyName, propertyRef );
173     }
174
175     public String JavaDoc getPropertyReferencedAssociation(String JavaDoc entityName, String JavaDoc propertyName) {
176         return propertyRefResolver.get(entityName + "." + propertyName);
177     }
178
179     @Override JavaDoc public void addUniquePropertyReference(String JavaDoc referencedClass, String JavaDoc propertyName) {
180         super.addUniquePropertyReference( referencedClass, propertyName );
181     }
182
183     @Override JavaDoc public void addPropertyReference(String JavaDoc referencedClass, String JavaDoc propertyName) {
184         super.addPropertyReference( referencedClass, propertyName );
185     }
186 }
187
Popular Tags