KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Mappings.java,v 1.24 2005/07/20 16:38:28 oneovthafew Exp $
2
package org.hibernate.cfg;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.dom4j.Document;
14 import org.hibernate.MappingException;
15 import org.hibernate.engine.FilterDefinition;
16 import org.hibernate.engine.NamedQueryDefinition;
17 import org.hibernate.engine.NamedSQLQueryDefinition;
18 import org.hibernate.engine.ResultSetMappingDefinition;
19 import org.hibernate.mapping.Collection;
20 import org.hibernate.mapping.DenormalizedTable;
21 import org.hibernate.mapping.PersistentClass;
22 import org.hibernate.mapping.Table;
23 import org.hibernate.mapping.TypeDef;
24 import org.hibernate.util.StringHelper;
25
26 /**
27  * A collection of mappings from classes and collections to
28  * relational database tables. (Represents a single
29  * <tt>&lt;hibernate-mapping&gt;</tt> element.)
30  * @author Gavin King
31  */

32 public class Mappings implements Serializable JavaDoc {
33
34     private static final Log log = LogFactory.getLog(Mappings.class);
35
36     private final Map JavaDoc classes;
37     private final Map JavaDoc collections;
38     private final Map JavaDoc tables;
39     private final Map JavaDoc queries;
40     private final Map JavaDoc sqlqueries;
41     private final Map JavaDoc resultSetMappings;
42     private final Map JavaDoc typeDefs;
43     private final List JavaDoc secondPasses;
44     private final Map JavaDoc imports;
45     private String JavaDoc schemaName;
46     private String JavaDoc catalogName;
47     private String JavaDoc defaultCascade;
48     private String JavaDoc defaultPackage;
49     private String JavaDoc defaultAccess;
50     private boolean autoImport;
51     private boolean defaultLazy;
52     private final List JavaDoc propertyReferences;
53     private final NamingStrategy namingStrategy;
54     private final Map JavaDoc filterDefinitions;
55
56     private final Map JavaDoc extendsQueue;
57
58
59     Mappings(
60             final Map JavaDoc classes,
61             final Map JavaDoc collections,
62             final Map JavaDoc tables,
63             final Map JavaDoc queries,
64             final Map JavaDoc sqlqueries,
65             final Map JavaDoc sqlResultSetMappings,
66             final Map JavaDoc imports,
67             final List JavaDoc secondPasses,
68             final List JavaDoc propertyReferences,
69             final NamingStrategy namingStrategy,
70             final Map JavaDoc typeDefs,
71             final Map JavaDoc filterDefinitions,
72             final Map JavaDoc extendsQueue
73             ) {
74         this.classes = classes;
75         this.collections = collections;
76         this.queries = queries;
77         this.sqlqueries = sqlqueries;
78         this.resultSetMappings = sqlResultSetMappings;
79         this.tables = tables;
80         this.imports = imports;
81         this.secondPasses = secondPasses;
82         this.propertyReferences = propertyReferences;
83         this.namingStrategy = namingStrategy;
84         this.typeDefs = typeDefs;
85         this.filterDefinitions = filterDefinitions;
86         this.extendsQueue = extendsQueue;
87     }
88
89     public void addClass(PersistentClass persistentClass) throws MappingException {
90         Object JavaDoc old = classes.put( persistentClass.getEntityName(), persistentClass );
91         if ( old!=null ) {
92             throw new MappingException( "duplicate class mapping: " + persistentClass.getEntityName() );
93         }
94     }
95     public void addCollection(Collection collection) throws MappingException {
96         Object JavaDoc old = collections.put( collection.getRole(), collection );
97         if ( old!=null ) {
98             throw new MappingException( "duplicate collection role mapping: " + collection.getRole() );
99         }
100     }
101     public PersistentClass getClass(String JavaDoc className) {
102         return (PersistentClass) classes.get(className);
103     }
104     public Collection getCollection(String JavaDoc role) {
105         return (Collection) collections.get(role);
106     }
107
108     public void addImport(String JavaDoc className, String JavaDoc rename) throws MappingException {
109         String JavaDoc existing = (String JavaDoc) imports.put(rename, className);
110         if ( existing!=null ) {
111             if ( existing.equals(className) ) {
112                 log.info( "duplicate import: " + className + "->" + rename );
113             }
114             else {
115                 throw new MappingException(
116                         "duplicate import: " + rename +
117                         " refers to both " + className +
118                         " and " + existing +
119                         " (try using auto-import=\"false\")"
120                     );
121             }
122         }
123     }
124
125     public Table addTable(String JavaDoc schema,
126             String JavaDoc catalog,
127             String JavaDoc name,
128             String JavaDoc subselect,
129             boolean isAbstract
130     ) {
131         String JavaDoc key = subselect==null ?
132             Table.qualify(catalog, schema, name, '.') :
133             subselect;
134         Table table = (Table) tables.get(key);
135
136         if (table == null) {
137             table = new Table();
138             table.setAbstract(isAbstract);
139             table.setName(name);
140             table.setSchema(schema);
141             table.setCatalog(catalog);
142             table.setSubselect(subselect);
143             tables.put(key, table);
144         }
145         else {
146             if (!isAbstract) table.setAbstract(false);
147         }
148
149         return table;
150     }
151
152     public Table addDenormalizedTable(
153             String JavaDoc schema,
154             String JavaDoc catalog,
155             String JavaDoc name,
156             boolean isAbstract,
157             String JavaDoc subselect,
158             Table includedTable)
159     throws MappingException {
160         String JavaDoc key = subselect==null ?
161                 Table.qualify(catalog, schema, name, '.') :
162                 subselect;
163         if ( tables.containsKey(key) ) {
164             throw new MappingException("duplicate table: " + name);
165         }
166         Table table = new DenormalizedTable(includedTable);
167         table.setAbstract(isAbstract);
168         table.setName(name);
169         table.setSchema(schema);
170         table.setCatalog(catalog);
171         table.setSubselect(subselect);
172         tables.put(key, table);
173         return table;
174     }
175
176     public Table getTable(String JavaDoc schema, String JavaDoc catalog, String JavaDoc name) {
177         String JavaDoc key = Table.qualify(catalog, schema, name, '.');
178         return (Table) tables.get(key);
179     }
180
181     public String JavaDoc getSchemaName() {
182         return schemaName;
183     }
184
185     public String JavaDoc getCatalogName() {
186         return catalogName;
187     }
188
189     public String JavaDoc getDefaultCascade() {
190         return defaultCascade;
191     }
192
193     /**
194      * Sets the schemaName.
195      * @param schemaName The schemaName to set
196      */

197     public void setSchemaName(String JavaDoc schemaName) {
198         this.schemaName = schemaName;
199     }
200
201     /**
202      * Sets the catalogName.
203      * @param catalogName The catalogName to set
204      */

205     public void setCatalogName(String JavaDoc catalogName) {
206         this.catalogName = catalogName;
207     }
208
209     /**
210      * Sets the defaultCascade.
211      * @param defaultCascade The defaultCascade to set
212      */

213     public void setDefaultCascade(String JavaDoc defaultCascade) {
214         this.defaultCascade = defaultCascade;
215     }
216
217     /**
218      * sets the default access strategy
219      * @param defaultAccess the default access strategy.
220      */

221     public void setDefaultAccess(String JavaDoc defaultAccess) {
222         this.defaultAccess = defaultAccess;
223     }
224
225     public String JavaDoc getDefaultAccess() {
226         return defaultAccess;
227     }
228
229     public void addQuery(String JavaDoc name, NamedQueryDefinition query) throws MappingException {
230         checkQueryExist(name);
231         queries.put( name.intern(), query );
232     }
233
234     public void addSQLQuery(String JavaDoc name, NamedSQLQueryDefinition query) throws MappingException {
235         checkQueryExist(name);
236         sqlqueries.put( name.intern(), query );
237     }
238
239     private void checkQueryExist(String JavaDoc name) throws MappingException {
240         if ( sqlqueries.containsKey(name) || queries.containsKey(name) ) {
241             throw new MappingException("Duplicate query named: " + name);
242         }
243     }
244
245     public void addResultSetMapping(ResultSetMappingDefinition sqlResultSetMapping) {
246         final String JavaDoc name = sqlResultSetMapping.getName();
247         if ( resultSetMappings.containsKey(name) ) {
248             throw new MappingException("Duplicate ResultSet mapping named: " + name);
249         }
250         resultSetMappings.put(name, sqlResultSetMapping);
251     }
252
253     public ResultSetMappingDefinition getResultSetMapping(String JavaDoc name) {
254         return (ResultSetMappingDefinition) resultSetMappings.get(name);
255     }
256
257
258     public NamedQueryDefinition getQuery(String JavaDoc name) {
259         return (NamedQueryDefinition) queries.get(name);
260     }
261
262     public void addSecondPass(HbmBinder.SecondPass sp) {
263         addSecondPass(sp, false);
264     }
265     
266     public void addSecondPass(HbmBinder.SecondPass sp, boolean onTopOfTheQueue) {
267         if (onTopOfTheQueue) {
268             secondPasses.add(0, sp);
269         }
270         else {
271             secondPasses.add(sp);
272         }
273     }
274
275     /**
276      * Returns the autoImport.
277      * @return boolean
278      */

279     public boolean isAutoImport() {
280         return autoImport;
281     }
282
283     /**
284      * Sets the autoImport.
285      * @param autoImport The autoImport to set
286      */

287     public void setAutoImport(boolean autoImport) {
288         this.autoImport = autoImport;
289     }
290
291     void addUniquePropertyReference(String JavaDoc referencedClass, String JavaDoc propertyName) {
292         PropertyReference upr = new PropertyReference();
293         upr.referencedClass = referencedClass;
294         upr.propertyName = propertyName;
295         upr.unique = true;
296         propertyReferences.add(upr);
297     }
298
299     void addPropertyReference(String JavaDoc referencedClass, String JavaDoc propertyName) {
300         PropertyReference upr = new PropertyReference();
301         upr.referencedClass = referencedClass;
302         upr.propertyName = propertyName;
303         propertyReferences.add(upr);
304     }
305
306     static final class PropertyReference implements Serializable JavaDoc {
307         String JavaDoc referencedClass;
308         String JavaDoc propertyName;
309         boolean unique;
310     }
311
312     /**
313      * @return Returns the defaultPackage.
314      */

315     public String JavaDoc getDefaultPackage() {
316         return defaultPackage;
317     }
318
319     /**
320      * @param defaultPackage The defaultPackage to set.
321      */

322     public void setDefaultPackage(String JavaDoc defaultPackage) {
323         this.defaultPackage = defaultPackage;
324     }
325
326     public NamingStrategy getNamingStrategy() {
327         return namingStrategy;
328     }
329
330     public void addTypeDef(String JavaDoc typeName, String JavaDoc typeClass, Properties JavaDoc paramMap) {
331         TypeDef def = new TypeDef(typeClass, paramMap);
332         typeDefs.put(typeName, def);
333         log.debug("Added " + typeName + " with class " + typeClass);
334     }
335
336     public TypeDef getTypeDef(String JavaDoc typeName) {
337         return (TypeDef) typeDefs.get(typeName);
338     }
339
340     public Iterator JavaDoc iterateCollections() {
341         return collections.values().iterator();
342     }
343     
344     public Iterator JavaDoc iterateTables() {
345         return tables.values().iterator();
346     }
347
348     public Map JavaDoc getFilterDefinitions() {
349         return filterDefinitions;
350     }
351
352     public void addFilterDefinition(FilterDefinition definition) {
353         filterDefinitions.put( definition.getFilterName(), definition );
354     }
355     
356     public FilterDefinition getFilterDefinition(String JavaDoc name) {
357         return (FilterDefinition) filterDefinitions.get(name);
358     }
359     
360     public boolean isDefaultLazy() {
361         return defaultLazy;
362     }
363     public void setDefaultLazy(boolean defaultLazy) {
364         this.defaultLazy = defaultLazy;
365     }
366
367     /**
368      * @param className
369      * @param doc
370      */

371     public void addToExtendsQueue(String JavaDoc className, Document doc) {
372         List JavaDoc existingQueue = (List JavaDoc) extendsQueue.get(className);
373         if(existingQueue==null) {
374             existingQueue = new ArrayList JavaDoc();
375         }
376         existingQueue.add(doc);
377         extendsQueue.put(className, existingQueue);
378     }
379
380     public PersistentClass locatePersistentClassByEntityName(String JavaDoc entityName) {
381         PersistentClass persistentClass = ( PersistentClass ) classes.get( entityName );
382         if ( persistentClass == null ) {
383             String JavaDoc actualEntityName = ( String JavaDoc ) imports.get( entityName );
384             if ( StringHelper.isNotEmpty( actualEntityName ) ) {
385                 persistentClass = ( PersistentClass ) classes.get( actualEntityName );
386             }
387         }
388         return persistentClass;
389     }
390 }
Popular Tags