KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > CalendarDateType


1 //$Id: CalendarDateType.java,v 1.11 2005/06/20 09:17:00 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.Date JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.util.Calendar JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
11
12 import org.hibernate.EntityMode;
13 import org.hibernate.Hibernate;
14 import org.hibernate.HibernateException;
15 import org.hibernate.util.CalendarComparator;
16
17 /**
18  * <tt>calendar_date</tt>: A type mapping for a <tt>Calendar</tt>
19  * object that represents a date.
20  * @author Gavin King
21  */

22 public class CalendarDateType extends MutableType {
23
24     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
25
26         Date JavaDoc date = rs.getDate(name);
27         if (date!=null) {
28             Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
29             cal.setTime(date);
30             return cal;
31         }
32         else {
33             return null;
34         }
35
36     }
37
38     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
39         final Calendar JavaDoc cal = (Calendar JavaDoc) value;
40         //st.setDate( index, new Date( cal.getTimeInMillis() ), cal ); //JDK 1.5 only
41
st.setDate( index, new Date JavaDoc( cal.getTime().getTime() ), cal );
42     }
43
44     public int sqlType() {
45         return Types.DATE;
46     }
47
48     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
49         return Hibernate.DATE.toString( ( (Calendar JavaDoc) value ).getTime() );
50     }
51
52     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
53         Calendar JavaDoc result = new GregorianCalendar JavaDoc();
54         result.setTime( ( (Date JavaDoc) Hibernate.DATE.fromStringValue(xml) ) );
55         return result;
56     }
57
58     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) {
59         return ( (Calendar JavaDoc) value ).clone();
60     }
61
62     public Class JavaDoc getReturnedClass() {
63         return Calendar JavaDoc.class;
64     }
65
66     public int compare(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
67         return CalendarComparator.INSTANCE.compare(x, y);
68     }
69
70     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
71         if (x==y) return true;
72         if (x==null || y==null) return false;
73
74         Calendar JavaDoc calendar1 = (Calendar JavaDoc) x;
75         Calendar JavaDoc calendar2 = (Calendar JavaDoc) 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 JavaDoc x, EntityMode entityMode) {
83         Calendar JavaDoc calendar = (Calendar JavaDoc) 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 JavaDoc getName() {
92         return "calendar_date";
93     }
94
95 }
96
Popular Tags