Search in sources :

Example 36 with ClassInfo

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);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) ClassInfo(siena.ClassInfo)

Example 37 with ClassInfo

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;
}
Also used : ClassInfo(siena.ClassInfo)

Example 38 with ClassInfo

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;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Field(java.lang.reflect.Field) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 39 with ClassInfo

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

Example 40 with ClassInfo

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;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Aggregations

ClassInfo (siena.ClassInfo)68 Field (java.lang.reflect.Field)33 ArrayList (java.util.ArrayList)24 Key (com.google.appengine.api.datastore.Key)23 SienaException (siena.SienaException)21 Entity (com.google.appengine.api.datastore.Entity)17 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 List (java.util.List)10 DeletableItem (com.amazonaws.services.simpledb.model.DeletableItem)6 Item (com.amazonaws.services.simpledb.model.Item)6 ReplaceableItem (com.amazonaws.services.simpledb.model.ReplaceableItem)6 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)5 Relation (siena.core.Relation)5 IOException (java.io.IOException)4 QueryFilterSearch (siena.QueryFilterSearch)4 Many4PM (siena.core.Many4PM)4 SienaFutureContainer (siena.core.async.SienaFutureContainer)4