Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/type/CalendarType.java
//$Id: CalendarType.java 7736 2005-08-03 20:03:34Z steveebersole $
package org.hibernate.type;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;

import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.cfg.Environment;
import org.hibernate.util.CalendarComparator;

/**
 <tt>calendar</tt>: A type mapping for a <tt>Calendar</tt> object that
 * represents a datetime.
 @author Gavin King
 */
public class CalendarType extends MutableType implements VersionType {

  public Object get(ResultSet rs, String namethrows HibernateException, SQLException {

    Timestamp ts = rs.getTimestamp(name);
    if (ts!=null) {
      Calendar cal = new GregorianCalendar();
      if Environment.jvmHasTimestampBug() ) {
        cal.setTimenew Datets.getTime() + ts.getNanos() 1000000 ) );
      }
      else {
        cal.setTime(ts);
      }
      return cal;
    }
    else {
      return null;
    }

  }

  public void set(PreparedStatement st, Object value, int indexthrows HibernateException, SQLException {
    final Calendar cal = (Calendarvalue;
    //st.setTimestamp( index,  new Timestamp( cal.getTimeInMillis() ), cal ); //JDK 1.5 only
    st.setTimestampindex,  new Timestampcal.getTime().getTime() ), cal );
  }

  public int sqlType() {
    return Types.TIMESTAMP;
  }

  public String toString(Object valuethrows HibernateException {
    return Hibernate.TIMESTAMP.toString( ( (Calendarvalue ).getTime() );
  }

  public Object fromStringValue(String xmlthrows HibernateException {
    Calendar result = new GregorianCalendar();
    result.setTime( ( (DateHibernate.TIMESTAMP.fromStringValue(xml) ) );
    return result;
  }

  public Object deepCopyNotNull(Object valuethrows HibernateException {
    return ( (Calendarvalue ).clone();
  }

  public Class getReturnedClass() {
    return Calendar.class;
  }
  
  public int compare(Object x, Object y, EntityMode entityMode) {
    return CalendarComparator.INSTANCE.compare(x, y);
  }

  public boolean isEqual(Object x, Object y) {
    if (x==yreturn true;
    if (x==null || y==nullreturn false;

    Calendar calendar1 = (Calendarx;
    Calendar calendar2 = (Calendary;

    return calendar1.get(Calendar.MILLISECOND== calendar2.get(Calendar.MILLISECOND)
      && calendar1.get(Calendar.SECOND== calendar2.get(Calendar.SECOND)
      && calendar1.get(Calendar.MINUTE== calendar2.get(Calendar.MINUTE)
      && calendar1.get(Calendar.HOUR_OF_DAY== calendar2.get(Calendar.HOUR_OF_DAY)
      && calendar1.get(Calendar.DAY_OF_MONTH== calendar2.get(Calendar.DAY_OF_MONTH)
      && calendar1.get(Calendar.MONTH== calendar2.get(Calendar.MONTH)
      && calendar1.get(Calendar.YEAR== calendar2.get(Calendar.YEAR);
  }

  public int getHashCode(Object x, EntityMode entityMode) {
    Calendar calendar = (Calendarx;
    int hashCode = 1;
    hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND);
    hashCode = 31 * hashCode + calendar.get(Calendar.SECOND);
    hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE);
    hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY);
    hashCode = 31 * hashCode + calendar.get(Calendar.DAY_OF_MONTH);
    hashCode = 31 * hashCode + calendar.get(Calendar.MONTH);
    hashCode = 31 * hashCode + calendar.get(Calendar.YEAR);
    return hashCode;
  }

  public String getName() {
    return "calendar";
  }

  public Object next(Object current, SessionImplementor session) {
    return seedsession );
  }

  public Object seed(SessionImplementor session) {
    return Calendar.getInstance();
  }

  public Comparator getComparator() {
    return CalendarComparator.INSTANCE;
  }

}