KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package jodd.db.orm;
4
5 import jodd.db.orm.meta.DbColumn;
6 import jodd.db.orm.meta.DbTable;
7 import jodd.introspector.DefaultIntrospector;
8
9 import java.lang.reflect.Field JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  * Holds all information about some entity type.
15  */

16 public class DbEntityDescriptor {
17
18     private DbOrm dbOrm;
19
20     public DbEntityDescriptor(Class JavaDoc type, DbOrm dbOrm) {
21         this.type = type;
22         this.dbOrm = dbOrm;
23         resolveTableName();
24     }
25
26     /**
27      * Resolves table name from type. If type is annotated, table name
28      * will be read from annotation value. If this value is empty or if
29      * type is not annotated, table name will be generated from class name.
30      */

31     @SuppressWarnings JavaDoc({"unchecked"})
32     private void resolveTableName() {
33         if (tableName == null) {
34             DbTable dbTable = (DbTable) type.getAnnotation(DbTable.class);
35             if (dbTable != null) {
36                 isAnnotated = true;
37                 tableName = dbTable.value().trim();
38             }
39             if ((tableName == null) || (tableName.length() == 0)) {
40                 tableName = DbNameUtil.convertClassNameToTableName(type, dbOrm.tablePrefix);
41             }
42         }
43
44     }
45
46     // ---------------------------------------------------------------- type and table
47

48     private Class JavaDoc type;
49
50     /**
51      * Returns entity type.
52      */

53     public Class JavaDoc getType() {
54         return type;
55     }
56
57     private boolean isAnnotated;
58
59     /**
60      * Returns <code>true</code> if type is annotated with {@link jodd.db.orm.meta.DbTable}.
61      */

62     public boolean isAnnotated() {
63         return isAnnotated;
64     }
65
66     private String JavaDoc tableName;
67
68     /**
69      * Returns table name to which the entity is mapped.
70      */

71     public String JavaDoc getTableName() {
72         return tableName;
73     }
74
75     // ---------------------------------------------------------------- columns and fields
76

77     private String JavaDoc[] columns;
78     private String JavaDoc[] properties;
79
80     /**
81      * Returns the array of all columns mapped by entity type.
82      */

83     public String JavaDoc[] getColumns() {
84         if (columns == null) {
85             resolveColumnsAndProperties(type);
86         }
87         return columns;
88     }
89
90     /**
91      * Returns the array of all properties that are mapped to columns in entity type.
92      */

93     public String JavaDoc[] getProperties() {
94         if (properties == null) {
95             resolveColumnsAndProperties(type);
96         }
97         return properties;
98     }
99
100     /**
101      * Resolves list of all columns and properties.
102      */

103     private void resolveColumnsAndProperties(Class JavaDoc type) {
104         List JavaDoc<String JavaDoc> columnNames = new ArrayList JavaDoc<String JavaDoc>();
105         List JavaDoc<String JavaDoc> propertyNames = new ArrayList JavaDoc<String JavaDoc>();
106         Field JavaDoc[] fields = DefaultIntrospector.lookup(type).getAllFields(true);
107         for (Field JavaDoc field : fields) {
108             String JavaDoc columnName = resolveColumnName(field);
109             if (columnName != null) {
110                 columnNames.add(columnName);
111                 propertyNames.add(field.getName());
112             }
113         }
114         if (columnNames.isEmpty()) {
115             throw new DbOrmException("Entity '" + type + "' doesn't have any column mappings.");
116         }
117         columns = columnNames.toArray(new String JavaDoc[columnNames.size()]);
118         properties = propertyNames.toArray(new String JavaDoc[propertyNames.size()]);
119     }
120
121     /**
122      * Resolve column name from field. If field is annotated value will be read
123      * from annotation. If field is not annotated, then field will be ignored
124      * if entity is annotated. Otherwise, column name is generated from field name.
125      */

126     private String JavaDoc resolveColumnName(Field JavaDoc field) {
127         DbColumn dbColumn = field.getAnnotation(DbColumn.class);
128         String JavaDoc columnName = null;
129         if (dbColumn != null) {
130             columnName = dbColumn.value().trim();
131         } else {
132             if (isAnnotated == true) {
133                 return null;
134             }
135         }
136         if ((columnName == null) || (columnName.length() == 0)) {
137             columnName = DbNameUtil.convertPropertyNameToColumnName(field.getName());
138         }
139         return columnName;
140     }
141
142
143     /**
144      * Returns property name for specified column name..
145      */

146     public String JavaDoc getPropertyName(String JavaDoc columnName) {
147         if (columnName == null) {
148             return null;
149         }
150         if (columns == null) {
151             resolveColumnsAndProperties(type);
152         }
153         for (int i = 0; i < columns.length; i++) {
154             String JavaDoc name = columns[i];
155             if (name.equals(columnName) == true) {
156                 return properties[i];
157             }
158         }
159         return null;
160     }
161
162     /**
163      * Returns column name for specified property name..
164      */

165     public String JavaDoc getColumnName(String JavaDoc propertyName) {
166         if (propertyName == null) {
167             return null;
168         }
169         if (columns == null) {
170             resolveColumnsAndProperties(type);
171         }
172         for (int i = 0; i < properties.length; i++) {
173             String JavaDoc name = properties[i];
174             if (name.equals(propertyName) == true) {
175                 return columns[i];
176             }
177         }
178         return null;
179     }
180
181 }
182
Popular Tags