Search in sources :

Example 1 with SienaFutureContainer

use of siena.core.async.SienaFutureContainer in project siena by mandubian.

the class GaePersistenceManagerAsync method insert.

public SienaFuture<Integer> insert(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 = GaeMappingUtils.createEntityInstance(idField, 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> generatedKeys) throws Exception {
            int i = 0;
            for (Object obj : objects) {
                Class<?> clazz = obj.getClass();
                ClassInfo info = ClassInfo.getClassInfo(clazz);
                Field idField = info.getIdField();
                GaeMappingUtils.setIdFromKey(idField, obj, generatedKeys.get(i++));
            }
            return generatedKeys.size();
        }
    };
    return new SienaFutureContainer<Integer>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 2 with SienaFutureContainer

use of siena.core.async.SienaFutureContainer in project siena by mandubian.

the class GaePersistenceManagerAsync method get.

public SienaFuture<Void> get(final Object obj) {
    final Key key = GaeMappingUtils.getKey(obj);
    Future<Entity> future = ds.get(key);
    Future<Void> wrapped = new SienaFutureWrapper<Entity, Void>(future) {

        @Override
        protected Void wrap(Entity entity) throws Exception {
            GaeMappingUtils.fillModel(obj, entity);
            return null;
        }
    };
    return new SienaFutureContainer<Void>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) Key(com.google.appengine.api.datastore.Key)

Example 3 with SienaFutureContainer

use of siena.core.async.SienaFutureContainer in project siena by mandubian.

the class GaePersistenceManagerAsync method update.

public SienaFuture<Void> update(Object obj) {
    Entity entity = new Entity(GaeMappingUtils.getKey(obj));
    GaeMappingUtils.fillEntity(obj, entity);
    Future<Key> future = ds.put(entity);
    Future<Void> wrapped = new SienaFutureWrapper<Key, Void>(future) {

        @Override
        protected Void wrap(Key key) throws Exception {
            return null;
        }
    };
    return new SienaFutureContainer<Void>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) Key(com.google.appengine.api.datastore.Key)

Example 4 with SienaFutureContainer

use of siena.core.async.SienaFutureContainer 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);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 5 with SienaFutureContainer

use of siena.core.async.SienaFutureContainer in project siena by mandubian.

the class GaePersistenceManagerAsync method update.

@Override
public <T> SienaFuture<Integer> update(Iterable<T> objects) {
    //throw new NotImplementedException("update not implemented for GAE yet");
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Entity 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 {
            return keys.size();
        }
    };
    return new SienaFutureContainer<Integer>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) ArrayList(java.util.ArrayList) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Aggregations

Entity (com.google.appengine.api.datastore.Entity)9 Key (com.google.appengine.api.datastore.Key)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 ArrayList (java.util.ArrayList)6 QueryResultList (com.google.appengine.api.datastore.QueryResultList)5 List (java.util.List)5 ClassInfo (siena.ClassInfo)5 Field (java.lang.reflect.Field)4 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 SienaException (siena.SienaException)1