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