001 /**
002 *
003 * $Revision: 13342 $ $Date: 2008-04-02 14:49:50 +0200 (Mi, 02 Apr 2008) $
004 *
005 * This file is part of ** M y C o R e **
006 * Visit our homepage at http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, normally in the file license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 *
023 **/
024 package org.mycore.datamodel.classifications2.impl;
025
026 import java.io.Serializable;
027 import java.net.URI;
028 import java.sql.PreparedStatement;
029 import java.sql.ResultSet;
030 import java.sql.SQLException;
031 import java.sql.Types;
032
033 import org.hibernate.HibernateException;
034 import org.hibernate.usertype.UserType;
035
036 /**
037 * @author Thomas Scheffler (yagee)
038 *
039 * This class maps a java.net.URI to a SQL VARCHAR.
040 *
041 * @see URI
042 * @see Types#VARCHAR
043 */
044 public class MCRURIType implements UserType {
045
046 private static final int[] SQL_TYPES = { Types.VARCHAR };
047
048 /*
049 * (non-Javadoc)
050 *
051 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
052 * java.lang.Object)
053 */
054 public Object assemble(Serializable cached, Object owner) throws HibernateException {
055 return cached;
056 }
057
058 /*
059 * (non-Javadoc)
060 *
061 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
062 */
063 public Object deepCopy(Object value) throws HibernateException {
064 // arg0 is not mutable
065 return value;
066 }
067
068 /*
069 * (non-Javadoc)
070 *
071 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
072 */
073 public Serializable disassemble(Object value) throws HibernateException {
074 return (Serializable) value;
075 }
076
077 /*
078 * (non-Javadoc)
079 *
080 * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
081 * java.lang.Object)
082 */
083 public boolean equals(Object x, Object y) throws HibernateException {
084 if (x == y) {
085 return true;
086 } else if (x != null) {
087 return x.equals(y);
088 }
089 return false;
090 }
091
092 /*
093 * (non-Javadoc)
094 *
095 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
096 */
097 public int hashCode(Object x) throws HibernateException {
098 if (x == null) {
099 return 0;
100 }
101 return x.hashCode();
102 }
103
104 /*
105 * (non-Javadoc)
106 *
107 * @see org.hibernate.usertype.UserType#isMutable()
108 */
109 public boolean isMutable() {
110 return false;
111 }
112
113 /*
114 * (non-Javadoc)
115 *
116 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
117 * java.lang.String[], java.lang.Object)
118 */
119 public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
120 String name = rs.getString(names[0]);
121 return rs.wasNull() ? null : URI.create(name);
122 }
123
124 /*
125 * (non-Javadoc)
126 *
127 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
128 * java.lang.Object, int)
129 */
130 public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
131 if (value == null) {
132 st.setNull(index, Types.VARCHAR);
133 } else {
134 st.setString(index, value.toString());
135 }
136 }
137
138 /*
139 * (non-Javadoc)
140 *
141 * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
142 * java.lang.Object, java.lang.Object)
143 */
144 public Object replace(Object original, Object target, Object owner) throws HibernateException {
145 return original;
146 }
147
148 /*
149 * (non-Javadoc)
150 *
151 * @see org.hibernate.usertype.UserType#returnedClass()
152 */
153 public Class<URI> returnedClass() {
154 return URI.class;
155 }
156
157 /*
158 * (non-Javadoc)
159 *
160 * @see org.hibernate.usertype.UserType#sqlTypes()
161 */
162 public int[] sqlTypes() {
163 return SQL_TYPES;
164 }
165
166 }