Search in sources :

Example 6 with Enumerated

use of javax.persistence.Enumerated in project cloudstack by apache.

the class GenericDaoBase method prepareAttribute.

@DB()
protected void prepareAttribute(final int j, final PreparedStatement pstmt, final Attribute attr, Object value) throws SQLException {
    if (attr.is(Attribute.Flag.DaoGenerated) && value == null) {
        value = generateValue(attr);
        if (attr.field == null) {
            pstmt.setObject(j, value);
            return;
        }
    }
    if (attr.field.getType() == String.class) {
        final String str = (String) value;
        if (str == null) {
            pstmt.setString(j, null);
            return;
        }
        final Column column = attr.field.getAnnotation(Column.class);
        final int length = column != null ? column.length() : 255;
        // to support generic localization, utilize MySql UTF-8 support
        if (length < str.length()) {
            try {
                if (attr.is(Attribute.Flag.Encrypted)) {
                    pstmt.setBytes(j, DBEncryptionUtil.encrypt(str.substring(0, length)).getBytes("UTF-8"));
                } else {
                    pstmt.setBytes(j, str.substring(0, length).getBytes("UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                // no-way it can't support UTF-8 encoding
                assert (false);
                throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
            }
        } else {
            try {
                if (attr.is(Attribute.Flag.Encrypted)) {
                    pstmt.setBytes(j, DBEncryptionUtil.encrypt(str).getBytes("UTF-8"));
                } else {
                    pstmt.setBytes(j, str.getBytes("UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                // no-way it can't support UTF-8 encoding
                assert (false);
                throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
            }
        }
    } else if (attr.field.getType() == Date.class) {
        final Date date = (Date) value;
        if (date == null) {
            pstmt.setObject(j, null);
            return;
        }
        if (attr.is(Attribute.Flag.Date)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        } else if (attr.is(Attribute.Flag.TimeStamp)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        } else if (attr.is(Attribute.Flag.Time)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        }
    } else if (attr.field.getType() == Calendar.class) {
        final Calendar cal = (Calendar) value;
        if (cal == null) {
            pstmt.setObject(j, null);
            return;
        }
        if (attr.is(Attribute.Flag.Date)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        } else if (attr.is(Attribute.Flag.TimeStamp)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        } else if (attr.is(Attribute.Flag.Time)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        }
    } else if (attr.field.getType().isEnum()) {
        final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
        final EnumType type = (enumerated == null) ? EnumType.STRING : enumerated.value();
        if (type == EnumType.STRING) {
            pstmt.setString(j, value == null ? null : value.toString());
        } else if (type == EnumType.ORDINAL) {
            if (value == null) {
                pstmt.setObject(j, null);
            } else {
                pstmt.setInt(j, ((Enum<?>) value).ordinal());
            }
        }
    } else if (attr.field.getType() == URI.class) {
        pstmt.setString(j, value == null ? null : value.toString());
    } else if (attr.field.getType() == URL.class) {
        pstmt.setURL(j, (URL) value);
    } else if (attr.field.getType() == byte[].class) {
        pstmt.setBytes(j, (byte[]) value);
    } else if (attr.field.getType() == Ip.class) {
        final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
        final EnumType type = (enumerated == null) ? EnumType.ORDINAL : enumerated.value();
        if (type == EnumType.STRING) {
            pstmt.setString(j, value == null ? null : value.toString());
        } else if (type == EnumType.ORDINAL) {
            if (value == null) {
                pstmt.setObject(j, null);
            } else {
                pstmt.setLong(j, (value instanceof Ip) ? ((Ip) value).longValue() : NetUtils.ip2Long((String) value));
            }
        }
    } else {
        pstmt.setObject(j, value);
    }
}
Also used : Enumerated(javax.persistence.Enumerated) Column(javax.persistence.Column) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) EnumType(javax.persistence.EnumType) Calendar(java.util.Calendar) Ip(com.cloud.utils.net.Ip) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) URL(java.net.URL)

Aggregations

Enumerated (javax.persistence.Enumerated)6 EnumType (javax.persistence.EnumType)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 Ip (com.cloud.utils.net.Ip)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URL (java.net.URL)4 Calendar (java.util.Calendar)4 Column (javax.persistence.Column)4 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Date (java.util.Date)2 GeneratedValue (javax.persistence.GeneratedValue)2 JoinColumn (javax.persistence.JoinColumn)2 Column (io.requery.Column)1 ForeignKey (io.requery.ForeignKey)1 Index (io.requery.Index)1 Lazy (io.requery.Lazy)1 Nullable (io.requery.Nullable)1 EnumOrdinalConverter (io.requery.converter.EnumOrdinalConverter)1