use of javax.persistence.Enumerated in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method setField.
@DB(txn = false)
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
try {
final Class<?> type = field.getType();
if (type == String.class) {
byte[] bytes = rs.getBytes(index);
if (bytes != null) {
try {
if (field.getAnnotation(Column.class).encryptable()) {
field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
} else {
field.set(entity, new String(bytes, "UTF-8"));
}
} catch (IllegalArgumentException e) {
assert (false);
throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
} catch (UnsupportedEncodingException e) {
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
}
} else {
field.set(entity, null);
}
} else if (type == long.class) {
field.setLong(entity, rs.getLong(index));
} else if (type == Long.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getLong(index));
}
} else if (type.isEnum()) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
for (final Enum<?> e : enums) {
if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
field.set(entity, e);
return;
}
}
} else if (type == int.class) {
field.set(entity, rs.getInt(index));
} else if (type == Integer.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getInt(index));
}
} else if (type == Date.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
} else if (type == Calendar.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
field.set(entity, cal);
} else if (type == boolean.class) {
field.setBoolean(entity, rs.getBoolean(index));
} else if (type == Boolean.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getBoolean(index));
}
} else if (type == URI.class) {
try {
String str = rs.getString(index);
field.set(entity, str == null ? null : new URI(str));
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
}
} else if (type == URL.class) {
try {
String str = rs.getString(index);
field.set(entity, str != null ? new URL(str) : null);
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
}
} else if (type == Ip.class) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
Ip ip = null;
if (enumType == EnumType.STRING) {
String s = rs.getString(index);
ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
} else {
ip = new Ip(rs.getLong(index));
}
field.set(entity, ip);
} else if (type == short.class) {
field.setShort(entity, rs.getShort(index));
} else if (type == Short.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getShort(index));
}
} else if (type == float.class) {
field.setFloat(entity, rs.getFloat(index));
} else if (type == Float.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getFloat(index));
}
} else if (type == double.class) {
field.setDouble(entity, rs.getDouble(index));
} else if (type == Double.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getDouble(index));
}
} else if (type == byte.class) {
field.setByte(entity, rs.getByte(index));
} else if (type == Byte.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getByte(index));
}
} else if (type == byte[].class) {
field.set(entity, rs.getBytes(index));
} else {
field.set(entity, rs.getObject(index));
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
use of javax.persistence.Enumerated in project jirm by agentgt.
the class SqlParameterDefinition method parameterDef.
static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
final SqlParameterDefinition definition;
String sn = null;
ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
if (manyToOne != null) {
Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
checkState(!od.getIdParameters().isEmpty(), "No id parameters");
if (joinColumn != null)
sn = joinColumn.name();
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
FetchType fetch = manyToOne.fetch();
int depth;
if (FetchType.LAZY == fetch) {
depth = 1;
} else {
depth = config.getMaximumLoadDepth();
}
SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
} else {
Column col = getAnnotation(objectType, parameterName, Column.class);
if (col != null && !isNullOrEmpty(col.name()))
sn = col.name();
Id id = getAnnotation(objectType, parameterName, Id.class);
Version version = getAnnotation(objectType, parameterName, Version.class);
GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
boolean idFlag = id != null;
boolean versionFlag = version != null;
boolean generatedFlag = generated != null;
if (sn == null)
sn = config.getNamingStrategy().propertyToColumnName(parameterName);
definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
}
return definition;
}
use of javax.persistence.Enumerated in project cloudstack by apache.
the class GenericDaoBase method setField.
@DB()
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
try {
final Class<?> type = field.getType();
if (type == String.class) {
byte[] bytes = rs.getBytes(index);
if (bytes != null) {
try {
Encrypt encrypt = field.getAnnotation(Encrypt.class);
if (encrypt != null && encrypt.encrypt()) {
field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
} else {
field.set(entity, new String(bytes, "UTF-8"));
}
} catch (IllegalArgumentException e) {
assert (false);
throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
} catch (UnsupportedEncodingException e) {
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
}
} else {
field.set(entity, null);
}
} else if (type == long.class) {
field.setLong(entity, rs.getLong(index));
} else if (type == Long.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getLong(index));
}
} else if (type.isEnum()) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
for (final Enum<?> e : enums) {
if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
field.set(entity, e);
return;
}
}
} else if (type == int.class) {
field.set(entity, rs.getInt(index));
} else if (type == Integer.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getInt(index));
}
} else if (type == Date.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
} else if (type == Calendar.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
field.set(entity, cal);
} else if (type == boolean.class) {
field.setBoolean(entity, rs.getBoolean(index));
} else if (type == Boolean.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getBoolean(index));
}
} else if (type == URI.class) {
try {
String str = rs.getString(index);
field.set(entity, str == null ? null : new URI(str));
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
}
} else if (type == URL.class) {
try {
String str = rs.getString(index);
field.set(entity, str != null ? new URL(str) : null);
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
}
} else if (type == Ip.class) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
Ip ip = null;
if (enumType == EnumType.STRING) {
String s = rs.getString(index);
ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
} else {
ip = new Ip(rs.getLong(index));
}
field.set(entity, ip);
} else if (type == short.class) {
field.setShort(entity, rs.getShort(index));
} else if (type == Short.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getShort(index));
}
} else if (type == float.class) {
field.setFloat(entity, rs.getFloat(index));
} else if (type == Float.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getFloat(index));
}
} else if (type == double.class) {
field.setDouble(entity, rs.getDouble(index));
} else if (type == Double.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getDouble(index));
}
} else if (type == byte.class) {
field.setByte(entity, rs.getByte(index));
} else if (type == Byte.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getByte(index));
}
} else if (type == byte[].class) {
field.set(entity, rs.getBytes(index));
} else {
field.set(entity, rs.getObject(index));
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
use of javax.persistence.Enumerated in project cosmic by MissionCriticalCloud.
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 (final 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 (final 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);
}
}
use of javax.persistence.Enumerated in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method prepareAttribute.
@DB(txn = false)
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, column.length())).getBytes("UTF-8"));
} else {
pstmt.setBytes(j, str.substring(0, column.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) {
pstmt.setInt(j, value == null ? null : ((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) {
pstmt.setLong(j, value == null ? null : (value instanceof Ip) ? ((Ip) value).longValue() : NetUtils.ip2Long((String) value));
}
} else {
pstmt.setObject(j, value);
}
}
Aggregations