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;
}
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]);
}
}
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;
}
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;
}
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;
}
Aggregations