use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManagerAsync method delete.
public <T> SienaFuture<Integer> delete(QueryAsync<T> query) {
final ArrayList<Key> keys = new ArrayList<Key>();
for (final Entity entity : prepareKeysOnly(query).asIterable(FetchOptions.Builder.withDefaults())) {
keys.add(entity.getKey());
}
Future<Void> future = ds.delete(keys);
Future<Integer> wrapped = new SienaFutureWrapper<Void, Integer>(future) {
@Override
protected Integer wrap(Void v) throws Exception {
return keys.size();
}
};
return new SienaFutureContainer<Integer>(wrapped);
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManagerAsync method mapJoins.
protected <T> T mapJoins(QueryAsync<T> query, T model) {
try {
// join queries
Map<Field, ArrayList<Key>> fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(query);
// creates the list of joined entity keys to extract
for (Field field : fieldMap.keySet()) {
Key key = GaeMappingUtils.getKey(field.get(model));
List<Key> keys = fieldMap.get(field);
if (!keys.contains(key))
keys.add(key);
}
Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
try {
// retrieves all joined entities per field
for (Field field : fieldMap.keySet()) {
Future<Map<Key, Entity>> entities = ds.get(fieldMap.get(field));
// gets the future here because we need it!
entityMap.put(field, entities.get());
}
} catch (Exception ex) {
throw new SienaException(ex);
}
// associates linked models to their models
// linkedModels is just a map to contain entities already mapped
Map<Key, Object> linkedModels = new HashMap<Key, Object>();
Object linkedObj;
Entity entity;
for (Field field : fieldMap.keySet()) {
Object objVal = field.get(model);
Key key = GaeMappingUtils.getKey(objVal);
linkedObj = linkedModels.get(key);
if (linkedObj == null) {
entity = entityMap.get(field).get(key);
linkedObj = objVal;
GaeMappingUtils.fillModel(linkedObj, entity);
linkedModels.put(key, linkedObj);
}
field.set(model, linkedObj);
}
return model;
} catch (IllegalAccessException ex) {
throw new SienaException(ex);
}
}
use of com.google.appengine.api.datastore.Key in project siena by mandubian.
the class GaePersistenceManagerAsync method save.
public SienaFuture<Integer> save(final Iterable<?> objects) {
List<Entity> entities = new ArrayList<Entity>();
for (Object obj : objects) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
Entity entity;
Object idVal = Util.readField(obj, idField);
// id with null value means insert
if (idVal == null) {
entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
} else // id with not null value means update
{
entity = GaeMappingUtils.createEntityInstanceForUpdate(info, obj);
}
GaeMappingUtils.fillEntity(obj, entity);
entities.add(entity);
}
Future<List<Key>> future = ds.put(entities);
Future<Integer> wrapped = new SienaFutureWrapper<List<Key>, Integer>(future) {
@Override
protected Integer wrap(List<Key> keys) throws Exception {
int i = 0;
for (Object obj : objects) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
Object idVal = Util.readField(obj, idField);
if (idVal == null) {
GaeMappingUtils.setIdFromKey(idField, obj, keys.get(i++));
}
}
return keys.size();
}
};
return new SienaFutureContainer<Integer>(wrapped);
}
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 GaePersistenceManager method getByKey.
public <T> T getByKey(Class<T> clazz, Object key) {
Key gKey = GaeMappingUtils.makeKeyFromId(clazz, key);
ClassInfo info = ClassInfo.getClassInfo(clazz);
try {
Entity entity = ds.get(gKey);
T obj = null;
if (entity != null) {
obj = Util.createObjectInstance(clazz);
GaeMappingUtils.fillModelAndKey(obj, entity);
// related fields (Many<T> management mainly)
if (!info.ownedFields.isEmpty()) {
mapOwned(obj);
}
// aggregated management
if (!info.aggregatedFields.isEmpty()) {
mapAggregated(obj);
}
// join management
if (!info.joinFields.isEmpty()) {
mapJoins(obj);
}
}
return obj;
} catch (EntityNotFoundException e) {
return null;
} catch (Exception e) {
throw new SienaException(e);
}
}
Aggregations