Search in sources :

Example 81 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method mapEntitiesKeysOnly.

public static <T> List<T> mapEntitiesKeysOnly(Iterable<Entity> entities, Class<T> clazz) {
    Field id = ClassInfo.getIdField(clazz);
    List<T> list = new ArrayList<T>();
    for (Entity entity : entities) {
        T obj;
        try {
            obj = Util.createObjectInstance(clazz);
            list.add(obj);
            setIdFromKey(id, obj, entity.getKey());
        } catch (SienaException e) {
            throw e;
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
    return list;
}
Also used : Field(java.lang.reflect.Field) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 82 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method setIdFromKey.

public static void setIdFromKey(Field idField, Object obj, Key key) {
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();
    if (id != null) {
        switch(id.value()) {
            case NONE:
                // idField.setAccessible(true);
                Object val = null;
                if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
                    val = Long.parseLong((String) key.getName());
                } else if (String.class.isAssignableFrom(type)) {
                    val = key.getName();
                } else {
                    throw new SienaRestrictedApiException("DB", "setKey", "Id Type " + idField.getType() + " not supported");
                }
                Util.setField(obj, idField, val);
                break;
            case AUTO_INCREMENT:
                // Long value means key.getId()
                if (Long.TYPE == type || Long.class.isAssignableFrom(idField.getType())) {
                    Util.setField(obj, idField, key.getId());
                } else {
                    idField.setAccessible(true);
                    Object val2 = null;
                    if (Long.TYPE == type || Long.class.isAssignableFrom(idField.getType())) {
                        val = Long.parseLong((String) key.getName());
                    } else if (String.class.isAssignableFrom(idField.getType())) {
                        val = key.getName();
                    } else {
                        throw new SienaRestrictedApiException("DB", "setKey", "Id Type " + idField.getType() + " not supported");
                    }
                    Util.setField(obj, idField, val2);
                }
                break;
            case UUID:
                Util.setField(obj, idField, key.getName());
                break;
            default:
                throw new SienaException("Id Generator " + id.value() + " not supported");
        }
    } else
        throw new SienaException("Field " + idField.getName() + " is not an @Id field");
}
Also used : SienaRestrictedApiException(siena.SienaRestrictedApiException) Id(siena.Id) SienaException(siena.SienaException)

Example 83 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method makeKeyFromParent.

public static Key makeKeyFromParent(ClassInfo info, Object object, Key parentKey, ClassInfo parentInfo, Field parentField) {
    try {
        Field idField = info.getIdField();
        Object idVal = Util.readField(object, idField);
        if (idVal == null)
            throw new SienaException("Id Field " + idField.getName() + " value null");
        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), idVal.toString());
                case AUTO_INCREMENT:
                    Class<?> type = idField.getType();
                    // 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) idVal);
                    }
                    return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), idVal.toString());
                case UUID:
                    return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), idVal.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) SienaException(siena.SienaException) Id(siena.Id) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 84 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method mapEntities.

public static <T> List<T> mapEntities(List<Entity> entities, Class<T> clazz) {
    Field id = ClassInfo.getIdField(clazz);
    List<T> list = new ArrayList<T>(entities.size());
    for (Entity entity : entities) {
        T obj;
        try {
            obj = Util.createObjectInstance(clazz);
            fillModel(obj, entity);
            list.add(obj);
            setIdFromKey(id, obj, entity.getKey());
        } catch (SienaException e) {
            throw e;
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
    return list;
}
Also used : Field(java.lang.reflect.Field) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 85 with SienaException

use of siena.SienaException 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)

Aggregations

SienaException (siena.SienaException)214 Field (java.lang.reflect.Field)65 TransactionAccountFrom (siena.base.test.model.TransactionAccountFrom)52 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)40 SQLException (java.sql.SQLException)38 TransactionAccountTo (siena.base.test.model.TransactionAccountTo)34 SienaRestrictedApiException (siena.SienaRestrictedApiException)33 PreparedStatement (java.sql.PreparedStatement)25 ClassInfo (siena.ClassInfo)21 HashMap (java.util.HashMap)19 List (java.util.List)18 Discovery (siena.base.test.model.Discovery)18 Entity (com.google.appengine.api.datastore.Entity)15 Key (com.google.appengine.api.datastore.Key)14 ResultSet (java.sql.ResultSet)14 Id (siena.Id)14 AmazonClientException (com.amazonaws.AmazonClientException)12 Collection (java.util.Collection)10 DiscoveryStringId (siena.base.test.model.DiscoveryStringId)9