1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web;
24
25 import java.io.Serializable;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.Types;
30
31 import org.hibernate.Hibernate;
32 import org.hibernate.HibernateException;
33 import org.hibernate.usertype.UserType;
34
35
36
37
38
39
40
41
42
43
44 public abstract class StringEnumeration implements Serializable, UserType, Comparable {
45
46 private final static long serialVersionUID = -1298346029203487615L;
47
48
49
50
51 private final static int[] TYPES = new int[] {Types.VARCHAR};
52
53
54
55
56
57
58 protected String code;
59
60 private String display;
61
62
63
64
65
66 protected StringEnumeration() {
67 super();
68 }
69
70
71
72
73
74
75
76
77 protected StringEnumeration(String code, String display) {
78 this.code = code;
79 this.display = display;
80 }
81
82
83
84
85
86
87
88 public String getCode() {
89 return code;
90 }
91
92
93
94
95
96
97 public String getDisplay() {
98 return display;
99 }
100
101
102
103
104
105
106
107
108
109 public abstract StringEnumeration decode(String code);
110
111
112
113
114 public boolean equals(Object other) {
115 if (other != null && this.getClass().equals(other.getClass())) {
116 if (((StringEnumeration)other).code.equals(code)) {
117 return true;
118 }
119 }
120 return false;
121 }
122
123
124
125
126 public int hashCode() {
127 return code.hashCode();
128 }
129
130
131
132
133 public int[] sqlTypes() {
134 return TYPES;
135 }
136
137
138
139
140 public Class returnedClass() {
141 return this.getClass();
142 }
143
144
145
146
147
148 public boolean equals(Object src, Object dst) throws HibernateException {
149 if (src == dst) {
150 return true;
151 }
152 if (src == null || dst == null) {
153 return false;
154 }
155 return Hibernate.STRING.isEqual(src, dst);
156 }
157
158
159
160
161
162 public Object nullSafeGet(ResultSet rows, String[] names, Object owner) throws HibernateException, SQLException {
163 String code = ((String)Hibernate.STRING.nullSafeGet(rows, names[0]));
164 return this.decode(code);
165 }
166
167
168
169
170
171 public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
172 SQLException {
173 if (value == null) {
174 statement.setNull(index, TYPES[0]);
175 } else {
176 try {
177 statement.setString(index, ((StringEnumeration)value).getCode());
178 } catch (ClassCastException cce) {
179 throw new HibernateException(cce);
180 }
181 }
182 }
183
184
185
186
187 public Object deepCopy(Object value) throws HibernateException {
188 return value;
189 }
190
191
192
193
194 public boolean isMutable() {
195 return false;
196 }
197
198
199
200
201
202 public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
203
204 return null;
205 }
206
207
208
209
210
211 public Serializable disassemble(Object arg0) throws HibernateException {
212
213 return null;
214 }
215
216
217
218
219 public int hashCode(Object obj) throws HibernateException {
220 return ((Enumeration)obj).hashCode();
221 }
222
223
224
225
226
227 public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
228 return null;
229 }
230
231
232
233
234 public int compareTo(Object obj) {
235 try {
236 return this.getCode().compareTo(((StringEnumeration)obj).code);
237 } catch (ClassCastException cce) {
238 return 0;
239 }
240 }
241 }