001    /*
002     *  Copyright 2001-2009 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.contrib.hibernate;
017    
018    import java.io.Serializable;
019    import java.sql.PreparedStatement;
020    import java.sql.ResultSet;
021    import java.sql.SQLException;
022    import java.sql.Types;
023    
024    import org.hibernate.HibernateException;
025    import org.hibernate.usertype.EnhancedUserType;
026    import org.joda.time.DateTime;
027    import org.joda.time.TimeOfDay;
028    
029    /**
030     * Persist {@link org.joda.time.TimeOfDay} via hibernate.
031     * This uses a simple integer to store the time as milliseconds since 1970-1-1.
032     * The milliseconds will survive.
033     * 
034     * @author Mario Ivankovits (mario@ops.co.at)
035     */
036    public class PersistentTimeOfDayExact implements EnhancedUserType, Serializable {
037    
038        private final DateTime timeBase = new DateTime(1970, 1, 1, 0, 0, 0, 0);
039    
040        public static final PersistentTimeOfDayExact INSTANCE = new PersistentTimeOfDayExact();
041    
042        private static final int[] SQL_TYPES = new int[] { Types.INTEGER, };
043    
044        public int[] sqlTypes() {
045            return SQL_TYPES;
046        }
047    
048        public Class returnedClass() {
049            return TimeOfDay.class;
050        }
051    
052        public boolean equals(Object x, Object y) throws HibernateException {
053            if (x == y) {
054                return true;
055            }
056            if (x == null || y == null) {
057                return false;
058            }
059            TimeOfDay dtx = (TimeOfDay) x;
060            TimeOfDay dty = (TimeOfDay) y;
061    
062            return dtx.equals(dty);
063        }
064    
065        public int hashCode(Object object) throws HibernateException {
066            return object.hashCode();
067        }
068    
069        public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
070            return nullSafeGet(resultSet, strings[0]);
071    
072        }
073    
074        public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
075            int value = resultSet.getInt(string);
076            if (resultSet.wasNull()) {
077                return null;
078            }
079            return new TimeOfDay(value);
080        }
081    
082        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
083            if (value == null) {
084                preparedStatement.setNull(index, SQL_TYPES[0]);
085            } else {
086                preparedStatement.setInt(index, (int) ((TimeOfDay) value).toDateTime(timeBase).getMillis());
087            }
088        }
089    
090        public Object deepCopy(Object value) throws HibernateException {
091            return value;
092        }
093    
094        public boolean isMutable() {
095            return false;
096        }
097    
098        public Serializable disassemble(Object value) throws HibernateException {
099            return (Serializable) value;
100        }
101    
102        public Object assemble(Serializable cached, Object value) throws HibernateException {
103            return cached;
104        }
105    
106        public Object replace(Object original, Object target, Object owner) throws HibernateException {
107            return original;
108        }
109    
110        public String objectToSQLString(Object object) {
111            throw new UnsupportedOperationException();
112        }
113    
114        public String toXMLString(Object object) {
115            return object.toString();
116        }
117    
118        public Object fromXMLString(String string) {
119            return new TimeOfDay(string);
120        }
121    
122    }