Search in sources :

Example 1 with Id

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

use of siena.Id in project siena by mandubian.

the class GaeMappingUtils method createEntityInstanceFromParent.

public static Entity createEntityInstanceFromParent(Field idField, ClassInfo info, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
    Entity entity = null;
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();
    if (id != null) {
        switch(id.value()) {
            case NONE:
                Object idVal = null;
                idVal = Util.readField(obj, idField);
                if (idVal == null)
                    throw new SienaException("Id Field " + idField.getName() + " value null");
                String keyVal = Util.toString(idField, idVal);
                entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyVal, parentKey);
                break;
            case AUTO_INCREMENT:
                // manages String ID as not long!!!
                if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
                    entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), parentKey);
                } else {
                    Object idStringVal = null;
                    idStringVal = Util.readField(obj, idField);
                    if (idStringVal == null)
                        throw new SienaException("Id Field " + idField.getName() + " value null");
                    String keyStringVal = Util.toString(idField, idStringVal);
                    entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyStringVal, parentKey);
                }
                break;
            case UUID:
                entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), UUID.randomUUID().toString(), parentKey);
                break;
            default:
                throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator " + id.value() + " not supported");
        }
    } else
        throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    return entity;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) SienaRestrictedApiException(siena.SienaRestrictedApiException) Id(siena.Id) SienaException(siena.SienaException)

Example 3 with Id

use of siena.Id 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 4 with Id

use of siena.Id 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 5 with Id

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

Id (siena.Id)17 Field (java.lang.reflect.Field)14 SienaException (siena.SienaException)14 SienaRestrictedApiException (siena.SienaRestrictedApiException)13 IOException (java.io.IOException)7 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ClassInfo (siena.ClassInfo)3 Entity (com.google.appengine.api.datastore.Entity)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Key (com.google.appengine.api.datastore.Key)1 FilterOperator (com.google.appengine.api.datastore.Query.FilterOperator)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1