Search in sources :

Example 1 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaeMappingUtils method getKey.

public static Key getKey(Object obj) {
    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(ClassInfo.getClassInfo(clazz).tableName, 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(ClassInfo.getClassInfo(clazz).tableName, (Long) value);
                    }
                    return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, value.toString());
                case UUID:
                    return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, 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 2 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class PersistenceManagerLifeCycleWrapper method get.

@Override
public void get(Object obj) {
    ClassInfo ci = ClassInfo.getClassInfo(obj.getClass());
    LifeCycleUtils.executeMethods(LifeCyclePhase.PRE_FETCH, ci, obj);
    pm.get(obj);
    LifeCycleUtils.executeMethods(LifeCyclePhase.POST_FETCH, ci, obj);
}
Also used : ClassInfo(siena.ClassInfo)

Example 3 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class PersistenceManagerLifeCycleWrapper method update.

@Override
public void update(Object obj) {
    ClassInfo ci = ClassInfo.getClassInfo(obj.getClass());
    LifeCycleUtils.executeMethods(LifeCyclePhase.PRE_UPDATE, ci, obj);
    pm.update(obj);
    LifeCycleUtils.executeMethods(LifeCyclePhase.POST_UPDATE, ci, obj);
}
Also used : ClassInfo(siena.ClassInfo)

Example 4 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class PersistenceManagerLifeCycleWrapper method save.

@Override
public void save(Object obj) {
    ClassInfo ci = ClassInfo.getClassInfo(obj.getClass());
    LifeCycleUtils.executeMethods(LifeCyclePhase.PRE_SAVE, ci, obj);
    pm.save(obj);
    LifeCycleUtils.executeMethods(LifeCyclePhase.POST_SAVE, ci, obj);
}
Also used : ClassInfo(siena.ClassInfo)

Example 5 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManager method update.

public void update(Object 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);
            if (!info.hasAggregatedFields && !info.hasOwnedFields) {
                _updateSimple(obj, info, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _updateComplex(obj, parentKey, parentInfo, (Field) rel.discriminator);
            }
        } else {
            if (!info.hasAggregatedFields && !info.hasOwnedFields) {
                _updateSimple(obj, info, null, null, null);
            } else {
                _updateComplex(obj, null, null, null);
            }
        }
    } else {
        if (!info.hasAggregatedFields && !info.hasOwnedFields) {
            _updateSimple(obj, info, null, null, null);
        } else {
            _updateComplex(obj, null, null, null);
        }
    }
}
Also used : Relation(siena.core.Relation) 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