use of siena.ClassInfo 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);
}
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method map.
protected <T> List<T> map(Query<T> query, List<Entity> entities) {
Class<T> clazz = query.getQueriedClass();
List<T> results = GaeMappingUtils.mapEntities(entities, clazz);
ClassInfo info = ClassInfo.getClassInfo(clazz);
// maps model relations to be able to associate children to aggregators
GaeMappingUtils.mapRelations(query, results, info);
// related fields (Many<T> management mainly)
if (!info.ownedFields.isEmpty()) {
mapOwned(results);
}
// aggregated management
if (!info.aggregatedFields.isEmpty()) {
mapAggregated(results);
}
// join management
if (!query.getJoins().isEmpty() || !info.joinFields.isEmpty())
mapJoins(query, results);
return results;
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method _updateManageMaps.
private int _updateManageMaps(HashMap<PersistenceType, List<Entity>> entitiesMap, HashMap<PersistenceType, List<Object>> objectsMap, HashMap<PersistenceType, List<Key>> keysMap) {
int nb = 0;
// saves the updated owned objects
List<Object> objs = objectsMap.get(PersistenceType.SAVE);
if (objs != null && !objs.isEmpty()) {
nb += save(objs);
}
// saves the updated aggregated objects
List<Entity> entities = entitiesMap.get(PersistenceType.INSERT);
if (entities != null && !entities.isEmpty()) {
List<Key> generatedKeys = ds.put(entities);
int i = 0;
for (Object elt : objectsMap.get(PersistenceType.INSERT)) {
Class<?> clazz = elt.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
GaeMappingUtils.setIdFromKey(idField, elt, generatedKeys.get(i));
}
nb += generatedKeys.size();
}
// saves the updated aggregated objects
entities = entitiesMap.get(PersistenceType.UPDATE);
if (entities != null && !entities.isEmpty()) {
ds.put(entitiesMap.get(PersistenceType.UPDATE));
nb += entities.size();
}
// removes the deleted aggregated objects
List<Key> keys = keysMap.get(PersistenceType.DELETE);
if (keys != null && !keys.isEmpty()) {
ds.delete(keys);
nb += keys.size();
}
return nb;
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method mapAggregated.
protected <T> T mapAggregated(T model) {
Class<?> clazz = model.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
// if there is NO listquery, we use the kindless request
for (Field f : info.aggregatedFields) {
//ClassInfo fInfo = ClassInfo.getClassInfo(fClazz);
if (ClassInfo.isOne(f)) {
One4PM<?> one = (One4PM<?>) Util.readField(model, f);
// sets the sync flag to false to tell that it should be fetched when the one is accessed!
one.setSync(false);
//hasOneMany = true;
} else if (ClassInfo.isMany(f)) {
Many4PM<?> lq = (Many4PM<?>) Util.readField(model, f);
// sets the sync flag to false to tell that it should be fetched when the many is accessed!
lq.setSync(false);
//hasOneMany = true;
}
}
/*if(!hasOneMany){
// creates a kindless query to retrieve all subentities at once.
com.google.appengine.api.datastore.Query q =
new com.google.appengine.api.datastore.Query();
Key parentKey = GaeMappingUtils.getKey(model);
q.setAncestor(parentKey);
// this removes the parent from query
q.addFilter(Entity.KEY_RESERVED_PROPERTY,
com.google.appengine.api.datastore.Query.FilterOperator.GREATER_THAN,
parentKey);
PreparedQuery pq = ds.prepare(q);
List<Entity> childEntities = pq.asList(FetchOptions.Builder.withDefaults());
for(Field f: modelMap.keySet()){
Class<?> fClazz = f.getType();
ClassInfo fInfo = modelMap.get(f);
String kind = GaeMappingUtils.getKindWithAncestorField(fInfo, info, f);
Entity found = null;
for(Entity e: childEntities){
if(kind.equals(e.getKind())){
found = e;
childEntities.remove(e);
break;
}
}
if(found != null){
Object fObj = GaeMappingUtils.mapEntity(found, fClazz);
Util.setField(model, f, fObj);
}
}
}
else {
for(Field f: modelMap.keySet()){
Class<?> fClazz = f.getType();
ClassInfo fInfo = modelMap.get(f);
String kind = GaeMappingUtils.getKindWithAncestorField(fInfo, info, f);
com.google.appengine.api.datastore.Query q =
new com.google.appengine.api.datastore.Query(kind);
Key parentKey = GaeMappingUtils.getKey(model);
q.setAncestor(parentKey);
PreparedQuery pq = ds.prepare(q);
Entity childEntity = pq.asSingleEntity();
Object fObj = GaeMappingUtils.mapEntity(childEntity, fClazz);
Util.setField(model, f, fObj);
}
}*/
return model;
}
use of siena.ClassInfo in project siena by mandubian.
the class GaePersistenceManager method getByKeys.
public <T> List<T> getByKeys(Class<T> clazz, Iterable<?> keys) {
List<Key> gaeKeys = new ArrayList<Key>();
ClassInfo info = ClassInfo.getClassInfo(clazz);
for (Object key : keys) {
gaeKeys.add(GaeMappingUtils.makeKeyFromId(clazz, key));
}
Map<Key, Entity> entityMap = ds.get(gaeKeys);
List<T> models = new ArrayList<T>(entityMap.size());
for (Object key : keys) {
Entity entity = entityMap.get(GaeMappingUtils.makeKeyFromId(clazz, key));
T obj = null;
if (entity != null) {
obj = GaeMappingUtils.mapEntity(entity, clazz);
if (obj != null) {
// 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);
}
}
}
models.add(obj);
}
return models;
}
Aggregations