Search in sources :

Example 1 with DecimalPrecision

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

use of siena.core.DecimalPrecision 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 3 with DecimalPrecision

use of siena.core.DecimalPrecision in project siena by mandubian.

the class JdbcPersistenceManager method addParameters.

protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
    for (Field field : fields) {
        Class<?> type = field.getType();
        if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
            JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
            Object rel = Util.readField(obj, field);
            for (Field f : ci.keys) {
                if (rel != null) {
                    Object value = Util.readField(rel, f);
                    if (value instanceof Json)
                        value = ((Json) value).toString();
                    setParameter(ps, i++, value);
                } else {
                    setParameter(ps, i++, null);
                }
            }
        } else {
            Object value = Util.readField(obj, field);
            if (value != null) {
                if (Json.class.isAssignableFrom(type)) {
                    value = ((Json) value).toString();
                } else if (field.getAnnotation(Embedded.class) != null) {
                    value = JsonSerializer.serialize(value).toString();
                } else if (field.getAnnotation(Polymorphic.class) != null) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ObjectOutput out;
                    try {
                        out = new ObjectOutputStream(bos);
                        out.writeObject(value);
                        out.close();
                    } catch (IOException e) {
                        throw new SienaException(e);
                    }
                    value = bos.toByteArray();
                } else if (Enum.class.isAssignableFrom(type)) {
                    value = value.toString();
                } else if (BigDecimal.class == type) {
                    DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
                    if (ann == null) {
                        value = (BigDecimal) value;
                    } else {
                        switch(ann.storageType()) {
                            case DOUBLE:
                                value = ((BigDecimal) value).doubleValue();
                                break;
                            case STRING:
                                value = ((BigDecimal) value).toPlainString();
                                break;
                            case NATIVE:
                                value = (BigDecimal) value;
                                break;
                        }
                    }
                }
            }
            setParameter(ps, i++, value);
        }
    }
    return i;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 4 with DecimalPrecision

use of siena.core.DecimalPrecision in project siena by mandubian.

the class GoogleSqlPersistenceManager method addParameters.

@Override
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
    for (Field field : fields) {
        Class<?> type = field.getType();
        if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
            JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
            Object rel = Util.readField(obj, field);
            for (Field f : ci.keys) {
                if (rel != null) {
                    Object value = Util.readField(rel, f);
                    if (value instanceof Json)
                        value = ((Json) value).toString();
                    setParameter(ps, i++, value, f);
                } else {
                    setParameter(ps, i++, null, f);
                }
            }
        } else {
            Object value = Util.readField(obj, field);
            if (value != null) {
                if (Json.class.isAssignableFrom(type)) {
                    value = ((Json) value).toString();
                } else if (field.getAnnotation(Embedded.class) != null) {
                    value = JsonSerializer.serialize(value).toString();
                } else if (field.getAnnotation(Polymorphic.class) != null) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ObjectOutput out;
                    try {
                        out = new ObjectOutputStream(bos);
                        out.writeObject(value);
                        out.close();
                    } catch (IOException e) {
                        throw new SienaException(e);
                    }
                    value = bos.toByteArray();
                } else if (Enum.class.isAssignableFrom(type)) {
                    value = value.toString();
                } else if (BigDecimal.class == type) {
                    DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
                    if (ann == null) {
                        value = (BigDecimal) value;
                    } else {
                        switch(ann.storageType()) {
                            case DOUBLE:
                                value = ((BigDecimal) value).doubleValue();
                                break;
                            case STRING:
                                value = ((BigDecimal) value).toPlainString();
                                break;
                            case NATIVE:
                                value = (BigDecimal) value;
                                break;
                        }
                    }
                }
            }
            setParameter(ps, i++, value, field);
        }
    }
    return i;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 5 with DecimalPrecision

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

Aggregations

DecimalPrecision (siena.core.DecimalPrecision)8 IOException (java.io.IOException)6 SienaException (siena.SienaException)6 Field (java.lang.reflect.Field)5 Json (siena.Json)5 Embedded (siena.embed.Embedded)5 Polymorphic (siena.core.Polymorphic)4 BigDecimal (java.math.BigDecimal)3 SienaRestrictedApiException (siena.SienaRestrictedApiException)3 Blob (com.google.appengine.api.datastore.Blob)2 Text (com.google.appengine.api.datastore.Text)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2 DateTime (siena.DateTime)2 SimpleDate (siena.SimpleDate)2 Key (com.google.appengine.api.datastore.Key)1 BufferedReader (java.io.BufferedReader)1 ObjectInputStream (java.io.ObjectInputStream)1