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