Search in sources :

Example 16 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _insertSingle.

private <T> void _insertSingle(T obj, final Key parentEntityKey, final Object parentObj, final ClassInfo parentInfo, final Field field) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    Field idField = info.getIdField();
    final Entity entity;
    // first put the entity to have its ID at least!
    if (parentEntityKey == null) {
        entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
        GaeMappingUtils.fillEntity(obj, entity);
        ds.put(entity);
        GaeMappingUtils.setIdFromKey(idField, obj, entity.getKey());
    } else {
        entity = GaeMappingUtils.createEntityInstanceFromParent(idField, info, obj, parentEntityKey, parentInfo, field);
        GaeMappingUtils.fillEntity(obj, entity);
        ds.put(entity);
        GaeMappingUtils.setIdFromKey(idField, obj, entity.getKey());
    }
    if (info.hasAggregatedFields) {
        Map<Key, Map<Field, List<Object>>> keyMap = new HashMap<Key, Map<Field, List<Object>>>();
        Map<Field, List<Object>> objectMap = new HashMap<Field, List<Object>>();
        keyMap.put(entity.getKey(), objectMap);
        _populateAggregateFieldMap(objectMap, info, obj);
        _insertMultipleMapFromParent(keyMap, Arrays.asList(info));
    }
    if (info.hasOwnedFields) {
        List<Object> relObjects = new ArrayList<Object>();
        _populateOwnedList(relObjects, info, obj);
        // uses save because we don't know if the objects where already saved or not
        save(relObjects);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) 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 17 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _updateBuildMaps.

//	private <T> int _updateComplexMultiple(Iterable<T> objs, Key parentKey, ClassInfo parentInfo, Field parentField){
//		HashMap<PersistenceType, List<Entity>> entitiesMap = new HashMap<PersistenceType, List<Entity>>(); 
//		HashMap<PersistenceType, List<Object>> objectsMap = new HashMap<PersistenceType, List<Object>>(); 
//		HashMap<PersistenceType, List<Key>> keysMap = new HashMap<PersistenceType, List<Key>>(); 
//
//		for(Object obj:objs){
//			_updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, null, null, null);
//		}
//		
//		return _updateManageMaps(entitiesMap, objectsMap, keysMap);
//	}
//private void _buildUpdateList(List<Entity> entities2Insert, List<Object> objects2Insert, List<Entity> entities2Update, List<Key> entities2Remove, List<Object> objects2Save, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField){
private static void _updateBuildMaps(HashMap<PersistenceType, List<Entity>> entitiesMap, HashMap<PersistenceType, List<Object>> objectsMap, HashMap<PersistenceType, List<Key>> keysMap, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
    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) {
        if (parentKey == null) {
            entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
        } else {
            entity = GaeMappingUtils.createEntityInstanceFromParent(idField, info, obj, parentKey, parentInfo, parentField);
        }
        GaeMappingUtils.fillEntity(obj, entity);
        List<Entity> entities2Insert = entitiesMap.get(PersistenceType.INSERT);
        if (entities2Insert == null) {
            entities2Insert = new ArrayList<Entity>();
            entitiesMap.put(PersistenceType.INSERT, entities2Insert);
        }
        entities2Insert.add(entity);
        List<Object> objects2Insert = objectsMap.get(PersistenceType.INSERT);
        if (objects2Insert == null) {
            objects2Insert = new ArrayList<Object>();
            objectsMap.put(PersistenceType.INSERT, objects2Insert);
        }
        objects2Insert.add(obj);
    } else {
        if (parentKey == null) {
            entity = GaeMappingUtils.createEntityInstanceForUpdate(info, obj);
        } else {
            entity = GaeMappingUtils.createEntityInstanceForUpdateFromParent(info, obj, parentKey, parentInfo, parentField);
        }
        GaeMappingUtils.fillEntity(obj, entity);
        List<Entity> entities2Update = entitiesMap.get(PersistenceType.UPDATE);
        if (entities2Update == null) {
            entities2Update = new ArrayList<Entity>();
            entitiesMap.put(PersistenceType.UPDATE, entities2Update);
        }
        entities2Update.add(entity);
    }
    for (Field f : info.ownedFields) {
        // doesn't do anything with One<T>
        if (ClassInfo.isOne(f)) {
            // set the owner field in the child object using the content of the one
            One4PM<?> relObj = (One4PM<?>) Util.readField(obj, f);
            Map<FieldMapKeys, Object> m = info.oneFieldMap.get(f);
            if (m != null) {
                Field asField = (Field) m.get(FieldMapKeys.FIELD);
                if (relObj.isModified()) {
                    // unassociates previous object
                    Object prevObj = relObj.getPrev();
                    if (prevObj != null) {
                        Util.setField(prevObj, asField, null);
                    }
                    // resets modified flag
                    relObj.setModified(false);
                    List<Object> objects2Save = objectsMap.get(PersistenceType.SAVE);
                    if (objects2Save == null) {
                        objects2Save = new ArrayList<Object>();
                        objectsMap.put(PersistenceType.SAVE, objects2Save);
                    }
                    objects2Save.add(prevObj);
                    Object oneObj = relObj.get();
                    if (oneObj != null) {
                        Util.setField(oneObj, asField, obj);
                        objects2Save.add(oneObj);
                    }
                }
            }
        } else if (ClassInfo.isMany(f)) {
            Many4PM<?> lq = (Many4PM<?>) Util.readField(obj, f);
            // when you remove an element from a Many<T>, it just removes the link
            if (!lq.asList2Remove().isEmpty()) {
                Field asField = (Field) info.manyFieldMap.get(f).get(FieldMapKeys.FIELD);
                for (Object elt : lq.asList2Remove()) {
                    Util.setField(elt, asField, null);
                    List<Object> objects2Save = objectsMap.get(PersistenceType.SAVE);
                    if (objects2Save == null) {
                        objects2Save = new ArrayList<Object>();
                        objectsMap.put(PersistenceType.SAVE, objects2Save);
                    }
                    objects2Save.add(elt);
                }
                lq.asList2Remove().clear();
            }
            if (!lq.asList2Add().isEmpty()) {
                Field asField = (Field) info.manyFieldMap.get(f).get(FieldMapKeys.FIELD);
                for (Object elt : lq.asList2Add()) {
                    Util.setField(elt, asField, obj);
                    List<Object> objects2Save = objectsMap.get(PersistenceType.SAVE);
                    if (objects2Save == null) {
                        objects2Save = new ArrayList<Object>();
                        objectsMap.put(PersistenceType.SAVE, objects2Save);
                    }
                    objects2Save.add(elt);
                }
                lq.asList2Add().clear();
            }
        }
    }
    for (Field f : info.aggregatedFields) {
        if (ClassInfo.isOne(f)) {
            One4PM<?> one = (One4PM<?>) Util.readField(obj, f);
            if (one.isModified()) {
                // deletes previous object
                Object prevObj = one.getPrev();
                if (prevObj != null) {
                    Class<?> delClazz = prevObj.getClass();
                    ClassInfo delInfo = ClassInfo.getClassInfo(delClazz);
                    Key delKey = GaeMappingUtils.makeKeyFromParent(delInfo, prevObj, entity.getKey(), info, f);
                    List<Key> key2Remove = keysMap.get(PersistenceType.DELETE);
                    if (key2Remove == null) {
                        key2Remove = new ArrayList<Key>();
                        keysMap.put(PersistenceType.DELETE, key2Remove);
                    }
                    key2Remove.add(delKey);
                }
                // resets modified flag
                one.setModified(false);
                Object oneObj = one.get();
                if (oneObj != null) {
                    _updateBuildMaps(entitiesMap, objectsMap, keysMap, oneObj, entity.getKey(), info, f);
                }
            }
        } else if (ClassInfo.isMany(f)) {
            Many4PM<?> lq = (Many4PM<?>) Util.readField(obj, f);
            // add to entities2remove child entities that have been removed
            if (!lq.asList2Remove().isEmpty()) {
                Key delKey;
                for (Object elt : lq.asList2Remove()) {
                    Class<?> delClazz = elt.getClass();
                    ClassInfo delInfo = ClassInfo.getClassInfo(delClazz);
                    delKey = GaeMappingUtils.makeKeyFromParent(delInfo, elt, entity.getKey(), info, f);
                    List<Key> key2Remove = keysMap.get(PersistenceType.DELETE);
                    if (key2Remove == null) {
                        key2Remove = new ArrayList<Key>();
                        keysMap.put(PersistenceType.DELETE, key2Remove);
                    }
                    key2Remove.add(delKey);
                }
                lq.asList2Remove().clear();
            }
            if (!lq.asList2Add().isEmpty()) {
                for (Object elt : lq.asList2Add()) {
                    _updateBuildMaps(entitiesMap, objectsMap, keysMap, elt, entity.getKey(), info, f);
                }
                lq.asList2Add().clear();
            }
        }
    }
}
Also used : One4PM(siena.core.One4PM) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) FieldMapKeys(siena.ClassInfo.FieldMapKeys) Many4PM(siena.core.Many4PM) 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 18 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method mapKeysOnly.

protected <T> List<T> mapKeysOnly(Query<T> query, List<Entity> entities) {
    Class<T> clazz = query.getQueriedClass();
    List<T> results = GaeMappingUtils.mapEntitiesKeysOnly(entities, clazz);
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    // maps model relations to be able to associate children to aggregators
    GaeMappingUtils.mapRelations(query, results, info);
    // DOESN'T MANAGE OWNED/AGGREGATED/JOIN fields
    return results;
}
Also used : ClassInfo(siena.ClassInfo)

Example 19 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _updateMultiple.

private <T> int _updateMultiple(Iterable<T> objects) {
    HashMap<PersistenceType, List<Entity>> entitiesMap = new HashMap<PersistenceType, List<Entity>>();
    HashMap<PersistenceType, List<Object>> objectsMap = new HashMap<PersistenceType, List<Object>>();
    HashMap<PersistenceType, List<Key>> keysMap = new HashMap<PersistenceType, List<Key>>();
    for (Object obj : objects) {
        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);
                _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, null, null, null);
            }
        } else {
            _updateBuildMaps(entitiesMap, objectsMap, keysMap, obj, null, null, null);
        }
    }
    return _updateManageMaps(entitiesMap, objectsMap, keysMap);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Relation(siena.core.Relation) HashMap(java.util.HashMap) 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 20 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method map.

protected <T> T map(Query<T> query, Entity entity) {
    Class<T> clazz = query.getQueriedClass();
    T result = GaeMappingUtils.mapEntity(entity, clazz);
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    // maps model relations to be able to associate children to aggregators
    GaeMappingUtils.mapRelation(query, result, info);
    // related fields (Many<T> management mainly)
    if (!info.ownedFields.isEmpty()) {
        mapOwned(result);
    }
    // aggregated management
    if (!ClassInfo.getClassInfo(clazz).aggregatedFields.isEmpty()) {
        mapAggregated(result);
    }
    // join management
    if (!query.getJoins().isEmpty() || !ClassInfo.getClassInfo(clazz).joinFields.isEmpty())
        mapJoins(query, result);
    return result;
}
Also used : 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