Advertisement
Guest User

sfussenegger

a guest
Oct 20th, 2009
3,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package at.molindo.util.hibernate;
  2.  
  3. import java.io.Serializable;
  4. import java.lang.reflect.Method;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9.  
  10. import org.hibernate.HibernateException;
  11. import org.hibernate.type.NullableType;
  12. import org.hibernate.type.TypeFactory;
  13. import org.hibernate.usertype.ParameterizedType;
  14. import org.hibernate.usertype.UserType;
  15.  
  16. public class EnumUserType implements UserType, ParameterizedType {
  17.     private static final String DEFAULT_IDENTIFIER_METHOD_NAME = "name";
  18.     private static final String DEFAULT_VALUE_OF_METHOD_NAME = "valueOf";
  19.  
  20.     protected static final String IDENTIFIER_METHOD = "identifierMethod";
  21.     protected static final String VALUEOF_METHOD = "valueOfMethod";
  22.  
  23.     @SuppressWarnings("unchecked")
  24.     private Class<? extends Enum> enumClass;
  25.     private Class<?> identifierType;
  26.     private Method identifierMethod;
  27.     private Method valueOfMethod;
  28.     private NullableType type;
  29.     private int[] sqlTypes;
  30.  
  31.     public void setParameterValues(final Properties parameters) {
  32.         final String enumClassName = parameters.getProperty("enumClass");
  33.         try {
  34.             enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
  35.         } catch (final ClassNotFoundException cfne) {
  36.             throw new HibernateException("Enum class not found", cfne);
  37.         }
  38.  
  39.         final String identifierMethodName = parameters
  40.                 .getProperty(IDENTIFIER_METHOD, DEFAULT_IDENTIFIER_METHOD_NAME);
  41.  
  42.         try {
  43.             identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]);
  44.             identifierType = identifierMethod.getReturnType();
  45.         } catch (final Exception e) {
  46.             throw new HibernateException("Failed to obtain identifier method", e);
  47.         }
  48.  
  49.         type = (NullableType) TypeFactory.basic(identifierType.getName());
  50.  
  51.         if (type == null) {
  52.             throw new HibernateException("Unsupported identifier type " + identifierType.getName());
  53.         }
  54.  
  55.         sqlTypes = new int[] { type.sqlType() };
  56.  
  57.         final String valueOfMethodName = parameters
  58.                 .getProperty(VALUEOF_METHOD, DEFAULT_VALUE_OF_METHOD_NAME);
  59.  
  60.         try {
  61.             valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType });
  62.         } catch (final Exception e) {
  63.             throw new HibernateException("Failed to obtain valueOf method", e);
  64.         }
  65.     }
  66.  
  67.     public Class<?> returnedClass() {
  68.         return enumClass;
  69.     }
  70.  
  71.     public Object nullSafeGet(final ResultSet rs, final String[] names, final Object owner) throws HibernateException, SQLException {
  72.         final Object identifier = type.get(rs, names[0]);
  73.         if (rs.wasNull()) {
  74.             return null;
  75.         }
  76.  
  77.         try {
  78.             return valueOfMethod.invoke(enumClass, new Object[] { identifier });
  79.         } catch (final Exception e) {
  80.             throw new HibernateException("Exception while invoking valueOf method '"
  81.                     + valueOfMethod.getName() + "' of " + "enumeration class '" + enumClass + "'", e);
  82.         }
  83.     }
  84.  
  85.     public void nullSafeSet(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException {
  86.         try {
  87.             if (value == null) {
  88.                 st.setNull(index, type.sqlType());
  89.             } else {
  90.                 final Object identifier = identifierMethod.invoke(value, new Object[0]);
  91.                 type.set(st, identifier, index);
  92.             }
  93.         } catch (final Exception e) {
  94.             throw new HibernateException("Exception while invoking identifierMethod '"
  95.                     + identifierMethod.getName() + "' of " + "enumeration class '" + enumClass
  96.                     + "'", e);
  97.         }
  98.     }
  99.  
  100.     public int[] sqlTypes() {
  101.         return sqlTypes;
  102.     }
  103.  
  104.     public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
  105.         return cached;
  106.     }
  107.  
  108.     public Object deepCopy(final Object value) throws HibernateException {
  109.         return value;
  110.     }
  111.  
  112.     public Serializable disassemble(final Object value) throws HibernateException {
  113.         return (Serializable) value;
  114.     }
  115.  
  116.     public boolean equals(final Object x, final Object y) throws HibernateException {
  117.         return x == y;
  118.     }
  119.  
  120.     public int hashCode(final Object x) throws HibernateException {
  121.         return x.hashCode();
  122.     }
  123.  
  124.     public boolean isMutable() {
  125.         return false;
  126.     }
  127.  
  128.     public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
  129.         return original;
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement