1 package org.splat.dal.bo.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.io.Serializable;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13 import java.util.Properties;
15 import org.hibernate.HibernateException;
16 import org.hibernate.usertype.EnhancedUserType;
17 import org.hibernate.usertype.ParameterizedType;
20 public class GenericEnumType implements EnhancedUserType, ParameterizedType {
22 @SuppressWarnings("unchecked")
23 private Class<Enum> enumClass;
25 @SuppressWarnings("unchecked")
26 public void setParameterValues (Properties parameters) {
27 // ------------------------------------------------------
28 String enumClassName = parameters.getProperty("enumClassName");
30 enumClass = (Class<Enum>) Class.forName(enumClassName);
32 catch (ClassNotFoundException cnfe) {
33 throw new HibernateException("Enum class not found", cnfe);
37 public Object assemble (Serializable cached, Object owner) throws HibernateException {
38 // ----------------------------------------------------------
42 public Object deepCopy (Object value) throws HibernateException {
43 // -------------------------------------
47 @SuppressWarnings("unchecked")
48 public Serializable disassemble (Object value) throws HibernateException {
49 // ----------------------------------------------
53 public boolean equals (Object x, Object y) throws HibernateException {
54 // ------------------------------------------
58 public int hashCode (Object x) throws HibernateException {
59 // ------------------------------
63 public boolean isMutable () {
64 // ---------------------------
68 @SuppressWarnings("unchecked")
69 public Object nullSafeGet (ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
70 // ----------------------------------------------------------------------
71 String name = rs.getString( names[0] );
72 return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
75 @SuppressWarnings("unchecked")
76 public void nullSafeSet (PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
77 // -----------------------------------------------------------------------
79 st.setNull(index, Types.VARCHAR);
82 st.setString( index, ( (Enum) value ).name() );
86 public Object replace (Object original, Object target, Object owner) throws HibernateException {
87 // --------------------------------------------------------------------
91 @SuppressWarnings("unchecked")
92 public Class returnedClass () {
93 // -----------------------------
97 public int[] sqlTypes () {
98 // ------------------------
99 return new int[] { Types.VARCHAR };
102 @SuppressWarnings("unchecked")
103 public Object fromXMLString (String xmlValue) {
104 // ---------------------------------------------
105 return Enum.valueOf(enumClass, xmlValue);
108 @SuppressWarnings("unchecked")
109 public String objectToSQLString (Object value) {
110 // ----------------------------------------------
111 return '\'' + ( (Enum) value ).name() + '\'';
114 @SuppressWarnings("unchecked")
115 public String toXMLString (Object value) {
116 // ----------------------------------------
117 return ( (Enum) value ).name();