use of javax.persistence.EnumType in project simplejpa by appoxy.
the class Save method getEnumValue.
static String getEnumValue(PersistentProperty field, Object ob) {
EnumType enumType = field.getEnumType();
Class retType = field.getPropertyClass();
Object propertyValue = field.getProperty(ob);
String toSet = null;
if (enumType == EnumType.STRING) {
toSet = propertyValue.toString();
} else {
// ordinal
Object[] enumConstants = retType.getEnumConstants();
for (int i = 0; i < enumConstants.length; i++) {
Object enumConstant = enumConstants[i];
if (enumConstant.equals(propertyValue)) {
toSet = Integer.toString(i);
break;
}
}
}
if (toSet == null) {
// should never happen
throw new PersistenceException("Enum value is null, couldn't find ordinal match: " + ob);
}
return toSet;
}
use of javax.persistence.EnumType in project requery by requery.
the class AttributeMember method processBasicColumnAnnotations.
private void processBasicColumnAnnotations(ElementValidator validator) {
if (annotationOf(Key.class).isPresent() || annotationOf(javax.persistence.Id.class).isPresent()) {
isKey = true;
if (isTransient) {
validator.error("Key field cannot be transient");
}
}
// generated keys can't be set through a setter
if (annotationOf(Generated.class).isPresent() || annotationOf(GeneratedValue.class).isPresent()) {
isGenerated = true;
isReadOnly = true;
// check generation strategy
annotationOf(GeneratedValue.class).ifPresent(generatedValue -> {
if (generatedValue.strategy() != GenerationType.IDENTITY && generatedValue.strategy() != GenerationType.AUTO) {
validator.warning("GeneratedValue.strategy() " + generatedValue.strategy() + " not supported", generatedValue.getClass());
}
});
}
if (annotationOf(Lazy.class).isPresent()) {
if (isKey) {
cannotCombine(validator, Key.class, Lazy.class);
}
isLazy = true;
}
if (annotationOf(Nullable.class).isPresent() || isOptional || Mirrors.findAnnotationMirror(element(), "javax.annotation.Nullable").isPresent()) {
isNullable = true;
} else {
// if not a primitive type the value assumed nullable
if (element().getKind().isField()) {
isNullable = !element().asType().getKind().isPrimitive();
} else if (element().getKind() == ElementKind.METHOD) {
ExecutableElement executableElement = (ExecutableElement) element();
isNullable = !executableElement.getReturnType().getKind().isPrimitive();
}
}
if (annotationOf(Version.class).isPresent() || annotationOf(javax.persistence.Version.class).isPresent()) {
isVersion = true;
if (isKey) {
cannotCombine(validator, Key.class, Version.class);
}
}
Column column = annotationOf(Column.class).orElse(null);
ForeignKey foreignKey = null;
boolean foreignKeySetFromColumn = false;
if (column != null) {
name = "".equals(column.name()) ? null : column.name();
isUnique = column.unique();
isNullable = column.nullable();
defaultValue = column.value();
collate = column.collate();
definition = column.definition();
if (column.length() > 0) {
length = column.length();
}
if (column.foreignKey().length > 0) {
foreignKey = column.foreignKey()[0];
foreignKeySetFromColumn = true;
}
}
if (!foreignKeySetFromColumn) {
foreignKey = annotationOf(ForeignKey.class).orElse(null);
}
if (foreignKey != null) {
this.isForeignKey = true;
deleteAction = foreignKey.delete();
updateAction = foreignKey.update();
referencedColumn = foreignKey.referencedColumn();
}
annotationOf(Index.class).ifPresent(index -> {
isIndexed = true;
Collections.addAll(indexNames, index.value());
});
// JPA specific
annotationOf(Basic.class).ifPresent(basic -> {
isNullable = basic.optional();
isLazy = basic.fetch() == FetchType.LAZY;
});
annotationOf(javax.persistence.Index.class).ifPresent(index -> {
isIndexed = true;
Collections.addAll(indexNames, index.name());
});
annotationOf(JoinColumn.class).ifPresent(joinColumn -> {
javax.persistence.ForeignKey joinForeignKey = joinColumn.foreignKey();
this.isForeignKey = true;
ConstraintMode constraintMode = joinForeignKey.value();
switch(constraintMode) {
default:
case PROVIDER_DEFAULT:
case CONSTRAINT:
deleteAction = ReferentialAction.CASCADE;
updateAction = ReferentialAction.CASCADE;
break;
case NO_CONSTRAINT:
deleteAction = ReferentialAction.NO_ACTION;
updateAction = ReferentialAction.NO_ACTION;
break;
}
this.referencedTable = joinColumn.table();
this.referencedColumn = joinColumn.referencedColumnName();
});
annotationOf(javax.persistence.Column.class).ifPresent(persistenceColumn -> {
name = "".equals(persistenceColumn.name()) ? null : persistenceColumn.name();
isUnique = persistenceColumn.unique();
isNullable = persistenceColumn.nullable();
length = persistenceColumn.length();
isReadOnly = !persistenceColumn.updatable();
definition = persistenceColumn.columnDefinition();
});
annotationOf(Enumerated.class).ifPresent(enumerated -> {
EnumType enumType = enumerated.value();
if (enumType == EnumType.ORDINAL) {
converterType = EnumOrdinalConverter.class.getCanonicalName();
}
});
}
use of javax.persistence.EnumType 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);
}
}
use of javax.persistence.EnumType 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