View Javadoc
1   // license-header java merge-point
2   //
3   // Attention: Generated code! Do not modify by hand!
4   // Generated by: hibernate3/usertypes/HibernateEnumType.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.Serializable;
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  import java.sql.Types;
33  import java.util.Properties;
34  import org.hibernate.HibernateException;
35  import org.hibernate.engine.spi.SessionImplementor;
36  import org.hibernate.usertype.EnhancedUserType;
37  import org.hibernate.usertype.ParameterizedType;
38  
39  /**
40   * A Hibernate UserType for Java5 enumerations. Taken from
41   * <a href="http://www.hibernate.org/272.html">Java 5 EnumUserType</a>.
42   */
43  public class HibernateEnumType implements EnhancedUserType, ParameterizedType
44  {
45      @SuppressWarnings("unchecked")
46      private Class<Enum> enumClass;
47  
48      /**
49       * @see org.hibernate.usertype.ParameterizedType#setParameterValues(java.util.Properties)
50       */
51      @SuppressWarnings("unchecked")
52      public void setParameterValues(Properties parameters)
53      {
54          final String enumClassName = parameters.getProperty("enumClassName");
55          try
56          {
57              //noinspection unchecked
58              this.enumClass = (Class<Enum>)Class.forName(enumClassName);
59          }
60          catch (ClassNotFoundException cnfe)
61          {
62              throw new HibernateException("Enum class not found", cnfe);
63          }
64      }
65  
66      /**
67       * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, Object)
68       */
69      public Object assemble(Serializable cached, Object owner) throws HibernateException
70      {
71          return cached;
72      }
73  
74      /**
75       * @see org.hibernate.usertype.UserType#deepCopy(Object)
76       */
77      public Object deepCopy(Object value) throws HibernateException
78      {
79          return value;
80      }
81  
82      /**
83       * @see org.hibernate.usertype.UserType#disassemble(Object)
84       */
85      @SuppressWarnings("unchecked")
86      public Serializable disassemble(Object value) throws HibernateException
87      {
88          return (Enum)value;
89      }
90  
91      /**
92       * @see org.hibernate.usertype.UserType#equals(Object, Object)
93       */
94      public boolean equals(Object x, Object y) throws HibernateException
95      {
96          return x == y;
97      }
98  
99      /**
100      * @see org.hibernate.usertype.UserType#hashCode(Object)
101      */
102     public int hashCode(Object x) throws HibernateException
103     {
104         return x.hashCode();
105     }
106 
107     /**
108      * @see org.hibernate.usertype.UserType#isMutable()
109      */
110     public boolean isMutable()
111     {
112         return false;
113     }
114 
115     /**
116      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
117      */
118     @SuppressWarnings("unchecked")
119     public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException
120     {
121         final String name = resultSet.getString(names[0]);
122         return resultSet.wasNull() ? null : Enum.valueOf(this.enumClass, name);
123     }
124 
125     /**
126      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
127      */
128     @SuppressWarnings("unchecked")
129     public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException
130     {
131         if (value == null)
132         {
133             statement.setNull(index, Types.VARCHAR);
134         }
135         else
136         {
137             if(value instanceof Enum)
138             {
139                 statement.setString(index, ((Enum)value).name());
140             }
141             else
142             {
143                 statement.setString(index, (String)value);
144             }
145         }
146     }
147 
148     /**
149      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
150      */
151     @Override
152     public Object nullSafeGet(ResultSet resultSet, String[] names,
153         SessionImplementor session, Object owner) throws HibernateException, SQLException
154     {
155         return this.nullSafeGet(resultSet, names, owner);
156     }
157 
158     /**
159      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
160      */
161     @Override
162     public void nullSafeSet(
163         PreparedStatement preparedStatement,
164         Object data,
165         int index,
166         SessionImplementor session)
167         throws HibernateException, SQLException
168     {
169         this.nullSafeSet(preparedStatement, data, index);
170     }
171 
172     /**
173      * @see org.hibernate.usertype.UserType#replace(Object, Object, Object)
174      */
175     public Object replace(Object original, Object target, Object owner) throws HibernateException
176     {
177         return original;
178     }
179 
180     /**
181      * @see org.hibernate.usertype.UserType#returnedClass()
182      */
183     @SuppressWarnings("unchecked")
184     public Class returnedClass()
185     {
186         return this.enumClass;
187     }
188 
189     /**
190      * @see org.hibernate.usertype.UserType#sqlTypes()
191      */
192     public int[] sqlTypes()
193     {
194         return new int[]{Types.VARCHAR};
195     }
196 
197     /**
198      * @see org.hibernate.usertype.EnhancedUserType#fromXMLString(String)
199      */
200     @SuppressWarnings("unchecked")
201     public Object fromXMLString(String xmlValue)
202     {
203         return Enum.valueOf(this.enumClass, xmlValue);
204     }
205 
206     /**
207      * @see org.hibernate.usertype.EnhancedUserType#objectToSQLString(Object)
208      */
209     @SuppressWarnings("unchecked")
210     public String objectToSQLString(Object value)
211     {
212         return '\'' + ((Enum)value).name() + '\'';
213     }
214 
215     /**
216      * @see org.hibernate.usertype.EnhancedUserType#toXMLString(Object)
217      */
218     @SuppressWarnings("unchecked")
219     public String toXMLString(Object value)
220     {
221         return ((Enum)value).name();
222     }
223 }