001: //$Id: CalendarType.java 7736 2005-08-03 20:03:34Z steveebersole $
002: package org.hibernate.type;
003:
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.sql.Timestamp;
008: import java.sql.Types;
009: import java.util.Calendar;
010: import java.util.Comparator;
011: import java.util.Date;
012: import java.util.GregorianCalendar;
013:
014: import org.hibernate.EntityMode;
015: import org.hibernate.Hibernate;
016: import org.hibernate.HibernateException;
017: import org.hibernate.engine.SessionImplementor;
018: import org.hibernate.cfg.Environment;
019: import org.hibernate.util.CalendarComparator;
020:
021: /**
022: * <tt>calendar</tt>: A type mapping for a <tt>Calendar</tt> object that
023: * represents a datetime.
024: * @author Gavin King
025: */
026: public class CalendarType extends MutableType implements VersionType {
027:
028: public Object get(ResultSet rs, String name)
029: throws HibernateException, SQLException {
030:
031: Timestamp ts = rs.getTimestamp(name);
032: if (ts != null) {
033: Calendar cal = new GregorianCalendar();
034: if (Environment.jvmHasTimestampBug()) {
035: cal.setTime(new Date(ts.getTime() + ts.getNanos()
036: / 1000000));
037: } else {
038: cal.setTime(ts);
039: }
040: return cal;
041: } else {
042: return null;
043: }
044:
045: }
046:
047: public void set(PreparedStatement st, Object value, int index)
048: throws HibernateException, SQLException {
049: final Calendar cal = (Calendar) value;
050: //st.setTimestamp( index, new Timestamp( cal.getTimeInMillis() ), cal ); //JDK 1.5 only
051: st.setTimestamp(index, new Timestamp(cal.getTime().getTime()),
052: cal);
053: }
054:
055: public int sqlType() {
056: return Types.TIMESTAMP;
057: }
058:
059: public String toString(Object value) throws HibernateException {
060: return Hibernate.TIMESTAMP.toString(((Calendar) value)
061: .getTime());
062: }
063:
064: public Object fromStringValue(String xml) throws HibernateException {
065: Calendar result = new GregorianCalendar();
066: result
067: .setTime(((Date) Hibernate.TIMESTAMP
068: .fromStringValue(xml)));
069: return result;
070: }
071:
072: public Object deepCopyNotNull(Object value)
073: throws HibernateException {
074: return ((Calendar) value).clone();
075: }
076:
077: public Class getReturnedClass() {
078: return Calendar.class;
079: }
080:
081: public int compare(Object x, Object y, EntityMode entityMode) {
082: return CalendarComparator.INSTANCE.compare(x, y);
083: }
084:
085: public boolean isEqual(Object x, Object y) {
086: if (x == y)
087: return true;
088: if (x == null || y == null)
089: return false;
090:
091: Calendar calendar1 = (Calendar) x;
092: Calendar calendar2 = (Calendar) y;
093:
094: return calendar1.get(Calendar.MILLISECOND) == calendar2
095: .get(Calendar.MILLISECOND)
096: && calendar1.get(Calendar.SECOND) == calendar2
097: .get(Calendar.SECOND)
098: && calendar1.get(Calendar.MINUTE) == calendar2
099: .get(Calendar.MINUTE)
100: && calendar1.get(Calendar.HOUR_OF_DAY) == calendar2
101: .get(Calendar.HOUR_OF_DAY)
102: && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2
103: .get(Calendar.DAY_OF_MONTH)
104: && calendar1.get(Calendar.MONTH) == calendar2
105: .get(Calendar.MONTH)
106: && calendar1.get(Calendar.YEAR) == calendar2
107: .get(Calendar.YEAR);
108: }
109:
110: public int getHashCode(Object x, EntityMode entityMode) {
111: Calendar calendar = (Calendar) x;
112: int hashCode = 1;
113: hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND);
114: hashCode = 31 * hashCode + calendar.get(Calendar.SECOND);
115: hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE);
116: hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY);
117: hashCode = 31 * hashCode + calendar.get(Calendar.DAY_OF_MONTH);
118: hashCode = 31 * hashCode + calendar.get(Calendar.MONTH);
119: hashCode = 31 * hashCode + calendar.get(Calendar.YEAR);
120: return hashCode;
121: }
122:
123: public String getName() {
124: return "calendar";
125: }
126:
127: public Object next(Object current, SessionImplementor session) {
128: return seed(session);
129: }
130:
131: public Object seed(SessionImplementor session) {
132: return Calendar.getInstance();
133: }
134:
135: public Comparator getComparator() {
136: return CalendarComparator.INSTANCE;
137: }
138:
139: }
|