| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
package org.melati.poem; |
| 45 | |
|
| 46 | |
import java.lang.reflect.Method; |
| 47 | |
import java.lang.reflect.Modifier; |
| 48 | |
import java.util.Collection; |
| 49 | |
import java.util.Enumeration; |
| 50 | |
import java.util.Hashtable; |
| 51 | |
import java.util.Map; |
| 52 | |
|
| 53 | |
import org.melati.poem.util.ClassUtils; |
| 54 | |
import org.melati.poem.util.StringUtils; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public final class TableFactory { |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | 0 | private TableFactory() { |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public static Table<?> fromInstance(Database db, Object pojo) { |
| 87 | 16 | return fromClass(db, pojo.getClass()); |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public static Table<?> fromClass(Database db, Class<?> clazz) { |
| 98 | 21 | if (clazz.isPrimitive()) |
| 99 | 1 | throw new IllegalArgumentException( |
| 100 | 1 | "Cannot create a Table for a primitive type: " + clazz.getName()); |
| 101 | 20 | if (clazz.isInterface()) |
| 102 | 0 | throw new IllegalArgumentException( |
| 103 | 0 | "Cannot create a Table for an interface: " + clazz.getName()); |
| 104 | 20 | if(!Modifier.isPublic(clazz.getModifiers())) |
| 105 | 1 | throw new IllegalArgumentException( |
| 106 | 1 | "Cannot create a Table for a non public type: "+ clazz.getName()); |
| 107 | 19 | if (clazz.isArray() && !(clazz == byte[].class)) |
| 108 | 1 | throw new IllegalArgumentException("Cannot create a Table for an array: " + clazz.getName()); |
| 109 | 18 | String name = clazz.getName(); |
| 110 | 18 | String simpleName = name.substring(name.lastIndexOf('.') + 1); |
| 111 | |
try { |
| 112 | 18 | return db.getTable(simpleName); |
| 113 | 12 | } catch (NoSuchTablePoemException e) { |
| 114 | 12 | System.err.println("Creating a table for : " + name); |
| 115 | |
} |
| 116 | |
|
| 117 | 12 | TableInfo tableInfo = (TableInfo)db.getTableInfoTable().newPersistent(); |
| 118 | 12 | tableInfo.setName(simpleName); |
| 119 | 12 | tableInfo.setDisplayname(simpleName); |
| 120 | 12 | tableInfo.setDescription(simpleName + " introspected table"); |
| 121 | 12 | tableInfo.setDisplayorder(13); |
| 122 | 12 | tableInfo.setSeqcached(Boolean.FALSE); |
| 123 | 12 | tableInfo.setCategory(db.getTableCategoryTable().NORMAL); |
| 124 | 12 | tableInfo.setCachelimit(555); |
| 125 | 12 | tableInfo.makePersistent(); |
| 126 | |
|
| 127 | 12 | Table<Persistent> table = new JdbcTable<Persistent>(db, simpleName, DefinitionSource.runtime); |
| 128 | 12 | String troidName = "id"; |
| 129 | 12 | if (ClassUtils.getNoArgMethod(clazz, "getId") != null && |
| 130 | 3 | !(Persistent.class.isAssignableFrom(clazz))) |
| 131 | 3 | troidName = "poemId"; |
| 132 | 24 | table.defineColumn(new ExtraColumn<Integer>(table, troidName, TroidPoemType.it, |
| 133 | 12 | DefinitionSource.runtime, table.getNextExtrasIndex())); |
| 134 | 12 | table.setTableInfo(tableInfo); |
| 135 | 12 | table.unifyWithColumnInfo(); |
| 136 | 12 | table.unifyWithDB(null,troidName); |
| 137 | |
|
| 138 | 12 | PoemThread.commit(); |
| 139 | 12 | db.defineTable(table); |
| 140 | 12 | Hashtable<String,Prop> props = new Hashtable<String,Prop>(); |
| 141 | |
|
| 142 | 12 | Method[] methods = clazz.getMethods(); |
| 143 | |
|
| 144 | 252 | for (int i = 0; i < methods.length; i++) { |
| 145 | 240 | if (Modifier.isPublic(methods[i].getModifiers())) { |
| 146 | 240 | if (methods[i].getName().startsWith("set") |
| 147 | 68 | && ! methods[i].getName().equals("set") |
| 148 | 68 | && Character.isUpperCase(methods[i].getName().toCharArray()[3]) |
| 149 | 68 | && methods[i].getParameterTypes().length == 1 |
| 150 | 68 | && (methods[i].getParameterTypes()[0] == byte[].class || |
| 151 | 67 | ! methods[i].getParameterTypes()[0].isArray()) |
| 152 | 68 | && !methods[i].getParameterTypes()[0].isInterface() |
| 153 | 68 | && ! Collection.class.isAssignableFrom(methods[i].getParameterTypes()[0]) |
| 154 | 68 | && ! Map.class.isAssignableFrom(methods[i].getParameterTypes()[0]) |
| 155 | |
) { |
| 156 | 68 | String propName = methods[i].getName().substring(3); |
| 157 | 68 | propName = StringUtils.uncapitalised(propName); |
| 158 | 68 | Prop p = (Prop)props.get(propName); |
| 159 | 68 | Class<?> setClass = methods[i].getParameterTypes()[0]; |
| 160 | 68 | if (p == null) { |
| 161 | 16 | p = new Prop(propName); |
| 162 | 16 | p.setSet(setClass); |
| 163 | |
} else { |
| 164 | 52 | if (p.getGot() != null) { |
| 165 | 52 | if(p.getGot() == setClass) |
| 166 | 48 | p.setSet(setClass); |
| 167 | |
} else { |
| 168 | 0 | p.setSet(setClass); |
| 169 | |
} |
| 170 | |
} |
| 171 | 68 | props.put(propName, p); |
| 172 | |
} |
| 173 | |
|
| 174 | 240 | if (methods[i].getParameterTypes().length == 0 |
| 175 | 136 | && ! methods[i].getReturnType().isInterface() |
| 176 | 136 | && (methods[i].getReturnType() == byte[].class || |
| 177 | 135 | !methods[i].getReturnType().isArray()) |
| 178 | 136 | && !Collection.class.isAssignableFrom(methods[i].getReturnType()) |
| 179 | 136 | && !Map.class.isAssignableFrom(methods[i].getReturnType()) |
| 180 | |
) { |
| 181 | 136 | String propName = null; |
| 182 | 136 | if ((methods[i].getName().startsWith("get")) && |
| 183 | 72 | Character.isUpperCase(methods[i].getName().toCharArray()[3])) |
| 184 | 72 | propName = methods[i].getName().substring(3); |
| 185 | 64 | else if (methods[i].getName().startsWith("is") && |
| 186 | 4 | Character.isUpperCase(methods[i].getName().toCharArray()[2])) |
| 187 | 4 | propName = methods[i].getName().substring(2); |
| 188 | 136 | if (propName != null) { |
| 189 | 76 | propName = StringUtils.uncapitalised(propName); |
| 190 | 76 | Prop p = (Prop)props.get(propName); |
| 191 | 76 | Class<?> gotClass = methods[i].getReturnType(); |
| 192 | 76 | if (p == null) { |
| 193 | 60 | p = new Prop(propName); |
| 194 | 60 | p.setGot(gotClass); |
| 195 | |
} else { |
| 196 | 16 | p.setGot(gotClass); |
| 197 | |
} |
| 198 | 76 | props.put(propName, p); |
| 199 | |
} |
| 200 | |
} |
| 201 | |
} |
| 202 | |
} |
| 203 | 12 | Enumeration<Prop> propsEn = props.elements(); |
| 204 | 88 | while (propsEn.hasMoreElements()) { |
| 205 | 76 | Prop p = (Prop)propsEn.nextElement(); |
| 206 | 76 | if (p.getGot() != null && |
| 207 | 76 | p.getGot() == p.getSet()) { |
| 208 | 64 | addColumn(table, p.getName(), p.getGot(), p.getGot() == p.getSet()); |
| 209 | |
} |
| 210 | 76 | } |
| 211 | 12 | return table; |
| 212 | |
} |
| 213 | |
|
| 214 | |
private static void addColumn(Table<?> table, String name, Class<?> fieldClass, |
| 215 | |
boolean hasSetter) { |
| 216 | 64 | ColumnInfo columnInfo = (ColumnInfo)table.getDatabase() |
| 217 | 64 | .getColumnInfoTable().newPersistent(); |
| 218 | 64 | columnInfo.setTableinfo(table.getInfo()); |
| 219 | 64 | columnInfo.setName(name); |
| 220 | |
|
| 221 | 64 | columnInfo.setDisplayname(name); |
| 222 | 64 | columnInfo.setDisplayorder(99); |
| 223 | 64 | columnInfo.setSearchability(Searchability.yes); |
| 224 | 64 | columnInfo.setIndexed(false); |
| 225 | 64 | columnInfo.setUnique(false); |
| 226 | 64 | columnInfo.setDescription("An introspected member"); |
| 227 | 64 | columnInfo.setUsercreateable(hasSetter); |
| 228 | 64 | columnInfo.setUsereditable(hasSetter); |
| 229 | 64 | columnInfo.setSize(8); |
| 230 | 64 | columnInfo.setWidth(20); |
| 231 | 64 | columnInfo.setHeight(1); |
| 232 | 64 | columnInfo.setPrecision(0); |
| 233 | 64 | columnInfo.setScale(0); |
| 234 | 64 | columnInfo.setNullable(true); |
| 235 | 64 | columnInfo.setDisplaylevel(DisplayLevel.record); |
| 236 | 64 | if (fieldClass == java.lang.Boolean.class) { |
| 237 | 4 | columnInfo.setTypefactory(PoemTypeFactory.BOOLEAN); |
| 238 | 4 | columnInfo.setSize(1); |
| 239 | 4 | columnInfo.setWidth(10); |
| 240 | 60 | } else if (fieldClass == boolean.class) { |
| 241 | 4 | columnInfo.setTypefactory(PoemTypeFactory.BOOLEAN); |
| 242 | 4 | columnInfo.setSize(1); |
| 243 | 4 | columnInfo.setWidth(10); |
| 244 | 56 | } else if (fieldClass == java.lang.Integer.class) { |
| 245 | 5 | columnInfo.setTypefactory(PoemTypeFactory.INTEGER); |
| 246 | 51 | } else if (fieldClass == int.class) { |
| 247 | 4 | columnInfo.setTypefactory(PoemTypeFactory.INTEGER); |
| 248 | 47 | } else if (fieldClass == java.lang.Double.class) { |
| 249 | 4 | columnInfo.setTypefactory(PoemTypeFactory.DOUBLE); |
| 250 | 43 | } else if (fieldClass == double.class) { |
| 251 | 4 | columnInfo.setTypefactory(PoemTypeFactory.DOUBLE); |
| 252 | 39 | } else if (fieldClass == java.lang.Long.class) { |
| 253 | 4 | columnInfo.setTypefactory(PoemTypeFactory.LONG); |
| 254 | 35 | } else if (fieldClass == long.class) { |
| 255 | 4 | columnInfo.setTypefactory(PoemTypeFactory.LONG); |
| 256 | 31 | } else if (fieldClass == java.math.BigDecimal.class) { |
| 257 | 4 | columnInfo.setTypefactory(PoemTypeFactory.BIGDECIMAL); |
| 258 | 4 | columnInfo.setPrecision(22); |
| 259 | 4 | columnInfo.setScale(2); |
| 260 | 27 | } else if (fieldClass == java.lang.String.class) { |
| 261 | 14 | columnInfo.setTypefactory(PoemTypeFactory.STRING); |
| 262 | 14 | columnInfo.setSize(-1); |
| 263 | 13 | } else if (fieldClass == java.sql.Date.class) { |
| 264 | 4 | columnInfo.setTypefactory(PoemTypeFactory.DATE); |
| 265 | 9 | } else if (fieldClass == java.sql.Timestamp.class) { |
| 266 | 4 | columnInfo.setTypefactory(PoemTypeFactory.TIMESTAMP); |
| 267 | 5 | } else if (fieldClass == byte[].class) { |
| 268 | 1 | columnInfo.setTypefactory(PoemTypeFactory.BINARY); |
| 269 | |
} |
| 270 | |
else { |
| 271 | 4 | Table<?> referredTable = fromClass(table.getDatabase(), fieldClass); |
| 272 | 8 | columnInfo.setTypefactory(PoemTypeFactory.forCode(table.getDatabase(), |
| 273 | 4 | referredTable.getInfo().troid().intValue())); |
| 274 | |
} |
| 275 | 64 | columnInfo.makePersistent(); |
| 276 | 64 | table.addColumnAndCommit(columnInfo); |
| 277 | 64 | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
static final class Prop { |
| 283 | 76 | String name = null; |
| 284 | 76 | Class<?> set = null; |
| 285 | 76 | Class<?> got = null; |
| 286 | |
|
| 287 | 76 | Prop(String name) { |
| 288 | 76 | this.name = name; |
| 289 | 76 | } |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public String getName() { |
| 295 | 64 | return name; |
| 296 | |
} |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public Class<?> getGot() { |
| 302 | 384 | return got; |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public void setGot(Class<?> got) { |
| 310 | 76 | this.got = got; |
| 311 | 76 | } |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
public Class<?> getSet() { |
| 317 | 140 | return set; |
| 318 | |
} |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
public void setSet(Class<?> set) { |
| 325 | 64 | this.set = set; |
| 326 | 64 | } |
| 327 | |
|
| 328 | |
} |
| 329 | |
} |