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