#443 - ENH: Enum mapping - Add annotation @DbEnumValue that can be used to on a enum method which provides DB values

This commit is contained in:
Robin Bygrave committed 2015-10-30 21:42:21 +13:00
1 parent fc0b47342b
commit b326724139
10 files changed
+247 -20

No files matched your search

@@ -111,8 +111,8 @@ public class DeployUtil {
}
ScalarType<?> scalarType = typeManager.getScalarType(enumType);
if (scalarType == null) {
// see if it has a Mapping in avaje.properties
scalarType = typeManager.createEnumScalarType(enumType);
// look for @DbEnumValue or @EnumValue annotations etc
scalarType = typeManager.createEnumScalarType((Class<? extends Enum<?>>)enumType);
if (scalarType == null) {
// use JPA normal Enum type (without mapping)
EnumType type = enumerated != null ? enumerated.value() : null;
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.annotation.DbEnumType;
import com.avaje.ebean.annotation.DbEnumValue;
import com.avaje.ebean.annotation.EnumMapping;
import com.avaje.ebean.annotation.EnumValue;
import com.avaje.ebean.config.*;
@@ -21,6 +23,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
@@ -478,7 +481,16 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
* much shorter codes used in the DB.
* </p>
*/
public ScalarType<?> createEnumScalarType(Class<?> enumType) {
public ScalarType<?> createEnumScalarType(Class<? extends Enum<?>> enumType) {
Method[] methods = enumType.getMethods();
for (int i = 0; i <methods.length; i++) {
DbEnumValue dbValue = methods[i].getAnnotation(DbEnumValue.class);
if (dbValue != null) {
boolean integerValues = DbEnumType.INTEGER == dbValue.storage();
return createEnumScalarTypeDbValue(enumType, methods[i], integerValues);
}
}
// get the mapping information from EnumMapping
EnumMapping enumMapping = enumType.getAnnotation(EnumMapping.class);
@@ -496,6 +508,33 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
return createEnumScalarType(enumType, nameValueMap, integerType, dbColumnLength);
}
/**
* Create the Mapping of Enum fields to DB values using EnumValue annotations.
* <p>
* Return null if the EnumValue annotations are not present/used.
* </p>
*/
private ScalarType<?> createEnumScalarTypeDbValue(Class<? extends Enum<?>> enumType, Method method, boolean integerType) {
Map<String, String> nameValueMap = new HashMap<String, String>();
Enum<?>[] enumConstants = enumType.getEnumConstants();
for (int i = 0; i < enumConstants.length; i++) {
try {
Object value = method.invoke(enumConstants[i]);
nameValueMap.put(enumConstants[i].name(), value.toString());
} catch (Exception e) {
throw new IllegalArgumentException("Error trying to invoke DbEnumValue method on "+enumConstants[i], e);
}
}
if (nameValueMap.isEmpty()) {
// Not using EnumValue here
return null;
}
return createEnumScalarType(enumType, nameValueMap, integerType, 0);
}
/**
* Given the name value mapping and integer/string type and explicit DB column
* length create the ScalarType for the Enum.
@@ -55,7 +55,7 @@ public interface TypeManager {
* Create a ScalarType for an Enum using a mapping (rather than JPA Ordinal
* or String which has limitations).
*/
ScalarType<?> createEnumScalarType(Class<?> enumType);
ScalarType<?> createEnumScalarType(Class<? extends Enum<?>> enumType);
/**
* Return the ScalarType used to handle JSON content.