View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
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   * This class allows to use string to define enumeration elements identifier but
37   * needs the definition of two constructors. Implementors of this class need to
38   * define two constructors: one without parameters which simply calls
39   * <code>super()</code> and an additional one taking two strings and calling
40   * <code>super(String, String)</code>.
41   * 
42   * @author rlogiacco
43   */
44  public abstract class StringEnumeration implements Serializable, UserType, Comparable {
45  
46  	private final static long serialVersionUID = -1298346029203487615L;
47  
48  	/**
49  	 * The SQL column type.
50  	 */
51  	private final static int[] TYPES = new int[] {Types.VARCHAR};
52  
53  	/**
54  	 * TODO documentation
55  	 * 
56  	 * @uml.property name="code"
57  	 */
58  	protected String code;
59  
60  	private String display;
61  
62  
63  	/**
64  	 * Default constructor used for internal purposes only.
65  	 */
66  	protected StringEnumeration() {
67  		super();
68  	}
69  
70  	/**
71  	 * Constructs an available choice on a unique identifier and a brief, human
72  	 * understandable, description.
73  	 * 
74  	 * @param id the unique identifier for this choice.
75  	 * @param display a short, human understandable, description of the choice.
76  	 */
77  	protected StringEnumeration(String code, String display) {
78  		this.code = code;
79  		this.display = display;
80  	}
81  
82  	/**
83  	 * Returns the enumeration element code.
84  	 * 
85  	 * @return the enumeration element code.
86  	 * @uml.property name="code"
87  	 */
88  	public String getCode() {
89  		return code;
90  	}
91  
92  	/**
93  	 * Returns the enumeration element display name.
94  	 * 
95  	 * @return the enumeration element display name.
96  	 */
97  	public String getDisplay() {
98  		return display;
99  	}
100 
101 	/**
102 	 * Decodes a unique code into an instance of this class. This method <b>can
103 	 * not</b> return <code>null</code> values, instead it should define a
104 	 * <i>default</i> value.
105 	 * 
106 	 * @param code the code to decode.
107 	 * @return an instance of this class.
108 	 */
109 	public abstract StringEnumeration decode(String code);
110 
111 	/**
112 	 * @see java.lang.Object#equals(java.lang.Object)
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 	 * @see java.lang.Object#hashCode()
125 	 */
126 	public int hashCode() {
127 		return code.hashCode();
128 	}
129 	
130 	/**
131 	 * @see org.hibernate.usertype.UserType#sqlTypes()
132 	 */
133 	public int[] sqlTypes() {
134 		return TYPES;
135 	}
136 
137 	/**
138 	 * @see org.hibernate.usertype.UserType#returnedClass()
139 	 */
140 	public Class returnedClass() {
141 		return this.getClass();
142 	}
143 
144 	/**
145 	 * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
146 	 *      java.lang.Object)
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 	 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
160 	 *      java.lang.String[], java.lang.Object)
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 	 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
169 	 *      java.lang.Object, int)
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 	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
186 	 */
187 	public Object deepCopy(Object value) throws HibernateException {
188 		return value;
189 	}
190 
191 	/**
192 	 * @see org.hibernate.usertype.UserType#isMutable()
193 	 */
194 	public boolean isMutable() {
195 		return false;
196 	}
197 
198 	/**
199 	 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
200 	 *      java.lang.Object) TODO implement
201 	 */
202 	public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
203 		// TODO Auto-generated method stub
204 		return null;
205 	}
206 
207 	/**
208 	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object) TODO
209 	 *      implement
210 	 */
211 	public Serializable disassemble(Object arg0) throws HibernateException {
212 		// TODO Auto-generated method stub
213 		return null;
214 	}
215 
216 	/**
217 	 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
218 	 */
219 	public int hashCode(Object obj) throws HibernateException {
220 		return ((Enumeration)obj).hashCode();
221 	}
222 
223 	/**
224 	 * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
225 	 *      java.lang.Object, java.lang.Object) TODO implement
226 	 */
227 	public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
228 		return null;
229 	}
230 
231 	/**
232 	 * @see java.lang.Comparable#compareTo(T)
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 }