001    /**
002     * DataBaseConnector.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 02/01/2007
008     */
009    package jcolibri.connector.databaseutils;
010    
011    import java.io.Serializable; 
012    import java.sql.PreparedStatement; 
013    import java.sql.ResultSet; 
014    import java.sql.SQLException; 
015    import java.sql.Types; 
016    import org.hibernate.HibernateException; 
017    import org.hibernate.usertype.UserType; 
018    import java.util.Properties;
019    
020    import org.hibernate.MappingException;
021    import org.hibernate.usertype.ParameterizedType;
022    
023    /**
024     * Class that allows to use Enums in the DataBaseConnector.<p>
025     * This is a Hibernate subclass that wraps java 1.5 Enums.<p>
026     * @author Juan A. Recio-García
027     * @version 2.0
028     */
029    public class EnumUserType implements UserType, ParameterizedType {
030       
031       private Class clazz = null;
032       
033       public void setParameterValues(Properties params) {
034          String enumClassName = params.getProperty("enumClassName");
035          if (enumClassName == null) {
036             throw new MappingException("enumClassName parameter not specified");
037          }
038          
039          try {
040                this.clazz = Class.forName(enumClassName);
041                return;
042          }catch(Exception e){}
043          try {
044                String superclassName = enumClassName.substring(0, enumClassName.lastIndexOf("."));
045                String subclassName = enumClassName.substring(enumClassName.lastIndexOf(".")+1, enumClassName.length());
046                Class superclass = Class.forName(superclassName);
047                Class[] decClasses = superclass.getDeclaredClasses();
048                for(Class c : decClasses)
049                    if(c.getName().equals(subclassName))
050                            this.clazz = c;
051          } catch (java.lang.ClassNotFoundException e) {
052             throw new MappingException("enumClass " + enumClassName + " not found", e);
053            }
054       }
055       
056        private static final int[] SQL_TYPES = {Types.VARCHAR};
057        public int[] sqlTypes() {
058            return SQL_TYPES;
059        }
060    
061        public Class returnedClass() {
062            return clazz;
063        }
064    
065        @SuppressWarnings("unchecked")
066            public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
067                                 throws HibernateException, SQLException {
068            String name = resultSet.getString(names[0]);
069            Object result = null;
070            if (!resultSet.wasNull()) {
071                result = Enum.valueOf(clazz, name);
072            }
073            return result;
074        }
075    
076       public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) 
077                              throws HibernateException, SQLException {
078            if (null == value) {
079                preparedStatement.setNull(index, Types.VARCHAR);
080            } else {
081                preparedStatement.setString(index, ( (Enum) value ).name() );
082            }
083        }
084    
085        public Object deepCopy(Object value) throws HibernateException{
086            return value;
087        }
088    
089        public boolean isMutable() {
090            return false;
091        }
092    
093        public Object assemble(Serializable cached, Object owner) throws HibernateException {
094            return cached;
095        }
096    
097        public Serializable disassemble(Object value) throws HibernateException {
098            return (Serializable)value;
099        }
100    
101        public Object replace(Object original, Object target, Object owner) throws HibernateException {
102            return original;
103        }
104        public int hashCode(Object x) throws HibernateException {
105            return x.hashCode();
106        }
107        public boolean equals(Object x, Object y) throws HibernateException {
108            if (x == y)
109                return true;
110            if (null == x || null == y)
111                return false;
112            return x.equals(y);
113        }
114    }