use of com.google.appengine.api.datastore.Entity in project siena by mandubian.
the class GaeMappingUtils method mapEntitiesKeysOnly.
public static <T> List<T> mapEntitiesKeysOnly(List<Entity> entities, Class<T> clazz) {
Field id = ClassInfo.getIdField(clazz);
List<T> list = new ArrayList<T>(entities.size());
for (Entity entity : entities) {
T obj;
try {
obj = Util.createObjectInstance(clazz);
list.add(obj);
setIdFromKey(id, obj, entity.getKey());
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
}
return list;
}
use of com.google.appengine.api.datastore.Entity in project siena by mandubian.
the class GaeMappingUtils method createEntityInstance.
public static Entity createEntityInstance(Field idField, ClassInfo info, Object obj) {
Entity entity = null;
Id id = idField.getAnnotation(Id.class);
Class<?> type = idField.getType();
if (id != null) {
switch(id.value()) {
case NONE:
Object idVal = null;
idVal = Util.readField(obj, idField);
if (idVal == null)
throw new SienaException("Id Field " + idField.getName() + " value null");
String keyVal = Util.toString(idField, idVal);
entity = new Entity(info.tableName, keyVal);
break;
case AUTO_INCREMENT:
// manages String ID as not long!!!
if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
entity = new Entity(info.tableName);
} else {
Object idStringVal = null;
idStringVal = Util.readField(obj, idField);
if (idStringVal == null)
throw new SienaException("Id Field " + idField.getName() + " value null");
String keyStringVal = Util.toString(idField, idStringVal);
entity = new Entity(info.tableName, keyStringVal);
}
break;
case UUID:
entity = new Entity(info.tableName, UUID.randomUUID().toString());
break;
default:
throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
return entity;
}
use of com.google.appengine.api.datastore.Entity in project siena by mandubian.
the class GaeMappingUtils method mapEntities.
public static <T> List<T> mapEntities(Iterable<Entity> entities, Class<T> clazz) {
Field id = ClassInfo.getIdField(clazz);
List<T> list = new ArrayList<T>();
for (Entity entity : entities) {
T obj;
try {
obj = Util.createObjectInstance(clazz);
fillModel(obj, entity);
list.add(obj);
setIdFromKey(id, obj, entity.getKey());
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
}
return list;
}
use of com.google.appengine.api.datastore.Entity 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.Entity 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);
}
Aggregations