Search in sources :

Example 1 with Relation

use of siena.core.Relation in project siena by mandubian.

the class GaeMappingUtils method mapRelation.

public static <T> T mapRelation(Query<T> query, T obj, ClassInfo info) {
    // aggregators/owners
    List<QueryAggregated> aggregs = query.getAggregatees();
    List<QueryOwned> ownees = query.getOwnees();
    if (aggregs.isEmpty() && ownees.isEmpty()) {
        return obj;
    }
    if (aggregs.size() == 1) {
        // aggregators
        QueryAggregated aggreg = aggregs.get(0);
        Relation rel = new Relation(RelationMode.AGGREGATION, aggreg.aggregator, aggreg.field);
        Util.setField(obj, info.aggregator, rel);
    } else if (aggregs.size() > 1) {
        throw new SienaException("Only one aggregation per query allowed");
    }
    if (ownees.size() == 1) {
        // owners
        QueryOwned ownee = ownees.get(0);
        Util.setField(obj, ownee.field, ownee.owner);
    } else if (ownees.size() > 1) {
        throw new SienaException("Only one owner per query allowed");
    }
    return obj;
}
Also used : QueryOwned(siena.QueryOwned) Relation(siena.core.Relation) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated)

Example 2 with Relation

use of siena.core.Relation in project siena by mandubian.

the class GaePersistenceManager method update.

public void update(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    if (info.hasAggregator) {
        Relation rel = (Relation) Util.readField(obj, info.aggregator);
        if (rel != null && rel.mode == RelationMode.AGGREGATION) {
            ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
            Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
            if (!info.hasAggregatedFields && !info.hasOwnedFields) {
                _updateSimple(obj, info, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _updateComplex(obj, parentKey, parentInfo, (Field) rel.discriminator);
            }
        } else {
            if (!info.hasAggregatedFields && !info.hasOwnedFields) {
                _updateSimple(obj, info, null, null, null);
            } else {
                _updateComplex(obj, null, null, null);
            }
        }
    } else {
        if (!info.hasAggregatedFields && !info.hasOwnedFields) {
            _updateSimple(obj, info, null, null, null);
        } else {
            _updateComplex(obj, null, null, null);
        }
    }
}
Also used : Relation(siena.core.Relation) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 3 with Relation

use of siena.core.Relation in project siena by mandubian.

the class GaePersistenceManager method delete.

public void delete(Object obj) {
    List<Key> keys = new ArrayList<Key>();
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    if (info.hasAggregator) {
        Relation rel = (Relation) Util.readField(obj, info.aggregator);
        if (rel != null && rel.mode == RelationMode.AGGREGATION) {
            ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
            Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
            _deleteSingle(obj, keys, parentKey, parentInfo, (Field) rel.discriminator);
        } else {
            _deleteSingle(obj, keys, null, null, null);
        }
    } else {
        _deleteSingle(obj, keys, null, null, null);
    }
    ds.delete(keys);
}
Also used : Relation(siena.core.Relation) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 4 with Relation

use of siena.core.Relation in project siena by mandubian.

the class GaePersistenceManager method _updateMultiple.

private <T> int _updateMultiple(Iterable<T> objects) {
    HashMap<PersistenceType, List<Entity>> entitiesMap = new HashMap<PersistenceType, List<Entity>>();
    HashMap<PersistenceType, List<Object>> objectsMap = new HashMap<PersistenceType, List<Object>>();
    HashMap<PersistenceType, List<Key>> keysMap = new HashMap<PersistenceType, List<Key>>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        if (info.hasAggregator) {
            Relation rel = (Relation) Util.readField(obj, info.aggregator);
            if (rel != null && rel.mode == RelationMode.AGGREGATION) {
                ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
                Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
                _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, null, null, null);
            }
        } else {
            _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, null, null, null);
        }
    }
    return _updateManageMaps(entitiesMap, objectsMap, keysMap);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Relation(siena.core.Relation) HashMap(java.util.HashMap) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 5 with Relation

use of siena.core.Relation in project siena by mandubian.

the class GaePersistenceManager method _insertMultiple.

private <T> int _insertMultiple(Iterable<T> objects) {
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        if (info.hasAggregator) {
            Relation rel = (Relation) Util.readField(obj, info.aggregator);
            if (rel != null && rel.mode == RelationMode.AGGREGATION) {
                ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
                Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
                _insertAddEntity(entities, obj, info, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _insertAddEntity(entities, obj, info, null, null, null);
            }
        } else {
            _insertAddEntity(entities, obj, info, null, null, null);
        }
    }
    return _insertPutEntities(entities, objects);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Relation(siena.core.Relation) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Aggregations

Relation (siena.core.Relation)7 Key (com.google.appengine.api.datastore.Key)5 ClassInfo (siena.ClassInfo)5 ArrayList (java.util.ArrayList)3 Entity (com.google.appengine.api.datastore.Entity)2 QueryAggregated (siena.QueryAggregated)2 QueryOwned (siena.QueryOwned)2 SienaException (siena.SienaException)2 QueryResultList (com.google.appengine.api.datastore.QueryResultList)1 HashMap (java.util.HashMap)1 List (java.util.List)1