Search in sources :

Example 46 with Key

use of com.google.appengine.api.datastore.Key in project siena by mandubian.

the class GaePersistenceManagerAsync method save.

public SienaFuture<Integer> save(final Object... 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 47 with Key

use of com.google.appengine.api.datastore.Key in project siena by mandubian.

the class GaePersistenceManagerAsync method insert.

public SienaFuture<Integer> insert(final Object... objects) {
    List<Entity> entities = new ArrayList<Entity>(objects.length);
    for (int i = 0; i < objects.length; i++) {
        Class<?> clazz = objects[i].getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Field idField = info.getIdField();
        Entity entity = GaeMappingUtils.createEntityInstance(idField, info, objects[i]);
        GaeMappingUtils.fillEntity(objects[i], 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)

Aggregations

Key (com.google.appengine.api.datastore.Key)47 Entity (com.google.appengine.api.datastore.Entity)34 ArrayList (java.util.ArrayList)24 ClassInfo (siena.ClassInfo)23 Field (java.lang.reflect.Field)22 SienaException (siena.SienaException)14 HashMap (java.util.HashMap)11 List (java.util.List)11 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)9 Map (java.util.Map)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 DatastoreService (com.google.appengine.api.datastore.DatastoreService)6 Relation (siena.core.Relation)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)4 IOException (java.io.IOException)3 NotImplementedException (org.apache.commons.lang.NotImplementedException)3 Text (com.google.appengine.api.datastore.Text)2 AppRole (samples.gae.security.AppRole)2