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);
}
}
Aggregations