1 // license-header java merge-point
2 //
3 // Attention: Generated code! Do not modify by hand!
4 // Generated by: hibernate/usertypes/HibernateStringClobType.vsl in andromda-hibernate-cartridge.
5 //
6 package org.andromda.persistence.hibernate.usertypes;
7
8 /*
9 * #%L
10 * SIH-Adagio :: Core for Allegro
11 * $Id:$
12 * $HeadURL:$
13 * %%
14 * Copyright (C) 2012 - 2014 Ifremer
15 * %%
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * #L%
29 */
30
31 import java.io.IOException;
32 import java.io.Reader;
33 import java.io.Serializable;
34 import java.io.StringReader;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.sql.Types;
39 import org.hibernate.HibernateException;
40 import org.hibernate.engine.spi.SessionImplementor;
41 import org.hibernate.usertype.UserType;
42
43 /**
44 * <p>
45 * A hibernate user type which converts a Clob into a String and back again.
46 * </p>
47 */
48 public class HibernateStringClobType
49 implements UserType, Serializable
50 {
51 /**
52 * The serial version UID of this class. Needed for serialization.
53 */
54 private static final long serialVersionUID = -9186242984664002079L;
55
56 /**
57 * @see org.hibernate.usertype.UserType#sqlTypes()
58 */
59 @Override
60 public int[] sqlTypes()
61 {
62 return new int[] {Types.CLOB};
63 }
64
65 /**
66 * @see org.hibernate.usertype.UserType#returnedClass()
67 */
68 @Override
69 public Class<?> returnedClass()
70 {
71 return String.class;
72 }
73
74 /**
75 * @see org.hibernate.usertype.UserType#equals(Object, Object)
76 */
77 @Override
78 public boolean equals(
79 Object x,
80 Object y)
81 throws HibernateException
82 {
83 boolean equal = false;
84 if (x == y)
85 {
86 equal = true;
87 }
88 else if (x == null || y == null)
89 {
90 equal = false;
91 }
92 else if (!(x instanceof String) || !(y instanceof String))
93 {
94 equal = false;
95 }
96 else
97 {
98 equal = ((String)x).equals(y);
99 }
100 return equal;
101 }
102
103 /**
104 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
105 */
106 public Object nullSafeGet(
107 ResultSet resultSet,
108 String[] names,
109 Object owner)
110 throws HibernateException, SQLException
111 {
112 final StringBuilder buffer = new StringBuilder();
113 try
114 {
115 //First we get the stream
116 Reader inputStream = resultSet.getCharacterStream(names[0]);
117 if (inputStream == null)
118 {
119 return null;
120 }
121 char[] buf = new char[1024];
122 int read = -1;
123
124 while ((read = inputStream.read(buf)) > 0)
125 {
126 buffer.append(new String(
127 buf,
128 0,
129 read));
130 }
131 inputStream.close();
132 }
133 catch (IOException exception)
134 {
135 throw new HibernateException("Unable to read from resultset", exception);
136 }
137 return buffer.toString();
138 }
139
140 /**
141 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
142 */
143 public void nullSafeSet(
144 PreparedStatement preparedStatement,
145 Object data,
146 int index)
147 throws HibernateException, SQLException
148 {
149 if (data != null)
150 {
151 StringReader r = new StringReader((String)data);
152 preparedStatement.setCharacterStream(
153 index,
154 r,
155 ((String)data).length());
156 }
157 else
158 {
159 preparedStatement.setNull(
160 index,
161 sqlTypes()[0]);
162 }
163 }
164
165 /**
166 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
167 */
168 @Override
169 public Object nullSafeGet(ResultSet resultSet, String[] names,
170 SessionImplementor session, Object owner) throws HibernateException, SQLException
171 {
172 return this.nullSafeGet(resultSet, names, owner);
173 }
174
175 /**
176 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
177 */
178 @Override
179 public void nullSafeSet(
180 PreparedStatement preparedStatement,
181 Object data,
182 int index,
183 SessionImplementor session)
184 throws HibernateException, SQLException
185 {
186 this.nullSafeSet(preparedStatement, data, index);
187 }
188
189 /**
190 * @see org.hibernate.usertype.UserType#deepCopy(Object)
191 */
192 @Override
193 public Object deepCopy(Object value)
194 throws HibernateException
195 {
196 return value;
197 }
198
199 /**
200 * @see org.hibernate.usertype.UserType#isMutable()
201 */
202 @Override
203 public boolean isMutable()
204 {
205 return false;
206 }
207
208 /**
209 * @see org.hibernate.usertype.UserType#replace(Object original, Object target, Object owner)
210 */
211 @Override
212 public Object replace(Object original, Object target, Object owner)
213 {
214 return this.deepCopy(original);
215 }
216
217 /**
218 * @see org.hibernate.usertype.UserType#assemble(Serializable cached, Object owner)
219 */
220 @Override
221 public Object assemble(Serializable cached, Object owner)
222 {
223 return this.deepCopy(cached);
224 }
225
226 /**
227 * @see org.hibernate.usertype.UserType#disassemble(Object value)
228 */
229 @Override
230 public Serializable disassemble(Object value)
231 {
232 return (Serializable)value;
233 }
234
235 /**
236 * @param x
237 * @return x.hashCode()
238 * @see Object#hashCode()
239 */
240 @Override
241 public int hashCode(Object x)
242 {
243 return x.hashCode();
244 }
245 }