use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class Model method getForeignKeyField.
private static final <T extends Model, O extends Model> Field getForeignKeyField(Class<T> target, Class<O> originClass, O origin) throws IllegalArgumentException, IllegalAccessException {
Field fk = null;
if (originClass != null && originClass.isInstance(origin)) {
for (Field field : DatabaseBuilder.getFields(originClass, origin)) {
Object f = field.get(origin);
if (f instanceof ForeignKeyField) {
ForeignKeyField<?> tmp = (ForeignKeyField<?>) f;
Class<? extends Model> t = tmp.getTarget();
if (t.equals(target)) {
fk = field;
break;
}
}
}
if (fk == null) {
fk = getForeignKeyField(target, getSuperclass(originClass), origin);
}
}
return fk;
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class QueryBuilder method resolveLastField.
private static final <T extends Model> SelectStatement resolveLastField(Object field, Class<T> clazz, Rule rule) {
SelectStatement select = new SelectStatement();
if (DatabaseBuilder.isRelationalField(field) && !(field instanceof ForeignKeyField)) {
Relation<?> r = (Relation<?>) field;
return getRelationSelection(r, clazz, rule);
}
String tableName = DatabaseBuilder.getTableName(clazz);
Where where = new Where();
where.setStatement(rule.getStatement());
select.from(tableName).distinct().select(Model.PK + " AS " + tableName).where(where);
return select;
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class ForeignKeyFieldTest method testReset.
public void testReset() {
Product p = new Product();
p.setName("test product");
p.save(getContext());
ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
fk.set(p);
fk.reset();
assertNull(fk.get(getContext()));
fk.set(p.getId());
fk.reset();
assertNull(fk.get(getContext()));
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class ForeignKeyFieldTest method testIsPersisted.
public void testIsPersisted() {
ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
Product p = new Product();
assertFalse(fk.isPersisted());
fk.set(p);
assertFalse(fk.isPersisted());
p.save(getContext());
assertTrue(fk.isPersisted());
}
use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.
the class DatabaseBuilder method getRelationDefinitions.
@SuppressWarnings("unchecked")
private static final <T extends Model> void getRelationDefinitions(T instance, Class<T> clazz, List<TableDefinition> definitions) {
if (clazz != null && clazz.isInstance(instance)) {
for (Field field : getFields(clazz, instance)) {
try {
Object o = field.get(instance);
if (o instanceof ManyToManyField) {
ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) o;
String leftHand = getTableName(clazz);
String rightHand = getTableName(m.getTarget());
TableDefinition definition = new TableDefinition(m.getRelationTableName());
ForeignKeyField<T> leftLink = m.getLeftLinkDescriptor();
ForeignKeyField<?> rightLink = m.getRightHandDescriptor();
definition.addField(leftHand, leftLink);
definition.addField(rightHand, rightLink);
definitions.add(definition);
}
} catch (IllegalAccessException e) {
Log.e(TAG, "could not gather relation definitions for class " + clazz.getSimpleName(), e);
}
}
getRelationDefinitions(instance, Model.getSuperclass(clazz), definitions);
}
}
Aggregations