Search in sources :

Example 1 with Embedded

use of siena.embed.Embedded 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 2 with Embedded

use of siena.embed.Embedded in project siena by mandubian.

the class Util method fromObject.

public static Object fromObject(Field field, Object value) {
    Class<?> type = field.getType();
    if (value == null) {
        if (type.isPrimitive()) {
            if (byte.class == type)
                return (byte) 0;
            else if (Character.TYPE == type)
                return (char) 0;
            else if (Short.TYPE == type)
                return (short) 0;
            else if (Integer.TYPE == type)
                return (int) 0;
            else if (Long.TYPE == type)
                return (long) 0;
            else if (Float.TYPE == type)
                return (float) 0;
            else if (Double.TYPE == type)
                return (double) 0;
            else if (Boolean.TYPE == type)
                return false;
        }
        return null;
    }
    if (Number.class.isAssignableFrom(value.getClass())) {
        Number number = (Number) value;
        if (byte.class == type || Byte.class == type)
            return number.byteValue();
        else if (Short.TYPE == type || Short.class == type)
            return number.shortValue();
        else if (Integer.TYPE == type || Integer.class == type)
            return number.intValue();
        else if (Long.TYPE == type || Long.class == type)
            return number.longValue();
        else if (Float.TYPE == type || Float.class == type)
            return number.floatValue();
        else if (Double.TYPE == type || Double.class == type)
            return number.doubleValue();
        else if (Boolean.TYPE == type || Boolean.class == type)
            return number.byteValue() != 0;
        else if (BigDecimal.class == type)
            return (BigDecimal) value;
    }
    if (String.class.isAssignableFrom(value.getClass())) {
        if (Json.class.isAssignableFrom(type)) {
            return Json.loads((String) value);
        }
        if (UUID.class == type) {
            return UUID.fromString((String) value);
        }
    }
    Embedded embed = field.getAnnotation(Embedded.class);
    if (embed != null) {
        switch(embed.mode()) {
            case SERIALIZE_JSON:
                if (String.class.isAssignableFrom(value.getClass())) {
                    Json data = Json.loads((String) value);
                    return JsonSerializer.deserialize(field, data);
                }
                break;
            case SERIALIZE_JAVA:
                try {
                    return JavaSerializer.deserialize((byte[]) value);
                } catch (IOException e) {
                    throw new SienaException(e);
                } catch (ClassNotFoundException e) {
                    throw new SienaException(e);
                }
            case NATIVE:
                break;
        }
    }
    if (String.class.isAssignableFrom(value.getClass()) && type.isEnum()) {
        return Enum.valueOf((Class<Enum>) type, (String) value);
    }
    if (String.class.isAssignableFrom(value.getClass()) && type != String.class) {
        return fromString(field.getType(), (String) value, true);
    }
    return value;
}
Also used : IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Embedded(siena.embed.Embedded)

Example 3 with Embedded

use of siena.embed.Embedded in project siena by mandubian.

the class JdbcDBUtils method setObject.

public static void setObject(PreparedStatement ps, int index, Object value, Field field, String DB) throws SQLException {
    if (value == null) {
        ps.setNull(index, JdbcDBUtils.toSqlType(value, field, DB));
        return;
    }
    Class<?> type = field.getType();
    if (type == Byte.class || type == Byte.TYPE)
        ps.setByte(index, (Byte) value);
    else if (type == Short.class || type == Short.TYPE)
        ps.setShort(index, (Short) value);
    else if (type == Integer.class || type == Integer.TYPE)
        ps.setInt(index, (Integer) value);
    else if (type == Long.class || type == Long.TYPE)
        ps.setLong(index, (Long) value);
    else if (type == Float.class || type == Float.TYPE)
        ps.setFloat(index, (Float) value);
    else if (type == Double.class || type == Double.TYPE)
        ps.setDouble(index, (Double) value);
    else if (type == String.class) {
        ps.setString(index, (String) value);
    } else if (type == Boolean.class || type == Boolean.TYPE)
        ps.setBoolean(index, (Boolean) value);
    else if (type == Date.class) {
        if (field.getAnnotation(DateTime.class) != null) {
            java.sql.Timestamp ts = new java.sql.Timestamp(((Date) value).getTime());
            ps.setTimestamp(index, ts);
        } else if (field.getAnnotation(Time.class) != null) {
            java.sql.Time ts = new java.sql.Time(((Date) value).getTime());
            ps.setTime(index, ts);
        } else if (field.getAnnotation(SimpleDate.class) != null) {
            java.sql.Date d = new java.sql.Date(((Date) value).getTime());
            ps.setDate(index, d);
        } else {
            java.sql.Timestamp ts = new java.sql.Timestamp(((Date) value).getTime());
            ps.setTimestamp(index, ts);
        }
    } else if (type == Json.class) {
        ps.setString(index, (String) value);
    } else if (type == byte[].class) {
        ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
        ps.setBlob(index, bis);
    } else if (Enum.class.isAssignableFrom(type)) {
        ps.setString(index, (String) value);
    } else if (type == BigDecimal.class) {
        DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
        if (an == null) {
            ps.setObject(index, value);
        } else {
            if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
                ps.setBigDecimal(index, (BigDecimal) value);
            } else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
                ps.setString(index, ((BigDecimal) value).toPlainString());
            } else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
                ps.setDouble(index, ((BigDecimal) value).doubleValue());
            } else {
                ps.setBigDecimal(index, (BigDecimal) value);
            }
        }
    } else {
        Embedded embedded = field.getAnnotation(Embedded.class);
        if (embedded != null) {
            if ("h2".equals(DB)) {
                StringReader reader = new StringReader((String) value);
                ps.setClob(index, reader);
            } else {
                ps.setString(index, (String) value);
            }
        } else if (field.isAnnotationPresent(Polymorphic.class)) {
            ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
            ps.setBlob(index, bis);
        } else {
            throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + type.getName() + "." + field.getName());
        }
    }
}
Also used : SienaRestrictedApiException(siena.SienaRestrictedApiException) DecimalPrecision(siena.core.DecimalPrecision) Time(siena.Time) DateTime(siena.DateTime) Json(siena.Json) DateTime(siena.DateTime) Date(java.util.Date) SimpleDate(siena.SimpleDate) BigDecimal(java.math.BigDecimal) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDate(siena.SimpleDate) StringReader(java.io.StringReader) Embedded(siena.embed.Embedded) Polymorphic(siena.core.Polymorphic)

Example 4 with Embedded

use of siena.embed.Embedded in project siena by mandubian.

the class DdlGenerator method createColumn.

private Column createColumn(Class<?> clazz, Field field, String col) {
    Class<?> type = field.getType();
    Column column = new Column();
    column.setName(col);
    int columnType;
    if (type == Byte.class || type == Byte.TYPE)
        columnType = Types.TINYINT;
    else if (type == Short.class || type == Short.TYPE)
        columnType = Types.SMALLINT;
    else if (type == Integer.class || type == Integer.TYPE)
        columnType = Types.INTEGER;
    else if (type == Long.class || type == Long.TYPE)
        columnType = Types.BIGINT;
    else if (// TODO verify
    type == Float.class || type == Float.TYPE)
        // TODO verify
        columnType = Types.FLOAT;
    else if (// TODO verify
    type == Double.class || type == Double.TYPE)
        // TODO verify
        columnType = Types.DOUBLE;
    else if (type == String.class) {
        if (field.getAnnotation(Text.class) != null) {
            columnType = Types.LONGVARCHAR;
        } else {
            columnType = Types.VARCHAR;
            Max max = field.getAnnotation(Max.class);
            if (max == null) {
                // throw new SienaRestrictedApiException(DB, "createColumn", "Field "+field.getName()+" in class "
                // +clazz.getName()+" doesn't have a @Max annotation");
                // default is 255 chars as in hibernate
                column.setSize("255");
            } else
                column.setSize("" + max.value());
        }
    } else if (type == Boolean.class || type == Boolean.TYPE)
        columnType = Types.BOOLEAN;
    else if (type == Date.class) {
        if (field.getAnnotation(DateTime.class) != null)
            columnType = Types.TIMESTAMP;
        else if (field.getAnnotation(Time.class) != null)
            columnType = Types.TIME;
        else if (field.getAnnotation(SimpleDate.class) != null)
            columnType = Types.DATE;
        else
            columnType = Types.TIMESTAMP;
    } else if (type == Json.class) {
        columnType = Types.LONGVARCHAR;
    } else if (type == byte[].class) {
        columnType = Types.BLOB;
    } else if (Enum.class.isAssignableFrom(type)) {
        // enums are stored as string
        columnType = Types.VARCHAR;
        Max max = field.getAnnotation(Max.class);
        if (max == null)
            // fixes by default to this value in order to prevent alter tables every time
            column.setSize("" + 255);
        else
            column.setSize("" + max.value());
    } else if (type == BigDecimal.class) {
        DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
        if (an == null) {
            columnType = Types.DECIMAL;
            column.setSizeAndScale(19, 2);
        } else {
            if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
                columnType = Types.DECIMAL;
                column.setSizeAndScale(an.size(), an.scale());
            } else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
                columnType = Types.VARCHAR;
                // should be an.size+"."+sign
                column.setSize((an.size() + 2) + "");
            } else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
                columnType = Types.DOUBLE;
            } else {
                columnType = Types.DECIMAL;
                column.setSizeAndScale(19, 2);
            }
        }
    } else {
        Embedded embedded = field.getAnnotation(Embedded.class);
        if (embedded != null) {
            if ("h2".equals(DB)) {
                columnType = Types.CLOB;
            } else {
                columnType = Types.LONGVARCHAR;
            }
        } else if (field.isAnnotationPresent(Polymorphic.class)) {
            columnType = Types.BLOB;
        } else {
            throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + clazz.getName() + "." + field.getName());
        }
    }
    column.setTypeCode(columnType);
    return column;
}
Also used : Max(siena.Max) SienaRestrictedApiException(siena.SienaRestrictedApiException) DecimalPrecision(siena.core.DecimalPrecision) Text(siena.Text) Json(siena.Json) DateTime(siena.DateTime) IndexColumn(org.apache.ddlutils.model.IndexColumn) Column(org.apache.ddlutils.model.Column) SimpleDate(siena.SimpleDate) Embedded(siena.embed.Embedded) Polymorphic(siena.core.Polymorphic)

Example 5 with Embedded

use of siena.embed.Embedded in project siena by mandubian.

the class GaeNativeSerializer method embed.

public static void embed(Entity entity, String embeddingColumnName, Object embeddedObj, int level) {
    // the level prevents from stackoverflow in case of a circular ref
    if (level > 2)
        return;
    Class<?> clazz = embeddedObj.getClass();
    if (clazz.isArray() || Collection.class.isAssignableFrom(clazz)) {
        throw new SienaException("can't serializer Array/Collection in native mode");
    }
    for (Field f : ClassInfo.getClassInfo(clazz).allFields) {
        // doesn't try to analyze fields, just try to store it
        Class<?> fieldClass = f.getType();
        String propName = embeddingColumnName + "." + ClassInfo.getSingleColumnName(f);
        Object propValue = Util.readField(embeddedObj, f);
        if (propValue != null) {
            if (fieldClass == Json.class) {
                propValue = propValue.toString();
            } else if (propValue instanceof String) {
                String s = (String) propValue;
                if (s.length() > 500)
                    propValue = new Text(s);
            } else if (propValue instanceof byte[]) {
                byte[] arr = (byte[]) propValue;
                // GAE Blob doesn't accept more than 1MB
                if (arr.length < 1000000)
                    propValue = new Blob(arr);
                else
                    propValue = new Blob(Arrays.copyOf(arr, 1000000));
            } else if (ClassInfo.isEmbedded(f)) {
                Embedded embed = f.getAnnotation(Embedded.class);
                switch(embed.mode()) {
                    case SERIALIZE_JSON:
                        propValue = JsonSerializer.serialize(propValue).toString();
                        String s = (String) propValue;
                        if (s.length() > 500)
                            propValue = new Text(s);
                        break;
                    case SERIALIZE_JAVA:
                        // this embedding mode doesn't manage @EmbedIgnores
                        try {
                            byte[] b = JavaSerializer.serialize(propValue);
                            // if length is less than 1Mb, can store in a blob else???
                            if (b.length <= 1000000) {
                                propValue = 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, embeddingColumnName + "." + ClassInfo.getSingleColumnName(f), propValue, level + 1);
                        // has set several new properties in entity so go to next field
                        continue;
                }
            } else if (fieldClass == BigDecimal.class) {
                DecimalPrecision ann = f.getAnnotation(DecimalPrecision.class);
                if (ann == null) {
                    propValue = ((BigDecimal) propValue).toPlainString();
                } else {
                    switch(ann.storageType()) {
                        case DOUBLE:
                            propValue = ((BigDecimal) propValue).doubleValue();
                            break;
                        case STRING:
                        case NATIVE:
                            propValue = ((BigDecimal) propValue).toPlainString();
                            break;
                    }
                }
            } else // don't know if anyone will use it but it will work :)
            if (Enum.class.isAssignableFrom(fieldClass)) {
                propValue = propValue.toString();
            } else if (ClassInfo.isModel(fieldClass)) {
                // if it's a model and as there a no join, we can't fetch everything! So only the key is embedded
                // GaeNativeSerializer.embed(entity, embeddingColumnName + "." + ClassInfo.getSingleColumnName(f), propValue, level + 1);
                propValue = Util.readField(propValue, ClassInfo.getIdField(fieldClass));
            }
        }
        Unindexed ui = f.getAnnotation(Unindexed.class);
        if (ui == null) {
            entity.setProperty(propName, propValue);
        } else {
            entity.setUnindexedProperty(propName, propValue);
        }
    }
}
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) Collection(java.util.Collection) SienaException(siena.SienaException) Embedded(siena.embed.Embedded)

Aggregations

Embedded (siena.embed.Embedded)5 DecimalPrecision (siena.core.DecimalPrecision)4 IOException (java.io.IOException)3 Blob (com.google.appengine.api.datastore.Blob)2 Text (com.google.appengine.api.datastore.Text)2 Field (java.lang.reflect.Field)2 BigDecimal (java.math.BigDecimal)2 DateTime (siena.DateTime)2 Json (siena.Json)2 SienaException (siena.SienaException)2 SienaRestrictedApiException (siena.SienaRestrictedApiException)2 SimpleDate (siena.SimpleDate)2 Polymorphic (siena.core.Polymorphic)2 Key (com.google.appengine.api.datastore.Key)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Column (org.apache.ddlutils.model.Column)1 IndexColumn (org.apache.ddlutils.model.IndexColumn)1