00001 package cedar.hepdata.db; 00002 00003 import cedar.hepdata.util.*; 00004 00005 import java.io.Serializable; 00006 import java.sql.PreparedStatement; 00007 import java.sql.ResultSet; 00008 import java.sql.SQLException; 00009 import java.sql.Types; 00010 00011 import org.hibernate.HibernateException; 00012 import org.hibernate.usertype.UserType; 00013 00014 import org.apache.log4j.*; 00015 00016 00017 public class UnitUserType implements UserType { 00018 static Logger log = Logger.getLogger(UnitUserType.class); 00019 00020 private static final int[] SQL_TYPES = {Types.VARCHAR}; 00021 00022 public int[] sqlTypes() { 00023 return SQL_TYPES; 00024 } 00025 00026 public Class returnedClass() { 00027 return Unit.class; 00028 } 00029 00030 public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) 00031 throws HibernateException, SQLException { 00032 String unitstr = resultSet.getString(names[0]); 00033 Unit result = new Unit(); 00034 log.debug("Unit string: " + unitstr); 00035 if (!resultSet.wasNull()) { 00036 try { 00037 result.fromString(unitstr); 00038 } catch (HDUnitException e) { 00039 log.error("Problem converting stored string to Unit: " + e); 00040 } 00041 } 00042 return result; 00043 } 00044 00045 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) 00046 throws HibernateException, SQLException { 00047 if (null == value) { 00048 preparedStatement.setNull(index, Types.VARCHAR); 00049 } else { 00050 Unit theunit = (Unit) value; 00051 preparedStatement.setString(index, theunit.toString()); 00052 } 00053 } 00054 00055 public Object deepCopy(Object value) throws HibernateException{ 00056 return value; 00057 } 00058 00059 public boolean isMutable() { 00060 return false; 00061 } 00062 00063 public Object assemble(Serializable cached, Object owner) throws HibernateException { 00064 return cached; 00065 } 00066 00067 public Serializable disassemble(Object value) throws HibernateException { 00068 return (Serializable)value; 00069 } 00070 00071 public Object replace(Object original, Object target, Object owner) throws HibernateException { 00072 return original; 00073 } 00074 00075 public int hashCode(Object x) throws HibernateException { 00076 return x.hashCode(); 00077 } 00078 00079 public boolean equals(Object x, Object y) throws HibernateException { 00080 if (x == y) 00081 return true; 00082 if (null == x || null == y) 00083 return false; 00084 return x.equals(y); 00085 } 00086 }