Search in sources :

Example 76 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeQueryUtils method addSearchFilterIn.

public static void addSearchFilterIn(com.google.appengine.api.datastore.Query q, Field field, List<String> matches) {
    String[] columns = ClassInfo.getColumnNames(field);
    if (columns.length > 1)
        throw new SienaException("Search not possible for multi-column fields in GAE: only one field with one column");
    q.addFilter(columns[0], FilterOperator.IN, matches);
}
Also used : SienaException(siena.SienaException)

Example 77 with SienaException

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

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method fillEntity.

public static void fillEntity(Object obj, Entity entity) {
    Class<?> clazz = obj.getClass();
    for (Field field : ClassInfo.getClassInfo(clazz).updateFields) {
        String property = ClassInfo.getColumnNames(field)[0];
        Object value = Util.readField(obj, field);
        Class<?> fieldClass = field.getType();
        if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) /*&& !ClassInfo.isAggregated(field)
					&& !ClassInfo.isOwned(field)*/
        {
            if (value == null) {
                entity.setProperty(property, null);
            } else {
                Key key = getKey(value);
                entity.setProperty(property, key);
            }
        } else {
            if (value != null) {
                if (fieldClass == Json.class) {
                    value = value.toString();
                } else if (value instanceof String) {
                    String s = (String) value;
                    if (s.length() > 500)
                        value = new Text(s);
                } else if (value instanceof byte[]) {
                    byte[] arr = (byte[]) value;
                    // GAE Blob doesn't accept more than 1MB
                    if (arr.length < 1000000)
                        value = new Blob(arr);
                    else
                        value = new Blob(Arrays.copyOf(arr, 1000000));
                } else if (ClassInfo.isEmbedded(field)) {
                    Embedded embed = field.getAnnotation(Embedded.class);
                    switch(embed.mode()) {
                        case SERIALIZE_JSON:
                            value = JsonSerializer.serialize(value).toString();
                            String s = (String) value;
                            if (s.length() > 500)
                                value = new Text(s);
                            break;
                        case SERIALIZE_JAVA:
                            // this embedding mode doesn't manage @EmbedIgnores
                            try {
                                byte[] b = JavaSerializer.serialize(value);
                                // if length is less than 1Mb, can store in a blob else???
                                if (b.length <= 1000000) {
                                    value = new Blob(b);
                                } else {
                                    throw new SienaException("object can be java serialized because it's too large >1mb");
                                }
                            } catch (IOException ex) {
                                throw new SienaException(ex);
                            }
                            break;
                        case NATIVE:
                            GaeNativeSerializer.embed(entity, ClassInfo.getSingleColumnName(field), value, 0);
                            // has set several new properties in entity so go to next field
                            continue;
                    }
                } else /*else if (ClassInfo.isAggregated(field)){
						// can't save it now as it requires its parent key to be mapped
						// so don't do anything for the time being
						continue;
					}
					else if (ClassInfo.isOwned(field)){
						// can't save it now as it requires its parent key to be mapped
						// so don't do anything for the time being
						continue;
					}*/
                if (fieldClass == BigDecimal.class) {
                    DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
                    if (ann == null) {
                        value = ((BigDecimal) value).toPlainString();
                    } else {
                        switch(ann.storageType()) {
                            case DOUBLE:
                                value = ((BigDecimal) value).doubleValue();
                                break;
                            case STRING:
                            case NATIVE:
                                value = ((BigDecimal) value).toPlainString();
                                break;
                        }
                    }
                } else // don't know if anyone will use it but it will work :)
                if (Enum.class.isAssignableFrom(field.getType())) {
                    value = value.toString();
                }
            }
            Unindexed ui = field.getAnnotation(Unindexed.class);
            if (ui == null) {
                entity.setProperty(property, value);
            } else {
                entity.setUnindexedProperty(property, value);
            }
        }
    }
}
Also used : Blob(com.google.appengine.api.datastore.Blob) DecimalPrecision(siena.core.DecimalPrecision) Text(com.google.appengine.api.datastore.Text) IOException(java.io.IOException) Field(java.lang.reflect.Field) Embedded(siena.embed.Embedded) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key)

Example 79 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method makeKey.

public static Key makeKey(ClassInfo info, Object object) {
    Field idField = info.getIdField();
    Object idVal = Util.readField(object, idField);
    if (idVal == null)
        throw new SienaException("Id Field " + idField.getName() + " value null");
    return makeKeyFromId(object.getClass(), idVal);
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException)

Example 80 with SienaException

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

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