Search in sources :

Example 1 with QueryJoin

use of siena.QueryJoin in project siena by mandubian.

the class JdbcMappingUtils method getJoinFields.

public static <T> List<Field> getJoinFields(Query<T> query) {
    List<Field> joinFields = null;
    // adds all join fields brought by call to .join() functions
    if (query.getJoins().size() > 0) {
        joinFields = new ArrayList<Field>();
        for (QueryJoin join : query.getJoins()) joinFields.add(join.field);
    }
    // then adds the remaining joins coming from @Join if not added yet 
    ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
    if (ci.joinFields.size() > 0) {
        if (joinFields == null)
            joinFields = new ArrayList<Field>();
        for (Field f : ci.joinFields) {
            if (!joinFields.contains(f))
                joinFields.add(f);
        }
    }
    return joinFields;
}
Also used : Field(java.lang.reflect.Field) QueryJoin(siena.QueryJoin) ArrayList(java.util.ArrayList) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) ClassInfo(siena.ClassInfo)

Example 2 with QueryJoin

use of siena.QueryJoin in project siena by mandubian.

the class BaseTest method testDumpRestoreQueryJoin.

public void testDumpRestoreQueryJoin() {
    Query<Discovery> query = pm.createQuery(Discovery.class).join("discoverer", "firstName");
    QueryJoin qj = (QueryJoin) query.getJoins().get(0);
    String str = JsonSerializer.serialize(qj).toString();
    assertNotNull(str);
    QueryJoin qjRes = (QueryJoin) JsonSerializer.deserialize(QueryJoin.class, Json.loads(str));
    assertNotNull(qjRes);
    assertEquals(qj.field.getName(), qjRes.field.getName());
    for (int i = 0; i < qjRes.sortFields.length; i++) {
        assertEquals(qj.sortFields[i], qjRes.sortFields[i]);
    }
}
Also used : Discovery(siena.base.test.model.Discovery) QueryJoin(siena.QueryJoin)

Example 3 with QueryJoin

use of siena.QueryJoin in project siena by mandubian.

the class SdbPersistenceManager method mapJoins.

protected <T> List<T> mapJoins(Query<T> query, List<T> models) {
    List<QueryJoin> joins = query.getJoins();
    // join queries
    Map<Class<?>, Map<String, Object>> classMap = new HashMap<Class<?>, Map<String, Object>>();
    // joins in query
    for (final T model : models) {
        for (QueryJoin join : joins) {
            Field field = join.field;
            Map<String, Object> strMap = classMap.get(field.getType());
            if (strMap == null) {
                strMap = new HashMap<String, Object>();
                classMap.put(field.getType(), strMap);
            }
            String itemName = SdbMappingUtils.toString(Util.readField(model, field));
            strMap.put(itemName, null);
        }
        // join annotations
        for (Field field : ClassInfo.getClassInfo(query.getQueriedClass()).joinFields) {
            Map<String, Object> strMap = classMap.get(field.getType());
            if (strMap == null) {
                strMap = new HashMap<String, Object>();
                classMap.put(field.getType(), strMap);
            }
            String itemName = SdbMappingUtils.toString(Util.readField(model, field));
            strMap.put(itemName, null);
        }
    }
    for (Class<?> clazz : classMap.keySet()) {
        List<?> objs = this.getByKeys(clazz, classMap.get(clazz).keySet());
        Map<String, Object> strMap = classMap.get(clazz);
        for (Object obj : objs) {
            String itemName = SdbMappingUtils.getItemName(clazz, obj);
            strMap.put(itemName, obj);
        }
    }
    for (T model : models) {
        for (QueryJoin join : joins) {
            Field field = join.field;
            String itemName = SdbMappingUtils.toString(Util.readField(model, field));
            Util.setField(model, field, classMap.get(field.getType()).get(itemName));
        }
        for (Field field : ClassInfo.getClassInfo(model.getClass()).joinFields) {
            String itemName = SdbMappingUtils.toString(Util.readField(model, field));
            Util.setField(model, field, classMap.get(field.getType()).get(itemName));
        }
    }
    return models;
}
Also used : HashMap(java.util.HashMap) QueryJoin(siena.QueryJoin) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with QueryJoin

use of siena.QueryJoin in project siena by mandubian.

the class SdbPersistenceManager method mapJoins.

protected <T> T mapJoins(Query<T> query, T model) {
    List<QueryJoin> joins = query.getJoins();
    // join queries
    Map<Class<?>, Map<String, Object>> classMap = new HashMap<Class<?>, Map<String, Object>>();
    // joins in query
    for (QueryJoin join : joins) {
        Field field = join.field;
        Map<String, Object> strMap = classMap.get(field.getType());
        if (strMap == null) {
            strMap = new HashMap<String, Object>();
            classMap.put(field.getType(), strMap);
        }
        String itemName = SdbMappingUtils.toString(Util.readField(model, field));
        strMap.put(itemName, null);
    }
    // join annotations
    for (Field field : ClassInfo.getClassInfo(query.getQueriedClass()).joinFields) {
        Map<String, Object> strMap = classMap.get(field.getType());
        if (strMap == null) {
            strMap = new HashMap<String, Object>();
            classMap.put(field.getType(), strMap);
        }
        String itemName = SdbMappingUtils.toString(Util.readField(model, field));
        strMap.put(itemName, null);
    }
    for (Class<?> clazz : classMap.keySet()) {
        List<?> objs = this.getByKeys(clazz, classMap.get(clazz).keySet());
        Map<String, Object> strMap = classMap.get(clazz);
        for (Object obj : objs) {
            String itemName = SdbMappingUtils.getItemName(clazz, obj);
            strMap.put(itemName, obj);
        }
    }
    for (Field field : ClassInfo.getClassInfo(model.getClass()).joinFields) {
        String itemName = SdbMappingUtils.toString(Util.readField(model, field));
        Util.setField(model, field, classMap.get(field.getType()).get(itemName));
    }
    return model;
}
Also used : HashMap(java.util.HashMap) QueryJoin(siena.QueryJoin) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with QueryJoin

use of siena.QueryJoin in project siena by mandubian.

the class GaeQueryUtils method buildJoinFieldKeysMap.

public static <T> Map<Field, ArrayList<Key>> buildJoinFieldKeysMap(QueryData<T> query) {
    List<QueryJoin> joins = query.getJoins();
    // join queries
    Map<Field, ArrayList<Key>> fieldMap = new HashMap<Field, ArrayList<Key>>();
    for (QueryJoin join : joins) {
        Field field = join.field;
        if (!ClassInfo.isModel(field.getType())) {
            throw new SienaRestrictedApiException(GaePersistenceManager.DB, "join", "Join not possible: Field " + field.getName() + " is not a relation field");
        } else if (join.sortFields != null && join.sortFields.length != 0)
            throw new SienaRestrictedApiException(GaePersistenceManager.DB, "join", "Join not allowed with sort fields");
        fieldMap.put(field, new ArrayList<Key>());
    }
    // join annotations
    for (Field field : ClassInfo.getClassInfo(query.getQueriedClass()).joinFields) {
        fieldMap.put(field, new ArrayList<Key>());
    }
    return fieldMap;
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) SienaRestrictedApiException(siena.SienaRestrictedApiException) QueryJoin(siena.QueryJoin) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key)

Aggregations

QueryJoin (siena.QueryJoin)6 Field (java.lang.reflect.Field)5 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JdbcClassInfo (siena.jdbc.JdbcPersistenceManager.JdbcClassInfo)2 Key (com.google.appengine.api.datastore.Key)1 ClassInfo (siena.ClassInfo)1 QueryOrder (siena.QueryOrder)1 SienaException (siena.SienaException)1 SienaRestrictedApiException (siena.SienaRestrictedApiException)1 Discovery (siena.base.test.model.Discovery)1