Search in sources :

Example 36 with Key

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

the class GaePersistenceManager method _deleteSingle.

private void _deleteSingle(Object obj, List<Key> keys, final Key parentKey, final ClassInfo parentInfo, final Field parentField) {
    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.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) Many(siena.core.Many) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 37 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 38 with Key

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

the class GaePersistenceManagerAsync method getByKey.

public <T> SienaFuture<T> getByKey(final Class<T> clazz, final Object key) {
    Key gkey = GaeMappingUtils.makeKeyFromId(clazz, key);
    try {
        Future<Entity> future = ds.get(gkey);
        Future<T> wrapped = new SienaFutureWrapper<Entity, T>(future) {

            @Override
            protected T wrap(Entity entity) throws Exception {
                T obj = Util.createObjectInstance(clazz);
                GaeMappingUtils.fillModelAndKey(obj, entity);
                return obj;
            }
        };
        return new SienaFutureContainer<T>(wrapped);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) NotImplementedException(org.apache.commons.lang.NotImplementedException)

Example 39 with Key

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

the class GaeMappingUtils method createEntityInstanceForUpdateFromParent.

public static Entity createEntityInstanceForUpdateFromParent(ClassInfo info, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
    Key key = makeKeyFromParent(info, obj, parentKey, parentInfo, parentField);
    Entity entity = new Entity(key);
    return entity;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Key(com.google.appengine.api.datastore.Key)

Example 40 with Key

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

the class GaeMappingUtils method fillModel.

public static void fillModel(Object obj, Entity entity) {
    Class<?> clazz = obj.getClass();
    for (Field field : ClassInfo.getClassInfo(clazz).updateFields) {
        String property = ClassInfo.getColumnNames(field)[0];
        try {
            Class<?> fieldClass = field.getType();
            if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
                /*if(!ClassInfo.isAggregated(field)){*/
                Key key = (Key) entity.getProperty(property);
                if (key != null) {
                    Object value = Util.createObjectInstance(fieldClass);
                    Field id = ClassInfo.getIdField(fieldClass);
                    setIdFromKey(id, value, key);
                    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
				}*/
            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 {
                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)

Aggregations

Key (com.google.appengine.api.datastore.Key)47 Entity (com.google.appengine.api.datastore.Entity)34 ArrayList (java.util.ArrayList)24 ClassInfo (siena.ClassInfo)23 Field (java.lang.reflect.Field)22 SienaException (siena.SienaException)14 HashMap (java.util.HashMap)11 List (java.util.List)11 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)9 Map (java.util.Map)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 DatastoreService (com.google.appengine.api.datastore.DatastoreService)6 Relation (siena.core.Relation)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)4 IOException (java.io.IOException)3 NotImplementedException (org.apache.commons.lang.NotImplementedException)3 Text (com.google.appengine.api.datastore.Text)2 AppRole (samples.gae.security.AppRole)2