1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.Arrays ; 9 import java.util.Comparator ; 10 import java.util.Map ; 11 import java.util.Properties ; 12 13 import org.dom4j.Node; 14 import org.hibernate.EntityMode; 15 import org.hibernate.HibernateException; 16 import org.hibernate.MappingException; 17 import org.hibernate.engine.Mapping; 18 import org.hibernate.engine.SessionFactoryImplementor; 19 import org.hibernate.engine.SessionImplementor; 20 import org.hibernate.usertype.EnhancedUserType; 21 import org.hibernate.usertype.UserType; 22 import org.hibernate.usertype.UserVersionType; 23 24 30 public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType { 31 32 private final UserType userType; 33 private final String name; 34 private final int[] types; 35 36 public CustomType(Class userTypeClass, Properties parameters) throws MappingException { 37 38 name = userTypeClass.getName(); 39 40 if ( !UserType.class.isAssignableFrom(userTypeClass) ) { 41 throw new MappingException( 42 "Custom type does not implement UserType: " + 43 userTypeClass.getName() 44 ); 45 } 46 47 try { 48 userType = (UserType) userTypeClass.newInstance(); 49 } 50 catch (InstantiationException ie) { 51 throw new MappingException( 52 "Cannot instantiate custom type: " + 53 userTypeClass.getName() 54 ); 55 } 56 catch (IllegalAccessException iae) { 57 throw new MappingException( 58 "IllegalAccessException trying to instantiate custom type: " + 59 userTypeClass.getName() 60 ); 61 } 62 63 66 TypeFactory.injectParameters(userType, parameters); 67 types = userType.sqlTypes(); 68 69 } 70 71 public int[] sqlTypes(Mapping pi) { 72 return types; 73 } 74 75 public int getColumnSpan(Mapping session) { 76 return types.length; 77 } 78 79 public Class getReturnedClass() { 80 return userType.returnedClass(); 81 } 82 83 public boolean isEqual(Object x, Object y) throws HibernateException { 84 return userType.equals(x, y); 85 } 86 87 public boolean isEqual(Object x, Object y, EntityMode entityMode) 88 throws HibernateException { 89 return isEqual(x, y); 90 } 91 92 public int getHashCode(Object x, EntityMode entityMode) { 93 return userType.hashCode(x); 94 } 95 96 public Object nullSafeGet( 97 ResultSet rs, 98 String [] names, 99 SessionImplementor session, 100 Object owner 101 ) throws HibernateException, SQLException { 102 103 return userType.nullSafeGet(rs, names, owner); 104 } 105 106 public Object nullSafeGet( 107 ResultSet rs, 108 String columnName, 109 SessionImplementor session, 110 Object owner 111 ) throws HibernateException, SQLException { 112 return nullSafeGet(rs, new String [] { columnName }, session, owner); 113 } 114 115 116 public Object assemble(Serializable cached, SessionImplementor session, Object owner) 117 throws HibernateException { 118 return userType.assemble(cached, owner); 119 } 120 121 public Serializable disassemble(Object value, SessionImplementor session, Object owner) 122 throws HibernateException { 123 return userType.disassemble(value); 124 } 125 126 public Object replace( 127 Object original, 128 Object target, 129 SessionImplementor session, 130 Object owner, 131 Map copyCache) 132 throws HibernateException { 133 return userType.replace(original, target, owner); 134 } 135 136 public void nullSafeSet( 137 PreparedStatement st, 138 Object value, 139 int index, 140 boolean[] settable, 141 SessionImplementor session 142 ) throws HibernateException, SQLException { 143 144 if ( settable[0] ) userType.nullSafeSet(st, value, index); 145 } 146 147 public void nullSafeSet( 148 PreparedStatement st, 149 Object value, 150 int index, 151 SessionImplementor session 152 ) throws HibernateException, SQLException { 153 154 userType.nullSafeSet(st, value, index); 155 } 156 157 public String toXMLString(Object value, SessionFactoryImplementor factory) { 158 if (value==null) return null; 159 if (userType instanceof EnhancedUserType) { 160 return ( (EnhancedUserType) userType ).toXMLString(value); 161 } 162 else { 163 return value.toString(); 164 } 165 } 166 167 public Object fromXMLString(String xml, Mapping factory) { 168 return ( (EnhancedUserType) userType ).fromXMLString(xml); 169 } 170 171 public String getName() { 172 return name; 173 } 174 175 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) 176 throws HibernateException { 177 return userType.deepCopy(value); 178 } 179 180 public boolean isMutable() { 181 return userType.isMutable(); 182 } 183 184 public Object stringToObject(String xml) { 185 return ( (EnhancedUserType) userType ).fromXMLString(xml); 186 } 187 188 public String objectToSQLString(Object value) throws Exception { 189 return ( (EnhancedUserType) userType ).objectToSQLString(value); 190 } 191 192 public Comparator getComparator() { 193 return (Comparator ) userType; 194 } 195 196 public Object next(Object current) { 197 return ( (UserVersionType) userType ).next(current); 198 } 199 200 public Object seed() { 201 return ( (UserVersionType) userType ).seed(); 202 } 203 204 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 205 return fromXMLString( xml.getText(), factory ); 206 } 207 208 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) 209 throws HibernateException { 210 node.setText( toXMLString(value, factory) ); 211 } 212 213 public String toLoggableString(Object value, SessionFactoryImplementor factory) 214 throws HibernateException { 215 return value==null ? "null" : toXMLString(value, factory); 216 } 217 218 public boolean[] toColumnNullness(Object value, Mapping mapping) { 219 boolean[] result = new boolean[ getColumnSpan(mapping) ]; 220 if (value!=null) Arrays.fill(result, true); 221 return result; 222 } 223 224 } 225 | Popular Tags |