use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManager method _deleteSingle.
private void _deleteSingle(Object obj, List<Key> keys, final Key parentKey, final ClassInfo parentInfo, final Field parentField) {
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.isMany(f)) {
Many<?> lq = (Many<?>) Util.readField(obj, f);
if (!lq.asList().isEmpty()) {
_deleteMultiple(lq.asQuery().fetchKeys(), keys, key, info, f);
}
}
}
}
keys.add(key);
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManager method _insertSingle.
private <T> void _insertSingle(T 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);
_insertSingle(obj, parentKey, rel.target, parentInfo, (Field) rel.discriminator);
} else {
_insertSingle(obj, null, null, null, null);
}
} else {
_insertSingle(obj, null, null, null, null);
}
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManagerAsync method getByKey.
public <T> SienaFuture<T> getByKey(final Class<T> clazz, final Object key) {
Key gkey = GaeMappingUtils.makeKeyFromId(clazz, key);
try {
Future<Entity> future = ds.get(gkey);
Future<T> wrapped = new SienaFutureWrapper<Entity, T>(future) {
@Override
protected T wrap(Entity entity) throws Exception {
T obj = Util.createObjectInstance(clazz);
GaeMappingUtils.fillModelAndKey(obj, entity);
return obj;
}
};
return new SienaFutureContainer<T>(wrapped);
} catch (Exception e) {
throw new SienaException(e);
}
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaeMappingUtils method createEntityInstanceForUpdateFromParent.
public static Entity createEntityInstanceForUpdateFromParent(ClassInfo info, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
Key key = makeKeyFromParent(info, obj, parentKey, parentInfo, parentField);
Entity entity = new Entity(key);
return entity;
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaeMappingUtils method fillModel.
public static void fillModel(Object obj, Entity entity) {
Class<?> clazz = obj.getClass();
for (Field field : ClassInfo.getClassInfo(clazz).updateFields) {
String property = ClassInfo.getColumnNames(field)[0];
try {
Class<?> fieldClass = field.getType();
if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
/*if(!ClassInfo.isAggregated(field)){*/
Key key = (Key) entity.getProperty(property);
if (key != null) {
Object value = Util.createObjectInstance(fieldClass);
Field id = ClassInfo.getIdField(fieldClass);
setIdFromKey(id, value, key);
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
}*/
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 {
setFromObject(obj, field, entity.getProperty(property));
}
} catch (Exception e) {
throw new SienaException(e);
}
}
}
Aggregations