1
2
3
4
5
6 package org.andromda.persistence.hibernate.usertypes;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
41
42
43 public class HibernateEnumType implements EnhancedUserType, ParameterizedType
44 {
45 @SuppressWarnings("unchecked")
46 private Class<Enum> enumClass;
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public void setParameterValues(Properties parameters)
53 {
54 final String enumClassName = parameters.getProperty("enumClassName");
55 try
56 {
57
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
68
69 public Object assemble(Serializable cached, Object owner) throws HibernateException
70 {
71 return cached;
72 }
73
74
75
76
77 public Object deepCopy(Object value) throws HibernateException
78 {
79 return value;
80 }
81
82
83
84
85 @SuppressWarnings("unchecked")
86 public Serializable disassemble(Object value) throws HibernateException
87 {
88 return (Enum)value;
89 }
90
91
92
93
94 public boolean equals(Object x, Object y) throws HibernateException
95 {
96 return x == y;
97 }
98
99
100
101
102 public int hashCode(Object x) throws HibernateException
103 {
104 return x.hashCode();
105 }
106
107
108
109
110 public boolean isMutable()
111 {
112 return false;
113 }
114
115
116
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
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
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
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
174
175 public Object replace(Object original, Object target, Object owner) throws HibernateException
176 {
177 return original;
178 }
179
180
181
182
183 @SuppressWarnings("unchecked")
184 public Class returnedClass()
185 {
186 return this.enumClass;
187 }
188
189
190
191
192 public int[] sqlTypes()
193 {
194 return new int[]{Types.VARCHAR};
195 }
196
197
198
199
200 @SuppressWarnings("unchecked")
201 public Object fromXMLString(String xmlValue)
202 {
203 return Enum.valueOf(this.enumClass, xmlValue);
204 }
205
206
207
208
209 @SuppressWarnings("unchecked")
210 public String objectToSQLString(Object value)
211 {
212 return '\'' + ((Enum)value).name() + '\'';
213 }
214
215
216
217
218 @SuppressWarnings("unchecked")
219 public String toXMLString(Object value)
220 {
221 return ((Enum)value).name();
222 }
223 }