1 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 ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.Types ; 13 import java.util.Properties ; 14 15 20 public class CasterStringType implements UserType, ParameterizedType { 21 private static final String CAST = "cast"; 22 private Properties parameters; 23 24 public int[] sqlTypes() { 25 return new int[] { Types.VARCHAR }; 26 } 27 28 public Class returnedClass() { 29 return String .class; 30 } 31 32 public boolean equals(Object x, Object y) throws HibernateException { 33 return (x == y) || (x != null && x.equals(y) ); 34 } 35 36 public int hashCode(Object x) throws HibernateException { 37 return x.hashCode(); 38 } 39 40 public Object nullSafeGet(ResultSet rs, String [] names, Object owner) throws HibernateException, SQLException { 41 String 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 st, Object value, int index) throws HibernateException, SQLException { 51 if (value == null) { 52 st.setNull( index, sqlTypes()[0] ); 53 } 54 else { 55 String string = (String ) 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 deepCopy(Object value) throws HibernateException { 67 return value; 68 } 69 70 public boolean isMutable() { 71 return false; 72 } 73 74 public Serializable disassemble(Object value) throws HibernateException { 75 return (Serializable ) value; 76 } 77 78 public Object assemble(Serializable cached, Object owner) throws HibernateException { 79 return cached; 80 } 81 82 public Object replace(Object original, Object target, Object owner) throws HibernateException { 83 return original; 84 } 85 86 public void setParameterValues(Properties parameters) { 87 this.parameters = parameters; 88 } 89 } | Popular Tags |