KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > orm > DbOrm


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.orm;
4
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Global DB-ORM mapping definitions, prefixes and cache.
10  *
11  * <p>
12  * Mapping definitions are used <b>only</b> by a result set mapper (such as {@link jodd.db.orm.mapper.ResultSetMapper}
13  * to lookup for an entity from table name. Table names are read from result-set meta data, for example.
14  * Moreover, it is not needed to use mappings at all: in that case just provide entity types during result set to
15  * objects conversion.
16
17  */

18 public class DbOrm {
19
20     /**
21      * Singleton.
22      */

23     protected DbOrm() {
24         
25     }
26
27     // ---------------------------------------------------------------- singleton
28

29     protected static DbOrm orm = new DbOrm();
30
31     /**
32      * Returns current DB-ORM instance.
33      */

34     public static DbOrm getInstance() {
35         return orm;
36     }
37
38     /**
39      * Sets new default instance for DB-ORM mapper.
40      * If parameter is <code>null</code> current instance will be replaced
41      * with new instance of this class.
42      */

43     public static void setInstance(DbOrm orm) {
44         if (orm == null) {
45             orm = new DbOrm();
46         }
47         DbOrm.orm = orm;
48     }
49
50
51     // ---------------------------------------------------------------- prefix
52

53     protected String JavaDoc tablePrefix = null;
54
55     /**
56      * Sets default table prefix for all tables. This prefix affect default
57      * conversions from bean name to table name and vice-versa when no annotations
58      * are used.
59      */

60     public DbOrm setTablePrefix(String JavaDoc prefix) {
61         this.tablePrefix = prefix;
62         return this;
63     }
64
65     /**
66      * Returns current table prefix.
67      */

68     public String JavaDoc getTablePrefix() {
69         return tablePrefix;
70     }
71
72
73     protected String JavaDoc packagePrefix = null;
74
75     /**
76      * Sets package prefix for all DB entities. Used by {@link jodd.db.orm.sqlgen.DbSqlGenerator}
77      * during creating sql queries.
78      */

79     public DbOrm setPackagePrefix(String JavaDoc prefix) {
80         if ((packagePrefix != null) && (prefix.endsWith("."))) {
81             prefix = prefix.substring(0, prefix.length() - 1);
82         }
83         this.packagePrefix = prefix;
84         return this;
85     }
86
87     /**
88      * Returns current package prefix.
89      */

90     public String JavaDoc getPackagePrefix() {
91         return packagePrefix;
92     }
93
94     // ---------------------------------------------------------------- mappings
95

96     protected Map JavaDoc<String JavaDoc, Class JavaDoc> tableMaps = new HashMap JavaDoc<String JavaDoc, Class JavaDoc>();
97
98     /**
99      * Map table to entity type. Table name is readed from {@link jodd.db.orm.meta.DbTable}
100      * annotation. If type is not annotated, or value is not specified, table name will
101      * be created from default table prefix and type name. Duplicated table names mappings
102      * are not allowed.
103      * <p>
104      * Mapped tables are used only when result set is parsed with no specified types.
105      */

106     public DbOrm mapTable(Class JavaDoc type) {
107         DbEntityDescriptor ded = lookup(type);
108         if (ded == null) {
109             throw new DbOrmException("Mapping to a core java class has no sence: '" + type + "'.");
110         }
111         String JavaDoc tableName = ded.getTableName();
112         Class JavaDoc previousType = tableMaps.put(tableName, type);
113         if (previousType != null) {
114             throw new DbOrmException("Table '" + tableName + "' already registered with type '" + previousType + "'.");
115         }
116         return this;
117     }
118
119     /**
120      * Clear all mappings.
121      */

122     public void clearAllMappings() {
123         tableMaps.clear();
124     }
125
126     /**
127      * Returns mapped type for table name.
128      */

129     public Class JavaDoc getMappedEntityType(String JavaDoc tableName) {
130         return tableMaps.get(tableName);
131     }
132
133
134     // ---------------------------------------------------------------- cache
135

136     protected Map JavaDoc<Class JavaDoc, DbEntityDescriptor> descriptors = new HashMap JavaDoc<Class JavaDoc, DbEntityDescriptor>();
137
138     /**
139      * Lookups for {@link DbEntityDescriptor}. If it doesn't exist, new one will be created.
140      * Returns <code>null</code> for core classes from <code>java</code> runtime packages.
141      */

142     public DbEntityDescriptor lookup(Class JavaDoc type) {
143         String JavaDoc typeName = type.getName();
144         if (typeName.indexOf("java.") == 0) {
145             return null;
146         }
147         DbEntityDescriptor ded = descriptors.get(type);
148         if (ded == null) {
149             ded = new DbEntityDescriptor(type, this);
150             descriptors.put(type, ded);
151         }
152         return ded;
153     }
154
155     /**
156      * Clears descriptors cache.
157      */

158     public void clearDescriptorsCache() {
159         descriptors.clear();
160     }
161
162
163     // ---------------------------------------------------------------- table separators
164

165     protected String JavaDoc columnAliasSeparator = "$";
166
167     /**
168      * Returns value for separator for column aliases that divides table reference and column name.
169      */

170     public String JavaDoc getColumnAliasSeparator() {
171         return columnAliasSeparator;
172     }
173
174     /**
175      * Specifies separator for column aliases that divides table reference and column name.
176      * Separator should contains of characters that are not used in table names, such as:
177      * '$' or '__'.
178      */

179     public void setColumnAliasSeparator(String JavaDoc separator) {
180         this.columnAliasSeparator = separator;
181     }
182
183 }
184
Popular Tags