View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.hibernate.type;
24  
25  import java.io.Serializable;
26  import java.sql.PreparedStatement;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.sql.Types;
30  
31  import org.hibernate.HibernateException;
32  import org.hibernate.usertype.UserType;
33  
34  /**
35   * Hibernatetype to map postgresql ts_vector to String
36   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
37   *
38   */
39  public class TsVectorStringType implements UserType{
40  	
41  
42  
43  	/* (non-Javadoc)
44  	 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
45  	 */
46  	public Object assemble(Serializable cached, Object owner) throws HibernateException {
47  		return null;
48  	}
49  
50  	/* (non-Javadoc)
51  	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
52  	 */
53  	public Object deepCopy(Object value) throws HibernateException {
54  		if (value == null) {
55  		    return null;
56  		}
57  		return new String((String) value);
58  	}
59  
60  	/* (non-Javadoc)
61  	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
62  	 */
63  	public Serializable disassemble(Object o) throws HibernateException {
64  		return o == null? null: o.toString();
65  	}
66  
67  	/* (non-Javadoc)
68  	 * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
69  	 */
70  	public boolean equals(Object x, Object y) throws HibernateException {
71  		return (x == y) || ((x != null) && (y != null) && (x.equals(y)));
72  	}
73  
74  	/* (non-Javadoc)
75  	 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
76  	 */
77  	public int hashCode(Object obj) throws HibernateException {
78  		return obj.hashCode();
79  
80  	}
81  
82  	/* (non-Javadoc)
83  	 * @see org.hibernate.usertype.UserType#isMutable()
84  	 */
85  	public boolean isMutable() {
86  		return false;
87  	}
88  
89  	/* (non-Javadoc)
90  	 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
91  	 */
92  	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
93  		//Object value = rs.getObject(names[0]);
94  		//the code bellow doens't work because there is no jdbc extension for tsvector object
95  		//return value== null? null: value.toString();
96  		return null;
97  
98  	}
99  
100 	/* (non-Javadoc)
101 	 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
102 	 */
103 	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
104 		if (value != null) {
105 				if (!(value instanceof String)){
106 					throw new HibernateException("object must be a string to be converted but was "+value.getClass().getName());
107 				}
108 		   //the code bellow doens't work because there is no jdbc extension for tsvector object
109 		  //st.setObject(index,"(select to_tsvector('"+((String) value)+"'))",25);
110 		    st.setNull(index, sqlTypes()[0]);
111 		} else {
112 		    st.setNull(index, sqlTypes()[0]);
113 		}
114 
115 		
116 	}
117 
118 	/* (non-Javadoc)
119 	 * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
120 	 */
121 	public Object replace(Object original, Object target, Object owner) throws HibernateException {
122 		return null;
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see org.hibernate.usertype.UserType#returnedClass()
127 	 */
128 	@SuppressWarnings("unchecked")
129 	public Class returnedClass() {
130 		return String.class;
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see org.hibernate.usertype.UserType#sqlTypes()
135 	 */
136 	public int[] sqlTypes() {
137 		return new int[] { Types.STRUCT };
138 	}
139 
140 }