KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > entity > CasterStringType


1 //$Id: CasterStringType.java,v 1.1 2005/05/12 13:33:32 epbernard Exp $
2
package org.hibernate.test.annotations.entity;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.usertype.ParameterizedType;
6 import org.hibernate.usertype.UserType;
7
8 import java.io.Serializable JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.sql.Types JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 /**
16  * Sample of parameter type
17  *
18  * @author Emmanuel Bernard
19  */

20 public class CasterStringType implements UserType, ParameterizedType {
21     private static final String JavaDoc CAST = "cast";
22     private Properties JavaDoc parameters;
23
24     public int[] sqlTypes() {
25         return new int[] { Types.VARCHAR };
26     }
27
28     public Class JavaDoc returnedClass() {
29         return String JavaDoc.class;
30     }
31
32     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
33         return (x == y) || (x != null && x.equals(y) );
34     }
35
36     public int hashCode(Object JavaDoc x) throws HibernateException {
37         return x.hashCode();
38     }
39
40     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
41         String JavaDoc result = rs.getString( names[0] );
42         if ( rs.wasNull() ) return null;
43         if ( parameters.getProperty(CAST).equals("lower") ) {
44             return result.toLowerCase();
45         } else {
46             return result.toUpperCase();
47         }
48     }
49
50     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
51         if (value == null) {
52             st.setNull( index, sqlTypes()[0] );
53         }
54         else {
55             String JavaDoc string = (String JavaDoc) value;
56             if ( parameters.getProperty(CAST).equals("lower") ) {
57                 string = string.toLowerCase();
58             }
59             else {
60                 string = string.toUpperCase();
61             }
62             st.setString( index, string );
63         }
64     }
65
66     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
67         return value;
68     }
69
70     public boolean isMutable() {
71         return false;
72     }
73
74     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
75         return (Serializable JavaDoc) value;
76     }
77
78     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
79         return cached;
80     }
81
82     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
83         return original;
84     }
85
86     public void setParameterValues(Properties JavaDoc parameters) {
87         this.parameters = parameters;
88     }
89 }
Popular Tags