Search in sources :

Example 66 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _updateSimpleMultiple.

private <T> void _updateSimpleMultiple(Iterable<T> objs, Key parentKey, ClassInfo parentInfo, Field parentField) {
    List<Entity> entities = new ArrayList<Entity>();
    ClassInfo info = null;
    Field idField = null;
    for (T obj : objs) {
        if (info == null) {
            info = ClassInfo.getClassInfo(obj.getClass());
            idField = info.getIdField();
        }
        Entity entity;
        Object idVal = Util.readField(obj, idField);
        // id with null value means insert
        if (idVal == null) {
            if (parentKey == null) {
                entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
            } else {
                entity = GaeMappingUtils.createEntityInstanceFromParent(idField, info, obj, parentKey, parentInfo, parentField);
            }
            GaeMappingUtils.fillEntity(obj, entity);
        } else {
            if (parentKey == null) {
                entity = GaeMappingUtils.createEntityInstanceForUpdate(info, obj);
            } else {
                entity = GaeMappingUtils.createEntityInstanceForUpdateFromParent(info, obj, parentKey, parentInfo, parentField);
            }
            GaeMappingUtils.fillEntity(obj, entity);
        }
        entities.add(entity);
    }
    if (!entities.isEmpty()) {
        ds.put(entities);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ClassInfo(siena.ClassInfo)

Example 67 with ClassInfo

use of siena.ClassInfo 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 68 with ClassInfo

use of siena.ClassInfo 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

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