use of siena.ClassInfo 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());
}
use of siena.ClassInfo in project siena by mandubian.
the class GaeMappingUtils method fillModelAndKey.
public static void fillModelAndKey(Object obj, Entity entity) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field id = info.getIdField();
Class<?> fieldClass = id.getType();
Key key = entity.getKey();
if (key != null) {
setIdFromKey(id, obj, key);
}
for (Field field : info.updateFields) {
String property = ClassInfo.getColumnNames(field)[0];
try {
fieldClass = field.getType();
if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
/*if(!ClassInfo.isAggregated(field)){*/
key = (Key) entity.getProperty(property);
if (key != null) {
Object value = Util.createObjectInstance(fieldClass);
id = ClassInfo.getIdField(fieldClass);
setIdFromKey(id, value, key);
Util.setField(obj, field, value);
}
/*}*/
} else if (ClassInfo.isEmbedded(field) && field.getAnnotation(Embedded.class).mode() == Embedded.Mode.NATIVE) {
Object value = GaeNativeSerializer.unembed(field.getType(), ClassInfo.getSingleColumnName(field), entity, 0);
Util.setField(obj, field, value);
} else /*else if(ClassInfo.isAggregated(field)){
// does nothing for the time being
}
else if (ClassInfo.isOwned(field)){
// does nothing for the time being
}*/
{
setFromObject(obj, field, entity.getProperty(property));
}
} catch (Exception e) {
throw new SienaException(e);
}
}
}
use of siena.ClassInfo in project siena by mandubian.
the class GaeMappingUtils method getKeyFromParent.
public static Key getKeyFromParent(Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
try {
Field idField = info.getIdField();
Object value = Util.readField(obj, idField);
// TODO verify that returning NULL is not a bad thing
if (value == null)
return null;
Class<?> type = idField.getType();
if (idField.isAnnotationPresent(Id.class)) {
Id id = idField.getAnnotation(Id.class);
switch(id.value()) {
case NONE:
// long or string goes toString
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
case AUTO_INCREMENT:
// as a string with auto_increment can't exist, it is not cast into long
if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), (Long) value);
}
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
case UUID:
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
default:
throw new SienaException("Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
} catch (Exception e) {
throw new SienaException(e);
}
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method _insertMultipleMapFromParent.
private int _insertMultipleMapFromParent(final Map<Key, Map<Field, List<Object>>> keyMap, final List<ClassInfo> parentInfos) {
List<Entity> entities = new ArrayList<Entity>();
int i = 0;
for (Key key : keyMap.keySet()) {
Map<Field, List<Object>> objectMap = keyMap.get(key);
for (Field field : objectMap.keySet()) {
for (Object obj : objectMap.get(field)) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
Entity entity = GaeMappingUtils.createEntityInstanceFromParent(idField, info, obj, key, parentInfos.get(i), field);
GaeMappingUtils.fillEntity(obj, entity);
entities.add(entity);
}
}
i++;
}
List<Key> generatedKeys = ds.put(entities);
i = 0;
Map<Key, Map<Field, List<Object>>> recKeyMap = new HashMap<Key, Map<Field, List<Object>>>();
List<ClassInfo> recInfos = new ArrayList<ClassInfo>();
for (Key key : keyMap.keySet()) {
Map<Field, List<Object>> objectMap = keyMap.get(key);
for (Field field : objectMap.keySet()) {
for (Object obj : objectMap.get(field)) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
GaeMappingUtils.setIdFromKey(idField, obj, generatedKeys.get(i));
if (info.hasAggregatedFields) {
recInfos.add(info);
Map<Field, List<Object>> recObjectMap = new HashMap<Field, List<Object>>();
recKeyMap.put(generatedKeys.get(i), recObjectMap);
_populateAggregateFieldMap(recObjectMap, info, obj);
}
i++;
}
}
}
if (!recKeyMap.isEmpty()) {
_insertMultipleMapFromParent(recKeyMap, recInfos);
}
return generatedKeys.size();
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method _deleteMultiple.
private void _deleteMultiple(Iterable<?> objects, List<Key> keys, final Key parentKey, final ClassInfo parentInfo, final Field parentField) {
for (Object obj : objects) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Key key;
if (parentKey == null) {
key = GaeMappingUtils.getKey(obj);
} else {
key = GaeMappingUtils.getKeyFromParent(obj, parentKey, parentInfo, parentField);
}
// cascading on aggregated fields
if (!info.aggregatedFields.isEmpty()) {
for (Field f : info.aggregatedFields) {
if (ClassInfo.isModel(f.getType())) {
Object aggObj = Util.readField(obj, f);
_deleteSingle(aggObj, keys, key, info, f);
} else if (ClassInfo.isOne(f)) {
One<?> one = (One<?>) Util.readField(obj, f);
Object target = one.get();
if (target != null) {
_deleteSingle(target, keys, key, info, f);
}
} else if (ClassInfo.isMany(f)) {
Many<?> lq = (Many<?>) Util.readField(obj, f);
if (!lq.asList().isEmpty()) {
_deleteMultiple(lq.asQuery().fetchKeys(), keys, key, info, f);
}
}
}
}
keys.add(key);
}
}
Aggregations