|
| 1 | +package org.hibernate.ejb.criteria.basic; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.Calendar; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.GregorianCalendar; |
| 10 | + |
| 11 | +import org.hibernate.HibernateException; |
| 12 | +import org.hibernate.engine.spi.SessionImplementor; |
| 13 | +import org.hibernate.type.StandardBasicTypes; |
| 14 | +import org.hibernate.type.Type; |
| 15 | +import org.hibernate.usertype.CompositeUserType; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Francois Gerodez |
| 19 | + */ |
| 20 | +public class Date3Type implements CompositeUserType { |
| 21 | + |
| 22 | + @Override |
| 23 | + public String[] getPropertyNames() { |
| 24 | + return new String[] { "year", "month", "day" }; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Type[] getPropertyTypes() { |
| 29 | + return new Type[] { StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER }; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public Object getPropertyValue(Object component, int property) throws HibernateException { |
| 34 | + Date date = (Date) component; |
| 35 | + Calendar c = GregorianCalendar.getInstance(); |
| 36 | + c.setTime( date ); |
| 37 | + |
| 38 | + switch ( property ) { |
| 39 | + case 0: |
| 40 | + return c.get( Calendar.YEAR ); |
| 41 | + case 1: |
| 42 | + return c.get( Calendar.MONTH ); |
| 43 | + case 2: |
| 44 | + return c.get( Calendar.DAY_OF_MONTH ); |
| 45 | + } |
| 46 | + |
| 47 | + throw new HibernateException( "Invalid property provided" ); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void setPropertyValue(Object component, int property, Object value) throws HibernateException { |
| 52 | + Date date = (Date) component; |
| 53 | + Calendar c = GregorianCalendar.getInstance(); |
| 54 | + c.setTime( date ); |
| 55 | + |
| 56 | + switch ( property ) { |
| 57 | + case 0: |
| 58 | + c.set( Calendar.YEAR, (Integer) value ); |
| 59 | + case 1: |
| 60 | + c.set( Calendar.MONTH, (Integer) value ); |
| 61 | + case 2: |
| 62 | + c.set( Calendar.DAY_OF_MONTH, (Integer) value ); |
| 63 | + default: |
| 64 | + throw new HibernateException( "Invalid property provided" ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Class returnedClass() { |
| 70 | + return Date.class; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean equals(Object x, Object y) throws HibernateException { |
| 75 | + if ( x == y ) |
| 76 | + return true; |
| 77 | + if ( x == null || y == null ) |
| 78 | + return false; |
| 79 | + Date dx = (Date) x; |
| 80 | + Date dy = (Date) y; |
| 81 | + |
| 82 | + return dx.equals( dy ); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode(Object x) throws HibernateException { |
| 87 | + Date dx = (Date) x; |
| 88 | + return dx.hashCode(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { |
| 93 | + Date date = new Date(); |
| 94 | + Calendar c = GregorianCalendar.getInstance(); |
| 95 | + c.setTime( date ); |
| 96 | + |
| 97 | + Integer year = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[0], session ); |
| 98 | + Integer month = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[1], session ); |
| 99 | + Integer day = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[2], session ); |
| 100 | + |
| 101 | + c.set( year, month, day ); |
| 102 | + |
| 103 | + return date; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { |
| 108 | + Date date = new Date(); |
| 109 | + Calendar c = GregorianCalendar.getInstance(); |
| 110 | + c.setTime( date ); |
| 111 | + |
| 112 | + StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.YEAR ), index, session ); |
| 113 | + StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.MONTH ), index + 1, session ); |
| 114 | + StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.DAY_OF_MONTH ), index + 2, session ); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Object deepCopy(Object value) throws HibernateException { |
| 119 | + if ( value == null ) |
| 120 | + return null; |
| 121 | + |
| 122 | + Date date = (Date) value; |
| 123 | + return date.clone(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean isMutable() { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException { |
| 133 | + return (Serializable) deepCopy( value ); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException { |
| 138 | + return deepCopy( cached ); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException { |
| 143 | + return deepCopy( original ); // TODO: improve |
| 144 | + } |
| 145 | +} |
0 commit comments