use of com.orm.androrm.field.ManyToManyField in project androrm by androrm.
the class QueryBuilder method getRelationSelection.
@SuppressWarnings("unchecked")
private static final <T extends Model> SelectStatement getRelationSelection(Relation<?> r, Class<T> clazz, Rule rule) {
Class<? extends Model> target = r.getTarget();
Statement stmt = rule.getStatement();
Where where = new Where();
SelectStatement select = new SelectStatement();
if (r instanceof ManyToManyField) {
ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) r;
stmt.setKey(DatabaseBuilder.getTableName(target));
where.setStatement(stmt);
select.from(m.getRelationTableName()).select(DatabaseBuilder.getTableName(clazz));
}
if (r instanceof OneToManyField) {
String backLinkFieldName = Model.getBackLinkFieldName(target, clazz);
stmt.setKey(backLinkFieldName);
where.setStatement(stmt);
select.from(DatabaseBuilder.getTableName(target)).select(backLinkFieldName + " AS " + DatabaseBuilder.getTableName(clazz));
}
select.where(where).distinct();
return select;
}
use of com.orm.androrm.field.ManyToManyField in project androrm by androrm.
the class DatabaseBuilder method getFieldDefinitions.
private static final <T extends Model> void getFieldDefinitions(T instance, Class<T> clazz, TableDefinition modelTable) throws IllegalArgumentException, IllegalAccessException {
if (clazz != null && clazz.isInstance(instance)) {
ModelCache.addModel(clazz);
for (Field field : getFields(clazz, instance)) {
String name = field.getName();
Object o = field.get(instance);
if (o instanceof DataField) {
DataField<?> fieldObject = (DataField<?>) o;
modelTable.addField(name, fieldObject);
}
if (o instanceof ManyToManyField) {
modelTable.addRelationalClass(clazz);
}
}
getFieldDefinitions(instance, Model.getSuperclass(clazz), modelTable);
}
}
use of com.orm.androrm.field.ManyToManyField 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);
}
}
use of com.orm.androrm.field.ManyToManyField in project androrm by androrm.
the class Model method saveM2MToDatabase.
@SuppressWarnings("unchecked")
private <T extends Model> void saveM2MToDatabase(Context context, Class<T> clazz, Object field) {
ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) field;
List<? extends Model> targets = m.getCachedValues();
DatabaseAdapter adapter = DatabaseAdapter.getInstance(context);
for (Model target : targets) {
/*
* Only save relation to the database if the
* target model has been persisted.
*/
if (target.getId() != 0) {
ContentValues values = new ContentValues();
Where where = new Where();
where.and(DatabaseBuilder.getTableName(clazz), getId()).and(DatabaseBuilder.getTableName(m.getTarget()), target.getId());
values.put(DatabaseBuilder.getTableName(clazz), getId());
values.put(DatabaseBuilder.getTableName(m.getTarget()), target.getId());
adapter.doInsertOrUpdate(m.getRelationTableName(), values, where);
}
}
}
Aggregations