/** * e.g. IF PersonService extends DAO<Person> THEN entity == Person */ private static Class<?> inferEntityType(Class<? extends DAO<?>> daoClass) { U.must(daoClass.getSuperclass() == DAO.class, "Expected DAO to be superclass of %s, but found: %s!", daoClass, daoClass.getSuperclass()); ParameterizedType genDao = Cls.generic(daoClass.getGenericSuperclass()); U.must(genDao != null && genDao.getActualTypeArguments().length > 0, "Cannot infer entity type for: %s", daoClass); Type arg = genDao.getActualTypeArguments()[0]; return Cls.clazz(arg); }
@Override public E get() { return Cls.convert(map.get(name), type); }
@Override public Database map(String name) throws Exception { String dbFilename = DB.path() + name + ".db"; return (Database) Cls.newInstance(DB.DB_IMPL_CLASS, name, dbFilename); } });
@Override public boolean eval(Prop prop) throws Exception { return Cls .isAssignableTo(prop.getType(), Number.class, String.class, Boolean.class, Enum.class, Date.class); } };
public static void start() { db = (Database) Cls.customizable(DB.DB_IMPL_CLASS, "default", path() + "default.db"); db.loadAndStart(); }
private RelPair getRelPair(Object entity, Prop prop, String rel, boolean inverse) { Class<?> cls = prop.getRawTypeArg(0); Class<? extends Object> entCls = Cls.unproxy(entity.getClass()); Class<?> srcType = inverse ? cls : entCls; Class<?> destType = inverse ? entCls : cls; Tuple key = new Tuple(rel, srcType, destType); RelPair relPair = data.relPairs.get(key); Prop srcProp, destProp; if (relPair != null) { srcProp = relPair.srcProp; destProp = relPair.destProp; } else { String invRel = inverse ? rel : "^" + rel; Prop p = findRelProperty(cls, invRel, entCls); srcProp = inverse ? p : prop; destProp = inverse ? prop : p; relPair = new RelPair(rel, srcType, destType, srcProp, destProp); data.relPairs.putIfAbsent(key, relPair); if (srcType == null || srcProp == null || destType == null || destProp == null) { Log.warn("Incomplete relation pair!", "relation", relPair); } } return relPair; }
@SuppressWarnings("unchecked") public static <E extends IEntity> E create(Class<E> type, Map<String, ?> values) { EntityImpl entity = new EntityImpl(type); E proxy = Cls.createProxy(new DbProxy(entity, type), type); entity.setProxy(proxy); Beany.update(proxy, (Map<String, Object>) values, true); return proxy; }
public DbColumn<?> column(Method method) { U.must(DbColumn.class.isAssignableFrom(method.getReturnType())); String name = method.getName(); DbColumn<?> res = columns.get(name); if (res == null) { Class<Object> colType = Cls.clazz(Cls.generic(method.getGenericReturnType()).getActualTypeArguments()[0]); DbColumn<Object> value = DB.column(values, method.getName(), colType); DbColumn<?> old = columns.putIfAbsent(name, value); return U.or(old, value); } return res; }
@Override public void set(E value) { if (value != null) { map.put(name, Cls.convert(value, type)); } else { map.remove(name); } }
@SuppressWarnings("unchecked") @Override public <E> E entity(Class<E> clazz, Map<String, ?> properties) { U.notNull(clazz, "entity class"); Class<E> entityType = getEntityTypeFor(clazz); U.notNull(entityType, "entity type"); if (entityType.isInterface() && IEntity.class.isAssignableFrom(entityType)) { Class<? extends IEntity> cls = (Class<? extends IEntity>) entityType; return (E) DbProxy.create(cls, properties); } else { E entity = Cls.newInstance(entityType); Beany.update(entity, (Map<String, Object>) properties, true); return entity; } }
public long getVersionOf(long id) { Object ver = readColumn(id, VERSION); return ver != null ? Cls.convert(ver, Long.class) : 0; }