Search in sources :

Example 1 with QueryAggregated

use of siena.QueryAggregated 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 QueryAggregated

use of siena.QueryAggregated in project siena by mandubian.

the class GaePersistenceManager method prepareKeysOnly.

private <T> PreparedQuery prepareKeysOnly(Query<T> query) {
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    com.google.appengine.api.datastore.Query q;
    // manages aggregation at first
    List<QueryAggregated> aggregs = query.getAggregatees();
    if (aggregs.isEmpty()) {
        q = new com.google.appengine.api.datastore.Query(ClassInfo.getClassInfo(clazz).tableName);
    } else if (aggregs.size() == 1) {
        QueryAggregated aggreg = aggregs.get(0);
        q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(info, ClassInfo.getClassInfo(aggreg.aggregator.getClass()), aggreg.field));
        q.setAncestor(GaeMappingUtils.getKey(aggreg.aggregator));
    } else {
        throw new SienaException("Only one aggregation per query allowed");
    }
    return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q).setKeysOnly());
}
Also used : PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated) ClassInfo(siena.ClassInfo)

Example 3 with QueryAggregated

use of siena.QueryAggregated in project siena by mandubian.

the class GaePersistenceManager method prepare.

private <T> PreparedQuery prepare(Query<T> query) {
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    com.google.appengine.api.datastore.Query q;
    // manages aggregation at first
    List<QueryAggregated> aggregs = query.getAggregatees();
    if (aggregs.isEmpty()) {
        q = new com.google.appengine.api.datastore.Query(info.tableName);
        return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q));
    } else if (aggregs.size() == 1) {
        QueryAggregated aggreg = aggregs.get(0);
        Key ancestorKey = GaeMappingUtils.getKey(aggreg.aggregator);
        q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(info, ClassInfo.getClassInfo(aggreg.aggregator.getClass()), aggreg.field));
        q.setAncestor(ancestorKey);
        return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q, ancestorKey));
    } else {
        throw new SienaException("Only one aggregation per query allowed");
    }
}
Also used : PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 4 with QueryAggregated

use of siena.QueryAggregated in project siena by mandubian.

the class GaeMappingUtils method mapRelations.

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

Aggregations

QueryAggregated (siena.QueryAggregated)4 SienaException (siena.SienaException)4 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 ClassInfo (siena.ClassInfo)2 Query (siena.Query)2 QueryOwned (siena.QueryOwned)2 Relation (siena.core.Relation)2 Key (com.google.appengine.api.datastore.Key)1