1 package org.hibernate.type; 3 4 import java.sql.Date ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.sql.Types ; 9 import java.util.Calendar ; 10 import java.util.GregorianCalendar ; 11 12 import org.hibernate.EntityMode; 13 import org.hibernate.Hibernate; 14 import org.hibernate.HibernateException; 15 import org.hibernate.util.CalendarComparator; 16 17 22 public class CalendarDateType extends MutableType { 23 24 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 25 26 Date date = rs.getDate(name); 27 if (date!=null) { 28 Calendar cal = new GregorianCalendar (); 29 cal.setTime(date); 30 return cal; 31 } 32 else { 33 return null; 34 } 35 36 } 37 38 public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 39 final Calendar cal = (Calendar ) value; 40 st.setDate( index, new Date ( cal.getTime().getTime() ), cal ); 42 } 43 44 public int sqlType() { 45 return Types.DATE; 46 } 47 48 public String toString(Object value) throws HibernateException { 49 return Hibernate.DATE.toString( ( (Calendar ) value ).getTime() ); 50 } 51 52 public Object fromStringValue(String xml) throws HibernateException { 53 Calendar result = new GregorianCalendar (); 54 result.setTime( ( (Date ) Hibernate.DATE.fromStringValue(xml) ) ); 55 return result; 56 } 57 58 public Object deepCopyNotNull(Object value) { 59 return ( (Calendar ) value ).clone(); 60 } 61 62 public Class getReturnedClass() { 63 return Calendar .class; 64 } 65 66 public int compare(Object x, Object y, EntityMode entityMode) { 67 return CalendarComparator.INSTANCE.compare(x, y); 68 } 69 70 public boolean isEqual(Object x, Object y) { 71 if (x==y) return true; 72 if (x==null || y==null) return false; 73 74 Calendar calendar1 = (Calendar ) x; 75 Calendar calendar2 = (Calendar ) y; 76 77 return calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH) 78 && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) 79 && calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); 80 } 81 82 public int getHashCode(Object x, EntityMode entityMode) { 83 Calendar calendar = (Calendar ) x; 84 int hashCode = 1; 85 hashCode = 31 * hashCode + calendar.get(Calendar.DAY_OF_MONTH); 86 hashCode = 31 * hashCode + calendar.get(Calendar.MONTH); 87 hashCode = 31 * hashCode + calendar.get(Calendar.YEAR); 88 return hashCode; 89 } 90 91 public String getName() { 92 return "calendar_date"; 93 } 94 95 } 96 | Popular Tags |