From a1ab11053107c8995b3f3e850fa14a2374c2013a Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 4 Aug 2011 22:22:24 -0400 Subject: Customizable enumId() mapping (issue 2) --- src/com/iciql/Iciql.java | 35 ++++++++++++++++++++++++----- src/com/iciql/IciqlException.java | 7 ++++-- src/com/iciql/ModelUtils.java | 1 + src/com/iciql/TableDefinition.java | 16 +++++++++++++- src/com/iciql/util/Utils.java | 45 ++++++++++++++++++++++++-------------- 5 files changed, 80 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/com/iciql/Iciql.java b/src/com/iciql/Iciql.java index 8d71aa6..ff2452e 100644 --- a/src/com/iciql/Iciql.java +++ b/src/com/iciql/Iciql.java @@ -91,9 +91,22 @@ import java.lang.annotation.Target; * byte [] * BLOB * + * + * java.lang.Enum.name() + * VARCHAR (maxLength > 0) or TEXT (maxLength == 0)
EnumType.STRING + * + * + * java.lang.Enum.ordinal() + * INT
EnumType.ORDINAL + * + * + * java.lang.Enum implements
com.iciql.Iciql.EnumID.enumId() + * INT
EnumType.ENUMID + * + * * *

- * Unsupported data types: java.lang.Enum, Array Types, and custom types. + * Unsupported data types: primitives, Array Types, and custom types. *

* Table and field mapping: by default, the mapped table name is the class name * and the public fields are reflectively mapped, by their name, to columns. As @@ -373,15 +386,27 @@ public interface Iciql { } /** - * Enumeration representing now to map a java.lang.Enum to a column. + * Interface for using the EnumType.ENUMID enumeration mapping strategy. + *

+ * Enumerations wishing to use EnumType.ENUMID must implement this + * interface. + */ + public interface EnumId { + int enumId(); + } + + /** + * Enumeration representing how to map a java.lang.Enum to a column. *

*

+ * @see com.iciql.Iciql.EnumId interface */ public enum EnumType { - STRING, ORDINAL; + STRING, ORDINAL, ENUMID; } /** diff --git a/src/com/iciql/IciqlException.java b/src/com/iciql/IciqlException.java index 2aee36d..9f411d9 100644 --- a/src/com/iciql/IciqlException.java +++ b/src/com/iciql/IciqlException.java @@ -16,6 +16,8 @@ package com.iciql; +import java.text.MessageFormat; + /** * Iciql wraps all exceptions with this class. */ @@ -23,8 +25,9 @@ public class IciqlException extends RuntimeException { private static final long serialVersionUID = 1L; - public IciqlException(String message) { - super(message); + public IciqlException(String message, Object... parameters) { + super(parameters.length > 0 ? MessageFormat.format(message, parameters) : message); + } public IciqlException(Throwable t) { diff --git a/src/com/iciql/ModelUtils.java b/src/com/iciql/ModelUtils.java index 45a4882..9cbd295 100644 --- a/src/com/iciql/ModelUtils.java +++ b/src/com/iciql/ModelUtils.java @@ -149,6 +149,7 @@ class ModelUtils { } return "VARCHAR"; case ORDINAL: + case ENUMID: return "INT"; } } diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index a38ac51..72b5348 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -27,6 +27,7 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; +import com.iciql.Iciql.EnumId; import com.iciql.Iciql.EnumType; import com.iciql.Iciql.IQColumn; import com.iciql.Iciql.IQEnum; @@ -97,8 +98,15 @@ class TableDefinition { if (!field.isAccessible()) { field.setAccessible(true); } - o = Utils.convert(o, field.getType()); + Class targetType = field.getType(); + if (targetType.isEnum()) { + o = Utils.convertEnum(o, targetType, enumType); + } else { + o = Utils.convert(o, targetType); + } field.set(obj, o); + } catch (IciqlException e) { + throw e; } catch (Exception e) { throw new IciqlException(e); } @@ -344,6 +352,12 @@ class TableDefinition { return iqenum.name(); case ORDINAL: return iqenum.ordinal(); + case ENUMID: + if (!EnumId.class.isAssignableFrom(value.getClass())) { + throw new IciqlException(field.field.getName() + " does not implement EnumId!"); + } + EnumId enumid = (EnumId) value; + return enumid.enumId(); } } if (field.trimString && field.maxLength > 0) { diff --git a/src/com/iciql/util/Utils.java b/src/com/iciql/util/Utils.java index 39875fb..302dd4d 100644 --- a/src/com/iciql/util/Utils.java +++ b/src/com/iciql/util/Utils.java @@ -37,6 +37,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; +import com.iciql.Iciql.EnumId; +import com.iciql.Iciql.EnumType; import com.iciql.IciqlException; /** @@ -209,10 +211,7 @@ public class Utils { if (targetType.isAssignableFrom(currentType)) { return o; } - // convert enum - if (targetType.isEnum()) { - return convertEnum(o, targetType); - } + // convert from CLOB/TEXT/VARCHAR to String if (targetType == String.class) { if (Clob.class.isAssignableFrom(currentType)) { @@ -257,16 +256,18 @@ public class Utils { } } } - - throw new IciqlException("Can not convert the value " + o + " from " + currentType + " to " - + targetType); + throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType); } - - private static Object convertEnum(Object o, Class targetType) { + + public static Object convertEnum(Object o, Class targetType, EnumType type) { if (o == null) { return null; } Class currentType = o.getClass(); + if (targetType.isAssignableFrom(currentType)) { + return o; + } + // convert from VARCHAR/TEXT/INT to Enum Enum[] values = (Enum[]) targetType.getEnumConstants(); if (Clob.class.isAssignableFrom(currentType)) { @@ -297,16 +298,28 @@ public class Utils { } else if (Number.class.isAssignableFrom(currentType)) { // INT field int n = ((Number) o).intValue(); - - // ORDINAL mapping - for (Enum value : values) { - if (value.ordinal() == n) { - return value; + if (type.equals(EnumType.ORDINAL)) { + // ORDINAL mapping + for (Enum value : values) { + if (value.ordinal() == n) { + return value; + } + } + } else if (type.equals(EnumType.ENUMID)) { + if (!EnumId.class.isAssignableFrom(targetType)) { + throw new IciqlException("Can not convert the value {0} from {1} to {2} using ENUMID", o, + currentType, targetType); + } + // ENUMID mapping + for (Enum value : values) { + EnumId enumid = (EnumId) value; + if (enumid.enumId() == n) { + return value; + } } } } - throw new IciqlException("Can not convert the value " + o + " from " + currentType + " to " - + targetType); + throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType); } /** -- cgit v1.2.3