Search in sources :

Example 6 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method prepareKeysOnly.

private <T> PreparedQuery prepareKeysOnly(Query<T> query) {
    Class<?> clazz = query.getQueriedClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    com.google.appengine.api.datastore.Query q;
    // manages aggregation at first
    List<QueryAggregated> aggregs = query.getAggregatees();
    if (aggregs.isEmpty()) {
        q = new com.google.appengine.api.datastore.Query(ClassInfo.getClassInfo(clazz).tableName);
    } else if (aggregs.size() == 1) {
        QueryAggregated aggreg = aggregs.get(0);
        q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(info, ClassInfo.getClassInfo(aggreg.aggregator.getClass()), aggreg.field));
        q.setAncestor(GaeMappingUtils.getKey(aggreg.aggregator));
    } else {
        throw new SienaException("Only one aggregation per query allowed");
    }
    return ds.prepare(GaeQueryUtils.addFiltersOrders(query, q).setKeysOnly());
}
Also used : PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated) ClassInfo(siena.ClassInfo)

Example 7 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaeMappingUtils method fillModelAndKey.

public static void fillModelAndKey(Object obj, Entity entity) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    Field id = info.getIdField();
    Class<?> fieldClass = id.getType();
    Key key = entity.getKey();
    if (key != null) {
        setIdFromKey(id, obj, key);
    }
    for (Field field : info.updateFields) {
        String property = ClassInfo.getColumnNames(field)[0];
        try {
            fieldClass = field.getType();
            if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
                /*if(!ClassInfo.isAggregated(field)){*/
                key = (Key) entity.getProperty(property);
                if (key != null) {
                    Object value = Util.createObjectInstance(fieldClass);
                    id = ClassInfo.getIdField(fieldClass);
                    setIdFromKey(id, value, key);
                    Util.setField(obj, field, value);
                }
            /*}*/
            } else if (ClassInfo.isEmbedded(field) && field.getAnnotation(Embedded.class).mode() == Embedded.Mode.NATIVE) {
                Object value = GaeNativeSerializer.unembed(field.getType(), ClassInfo.getSingleColumnName(field), entity, 0);
                Util.setField(obj, field, value);
            } else /*else if(ClassInfo.isAggregated(field)){
					// does nothing for the time being
				}
				else if (ClassInfo.isOwned(field)){
					// does nothing for the time being
				}*/
            {
                setFromObject(obj, field, entity.getProperty(property));
            }
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException) ClassInfo(siena.ClassInfo)

Example 8 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaeMappingUtils method getKeyFromParent.

public static Key getKeyFromParent(Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    try {
        Field idField = info.getIdField();
        Object value = Util.readField(obj, idField);
        // TODO verify that returning NULL is not a bad thing
        if (value == null)
            return null;
        Class<?> type = idField.getType();
        if (idField.isAnnotationPresent(Id.class)) {
            Id id = idField.getAnnotation(Id.class);
            switch(id.value()) {
                case NONE:
                    // long or string goes toString
                    return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
                case AUTO_INCREMENT:
                    // as a string with auto_increment can't exist, it is not cast into long
                    if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
                        return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), (Long) value);
                    }
                    return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
                case UUID:
                    return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
                default:
                    throw new SienaException("Id Generator " + id.value() + " not supported");
            }
        } else
            throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Id(siena.Id) SienaException(siena.SienaException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException) ClassInfo(siena.ClassInfo)

Example 9 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _insertMultipleMapFromParent.

private int _insertMultipleMapFromParent(final Map<Key, Map<Field, List<Object>>> keyMap, final List<ClassInfo> parentInfos) {
    List<Entity> entities = new ArrayList<Entity>();
    int i = 0;
    for (Key key : keyMap.keySet()) {
        Map<Field, List<Object>> objectMap = keyMap.get(key);
        for (Field field : objectMap.keySet()) {
            for (Object obj : objectMap.get(field)) {
                Class<?> clazz = obj.getClass();
                ClassInfo info = ClassInfo.getClassInfo(clazz);
                Field idField = info.getIdField();
                Entity entity = GaeMappingUtils.createEntityInstanceFromParent(idField, info, obj, key, parentInfos.get(i), field);
                GaeMappingUtils.fillEntity(obj, entity);
                entities.add(entity);
            }
        }
        i++;
    }
    List<Key> generatedKeys = ds.put(entities);
    i = 0;
    Map<Key, Map<Field, List<Object>>> recKeyMap = new HashMap<Key, Map<Field, List<Object>>>();
    List<ClassInfo> recInfos = new ArrayList<ClassInfo>();
    for (Key key : keyMap.keySet()) {
        Map<Field, List<Object>> objectMap = keyMap.get(key);
        for (Field field : objectMap.keySet()) {
            for (Object obj : objectMap.get(field)) {
                Class<?> clazz = obj.getClass();
                ClassInfo info = ClassInfo.getClassInfo(clazz);
                Field idField = info.getIdField();
                GaeMappingUtils.setIdFromKey(idField, obj, generatedKeys.get(i));
                if (info.hasAggregatedFields) {
                    recInfos.add(info);
                    Map<Field, List<Object>> recObjectMap = new HashMap<Field, List<Object>>();
                    recKeyMap.put(generatedKeys.get(i), recObjectMap);
                    _populateAggregateFieldMap(recObjectMap, info, obj);
                }
                i++;
            }
        }
    }
    if (!recKeyMap.isEmpty()) {
        _insertMultipleMapFromParent(recKeyMap, recInfos);
    }
    return generatedKeys.size();
}
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 10 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method _deleteMultiple.

private void _deleteMultiple(Iterable<?> objects, List<Key> keys, final Key parentKey, final ClassInfo parentInfo, final Field parentField) {
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Key key;
        if (parentKey == null) {
            key = GaeMappingUtils.getKey(obj);
        } else {
            key = GaeMappingUtils.getKeyFromParent(obj, parentKey, parentInfo, parentField);
        }
        // cascading on aggregated fields
        if (!info.aggregatedFields.isEmpty()) {
            for (Field f : info.aggregatedFields) {
                if (ClassInfo.isModel(f.getType())) {
                    Object aggObj = Util.readField(obj, f);
                    _deleteSingle(aggObj, keys, key, info, f);
                } else if (ClassInfo.isOne(f)) {
                    One<?> one = (One<?>) Util.readField(obj, f);
                    Object target = one.get();
                    if (target != null) {
                        _deleteSingle(target, keys, key, info, f);
                    }
                } else if (ClassInfo.isMany(f)) {
                    Many<?> lq = (Many<?>) Util.readField(obj, f);
                    if (!lq.asList().isEmpty()) {
                        _deleteMultiple(lq.asQuery().fetchKeys(), keys, key, info, f);
                    }
                }
            }
        }
        keys.add(key);
    }
}
Also used : Field(java.lang.reflect.Field) One(siena.core.One) Many(siena.core.Many) 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