Search in sources :

Example 6 with Many4PM

use of siena.core.Many4PM 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;
}
Also used : One4PM(siena.core.One4PM) Field(java.lang.reflect.Field) Many4PM(siena.core.Many4PM) ClassInfo(siena.ClassInfo)

Aggregations

Field (java.lang.reflect.Field)6 Many4PM (siena.core.Many4PM)6 ClassInfo (siena.ClassInfo)4 One4PM (siena.core.One4PM)4 Entity (com.google.appengine.api.datastore.Entity)2 ArrayList (java.util.ArrayList)2 FieldMapKeys (siena.ClassInfo.FieldMapKeys)2 Key (com.google.appengine.api.datastore.Key)1 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)1 QueryResultList (com.google.appengine.api.datastore.QueryResultList)1 List (java.util.List)1 Query (siena.Query)1 One (siena.core.One)1