Search in sources :

Example 66 with Key

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

the class GaePersistenceManagerAsync method delete.

public <T> SienaFuture<Integer> delete(QueryAsync<T> query) {
    final ArrayList<Key> keys = new ArrayList<Key>();
    for (final Entity entity : prepareKeysOnly(query).asIterable(FetchOptions.Builder.withDefaults())) {
        keys.add(entity.getKey());
    }
    Future<Void> future = ds.delete(keys);
    Future<Integer> wrapped = new SienaFutureWrapper<Void, Integer>(future) {

        @Override
        protected Integer wrap(Void v) 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) Key(com.google.appengine.api.datastore.Key)

Example 67 with Key

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

the class GaePersistenceManagerAsync method mapJoins.

protected <T> T mapJoins(QueryAsync<T> query, T model) {
    try {
        // join queries
        Map<Field, ArrayList<Key>> fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(query);
        // creates the list of joined entity keys to extract
        for (Field field : fieldMap.keySet()) {
            Key key = GaeMappingUtils.getKey(field.get(model));
            List<Key> keys = fieldMap.get(field);
            if (!keys.contains(key))
                keys.add(key);
        }
        Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
        try {
            // retrieves all joined entities per field
            for (Field field : fieldMap.keySet()) {
                Future<Map<Key, Entity>> entities = ds.get(fieldMap.get(field));
                // gets the future here because we need it!
                entityMap.put(field, entities.get());
            }
        } catch (Exception ex) {
            throw new SienaException(ex);
        }
        // associates linked models to their models
        // linkedModels is just a map to contain entities already mapped
        Map<Key, Object> linkedModels = new HashMap<Key, Object>();
        Object linkedObj;
        Entity entity;
        for (Field field : fieldMap.keySet()) {
            Object objVal = field.get(model);
            Key key = GaeMappingUtils.getKey(objVal);
            linkedObj = linkedModels.get(key);
            if (linkedObj == null) {
                entity = entityMap.get(field).get(key);
                linkedObj = objVal;
                GaeMappingUtils.fillModel(linkedObj, entity);
                linkedModels.put(key, linkedObj);
            }
            field.set(model, linkedObj);
        }
        return model;
    } catch (IllegalAccessException ex) {
        throw new SienaException(ex);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) NotImplementedException(org.apache.commons.lang.NotImplementedException) Field(java.lang.reflect.Field) SienaException(siena.SienaException) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.google.appengine.api.datastore.Key)

Example 68 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 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 69 with Key

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

the class GaePersistenceManager method _insertSingle.

private <T> void _insertSingle(T obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    if (info.hasAggregator) {
        Relation rel = (Relation) Util.readField(obj, info.aggregator);
        if (rel != null && rel.mode == RelationMode.AGGREGATION) {
            ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
            Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
            _insertSingle(obj, parentKey, rel.target, parentInfo, (Field) rel.discriminator);
        } else {
            _insertSingle(obj, null, null, null, null);
        }
    } else {
        _insertSingle(obj, null, null, null, null);
    }
}
Also used : Relation(siena.core.Relation) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 70 with Key

use of com.google.appengine.api.datastore.Key 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)

Aggregations

Key (com.google.appengine.api.datastore.Key)121 Entity (com.google.appengine.api.datastore.Entity)83 ArrayList (java.util.ArrayList)39 DatastoreService (com.google.appengine.api.datastore.DatastoreService)26 Query (com.google.appengine.api.datastore.Query)23 Test (org.junit.Test)23 ClassInfo (siena.ClassInfo)23 Field (java.lang.reflect.Field)22 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)21 HashMap (java.util.HashMap)14 SienaException (siena.SienaException)14 List (java.util.List)13 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)12 QueryResultList (com.google.appengine.api.datastore.QueryResultList)11 Map (java.util.Map)11 Transaction (com.google.appengine.api.datastore.Transaction)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)7 IOException (java.io.IOException)7