001/** 002 * GenericUserType.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 * 11/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 java.util.Properties; 017 018import org.hibernate.HibernateException; 019import org.hibernate.MappingException; 020import org.hibernate.engine.spi.SharedSessionContractImplementor; 021import org.hibernate.type.StandardBasicTypes; 022import org.hibernate.usertype.ParameterizedType; 023import org.hibernate.usertype.UserType; 024 025import es.ucm.fdi.gaia.jcolibri.connector.TypeAdaptor; 026 027/** 028 * Class that allows to use any jcolibri.connector.TypeAdaptor class in the DataBaseConnector.<p> 029 * This is a Hibernate subclass that wraps any user defined data type. This way it can be persisted into 030 * the data base easly.<p> 031 * @author Juan A. Recio-Garc�a 032 * @version 2.0 033 * @see TypeAdaptor 034 */ 035@SuppressWarnings("rawtypes") 036public class GenericUserType implements UserType, ParameterizedType { 037 038 private Class clazz = null; 039 040 public void setParameterValues(Properties params) { 041 042 String className = params.getProperty("className"); 043 if (className == null) { 044 throw new MappingException("className parameter not specified"); 045 } 046 try { 047 this.clazz = Class.forName(className); 048 }catch(Exception e) 049 { 050 throw new MappingException("Class " + className + " not found", e); 051 } 052 053 for(Class c: clazz.getInterfaces()) 054 if (c.equals(TypeAdaptor.class)) 055 return; 056 throw new MappingException("Class "+ className + " does not implements jcolibri.connector.TypeAdaptor. " + 057 "It is a requirement for using the genericUserType"); 058 } 059 060 061 public Object assemble(Serializable arg0, Object arg1) 062 throws HibernateException { 063 try { 064 String s = (String)arg0; 065 TypeAdaptor ta = (TypeAdaptor)arg1; 066 ta.fromString(s); 067 return ta; 068 } catch (Exception e) { 069 throw new HibernateException(e); 070 } 071 } 072 073 public Serializable disassemble(Object arg0) throws HibernateException { 074 TypeAdaptor ta = (TypeAdaptor)arg0; 075 return ta.toString(); 076 } 077 078 079 public Object deepCopy(Object arg0) throws HibernateException { 080 try { 081 TypeAdaptor ta = (TypeAdaptor) arg0; 082 String s = ta.toString(); 083 084 TypeAdaptor copy = (TypeAdaptor) arg0.getClass().newInstance(); 085 copy.fromString(s); 086 return copy; 087 } catch (Exception e) { 088 throw new HibernateException(e); 089 } 090 091 } 092 093 094 /* (non-Javadoc) 095 * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object) 096 */ 097 public boolean equals(Object arg0, Object arg1) throws HibernateException { 098 return arg0.equals(arg1); 099 } 100 101 /* (non-Javadoc) 102 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object) 103 */ 104 public int hashCode(Object arg0) throws HibernateException { 105 return arg0.hashCode(); 106 } 107 108 @Override 109 public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException { 110 try { 111 // TODO - Revisar!! 112 String val = (String)StandardBasicTypes.STRING.nullSafeGet(resultSet, strings[0], sharedSessionContractImplementor); 113 TypeAdaptor ta = (TypeAdaptor)clazz.newInstance(); 114 ta.fromString(val); 115 return ta; 116 117 } catch (Exception e) { 118 throw new HibernateException(e); 119 } 120 } 121 122 @Override 123 public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException { 124 125 } 126 127 /* (non-Javadoc) 128 * @see org.hibernate.usertype.UserType#isMutable() 129 */ 130 public boolean isMutable() { 131 return false; 132 } 133 134 /* (non-Javadoc) 135 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object) 136 */ 137 public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2) 138 throws HibernateException, SQLException { 139 try { 140 // TODO - Revisar!! 141 //String val = (String)StandardBasicTypes.STRING.nullSafeGet(arg0, arg1[0]); 142 TypeAdaptor ta = (TypeAdaptor)clazz.newInstance(); 143 ta.fromString(""); 144 return ta; 145 146 } catch (Exception e) { 147 throw new HibernateException(e); 148 } 149 } 150 151 /* (non-Javadoc) 152 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int) 153 */ 154 public void nullSafeSet(PreparedStatement arg0, Object arg1, int i) 155 throws HibernateException, SQLException { 156 TypeAdaptor ta = (TypeAdaptor)arg1; 157 String val = ta.toString(); 158 arg0.setString(i, val); 159 160 } 161 162 /* (non-Javadoc) 163 * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object) 164 */ 165 public Object replace(Object arg0, Object arg1, Object arg2) 166 throws HibernateException { 167 return arg0; 168 } 169 170 /* (non-Javadoc) 171 * @see org.hibernate.usertype.UserType#returnedClass() 172 */ 173 public Class returnedClass() { 174 // TODO Auto-generated method stub 175 return this.clazz; 176 } 177 178 /* (non-Javadoc) 179 * @see org.hibernate.usertype.UserType#sqlTypes() 180 */ 181 public int[] sqlTypes() { 182 return new int[] {Types.VARCHAR}; 183 } 184 185 186}