Search in sources :

Example 11 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method delete.

public <T> int delete(Query<T> query) {
    final ArrayList<Key> keys = new ArrayList<Key>();
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    if (!info.hasAggregatedFields) {
        for (final Entity entity : prepareKeysOnly(query).asIterable(FetchOptions.Builder.withDefaults())) {
            keys.add(entity.getKey());
        }
    } else {
        // if aggregated fields, needs to retrieve all objects to scan & delete aggregated children
        List<T> models = query.fetch();
        _deleteMultiple(models, keys, null, null, null);
    }
    ds.delete(keys);
    return keys.size();
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 12 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method mapOwned.

protected <T> T mapOwned(T model) {
    Class<?> clazz = model.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    for (Field f : info.ownedFields) {
        if (ClassInfo.isOne(f)) {
            One4PM<?> lq = (One4PM<?>) Util.readField(model, f);
            // sets the sync flag to false to tell that it should be fetched when the listquery is accessed!
            lq.setSync(false);
        } 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 listquery is accessed!
            lq.setSync(false);
        }
    }
    return model;
}
Also used : One4PM(siena.core.One4PM) Field(java.lang.reflect.Field) Many4PM(siena.core.Many4PM) ClassInfo(siena.ClassInfo)

Example 13 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method save.

public int save(Iterable<?> objects) {
    List<Object> entities2Insert = new ArrayList<Object>();
    List<Object> entities2Update = new ArrayList<Object>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Field idField = info.getIdField();
        Object idVal = Util.readField(obj, idField);
        // id with null value means insert
        if (idVal == null) {
            entities2Insert.add(obj);
        } else // id with not null value means update
        {
            entities2Update.add(obj);
        }
    }
    return insert(entities2Insert) + update(entities2Update);
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ClassInfo(siena.ClassInfo)

Example 14 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _insertPutEntities.

private <T> int _insertPutEntities(Iterable<Entity> entities, Iterable<T> objects) {
    List<Key> generatedKeys = ds.put(entities);
    int i = 0;
    Map<Key, Map<Field, List<Object>>> keyMap = new HashMap<Key, Map<Field, List<Object>>>();
    List<ClassInfo> infos = new ArrayList<ClassInfo>();
    List<Object> relObjects = new ArrayList<Object>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Field idField = info.getIdField();
        GaeMappingUtils.setIdFromKey(idField, obj, generatedKeys.get(i));
        if (info.hasAggregatedFields) {
            infos.add(info);
            Map<Field, List<Object>> objectMap = new HashMap<Field, List<Object>>();
            keyMap.put(generatedKeys.get(i), objectMap);
            _populateAggregateFieldMap(objectMap, info, obj);
        }
        if (info.hasOwnedFields) {
            _populateOwnedList(relObjects, info, obj);
        }
        i++;
    }
    if (!keyMap.isEmpty()) {
        _insertMultipleMapFromParent(keyMap, infos);
    }
    if (!relObjects.isEmpty()) {
        // uses save because we don't know if the objects where already saved or not
        save(relObjects);
    }
    return generatedKeys.size();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 15 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method delete.

public void delete(Object obj) {
    List<Key> keys = new ArrayList<Key>();
    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);
            _deleteSingle(obj, keys, parentKey, parentInfo, (Field) rel.discriminator);
        } else {
            _deleteSingle(obj, keys, null, null, null);
        }
    } else {
        _deleteSingle(obj, keys, null, null, null);
    }
    ds.delete(keys);
}
Also used : Relation(siena.core.Relation) 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