1 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 ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 13 16 public class DbEntityDescriptor { 17 18 private DbOrm dbOrm; 19 20 public DbEntityDescriptor(Class type, DbOrm dbOrm) { 21 this.type = type; 22 this.dbOrm = dbOrm; 23 resolveTableName(); 24 } 25 26 31 @SuppressWarnings ({"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 48 private Class type; 49 50 53 public Class getType() { 54 return type; 55 } 56 57 private boolean isAnnotated; 58 59 62 public boolean isAnnotated() { 63 return isAnnotated; 64 } 65 66 private String tableName; 67 68 71 public String getTableName() { 72 return tableName; 73 } 74 75 77 private String [] columns; 78 private String [] properties; 79 80 83 public String [] getColumns() { 84 if (columns == null) { 85 resolveColumnsAndProperties(type); 86 } 87 return columns; 88 } 89 90 93 public String [] getProperties() { 94 if (properties == null) { 95 resolveColumnsAndProperties(type); 96 } 97 return properties; 98 } 99 100 103 private void resolveColumnsAndProperties(Class type) { 104 List <String > columnNames = new ArrayList <String >(); 105 List <String > propertyNames = new ArrayList <String >(); 106 Field [] fields = DefaultIntrospector.lookup(type).getAllFields(true); 107 for (Field field : fields) { 108 String 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 [columnNames.size()]); 118 properties = propertyNames.toArray(new String [propertyNames.size()]); 119 } 120 121 126 private String resolveColumnName(Field field) { 127 DbColumn dbColumn = field.getAnnotation(DbColumn.class); 128 String 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 146 public String getPropertyName(String 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 name = columns[i]; 155 if (name.equals(columnName) == true) { 156 return properties[i]; 157 } 158 } 159 return null; 160 } 161 162 165 public String getColumnName(String 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 name = properties[i]; 174 if (name.equals(propertyName) == true) { 175 return columns[i]; 176 } 177 } 178 return null; 179 } 180 181 } 182 | Popular Tags |