001    /*
002     *  Copyright 2001-2011 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.Time;
023    import java.sql.Types;
024    
025    import org.hibernate.HibernateException;
026    import org.hibernate.type.StandardBasicTypes;
027    import org.hibernate.usertype.EnhancedUserType;
028    import org.joda.time.DateTimeZone;
029    import org.joda.time.LocalTime;
030    
031    /**
032     * Persist {@link org.joda.time.LocalDate} via hibernate.
033     * This uses a simple integer to store the time as milliseconds since 1970-1-1.
034     * The milliseconds will survive.
035     * 
036     * @author Mario Ivankovits (mario@ops.co.at)
037     */
038    public class PersistentLocalTimeAsTime implements EnhancedUserType, Serializable {
039    
040        public static final PersistentLocalTimeAsTime INSTANCE = new PersistentLocalTimeAsTime();
041    
042        private static final int[] SQL_TYPES = new int[] { Types.TIME, };
043    
044        public int[] sqlTypes() {
045            return SQL_TYPES;
046        }
047    
048        public Class returnedClass() {
049            return LocalTime.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            LocalTime dtx = (LocalTime) x;
060            LocalTime dty = (LocalTime) y;
061            return dtx.equals(dty);
062        }
063    
064        public int hashCode(Object object) throws HibernateException {
065            return object.hashCode();
066        }
067    
068        public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
069            return nullSafeGet(resultSet, strings[0]);
070    
071        }
072    
073        public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
074            Object timestamp = StandardBasicTypes.TIME.nullSafeGet(resultSet, string);
075            if (timestamp == null) {
076                return null;
077            }
078    
079            return new LocalTime(timestamp, DateTimeZone.UTC);
080        }
081    
082        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
083            if (value == null) {
084                StandardBasicTypes.TIME.nullSafeSet(preparedStatement, null, index);
085            } else {
086                LocalTime lt = ((LocalTime) value);
087                Time time = new Time(lt.getMillisOfDay());
088                StandardBasicTypes.TIME.nullSafeSet(preparedStatement, time, index);
089            }
090        }
091    
092        public Object deepCopy(Object value) throws HibernateException {
093            return value;
094        }
095    
096        public boolean isMutable() {
097            return false;
098        }
099    
100        public Serializable disassemble(Object value) throws HibernateException {
101            return (Serializable) value;
102        }
103    
104        public Object assemble(Serializable cached, Object value) throws HibernateException {
105            return cached;
106        }
107    
108        public Object replace(Object original, Object target, Object owner) throws HibernateException {
109            return original;
110        }
111    
112        public String objectToSQLString(Object object) {
113            throw new UnsupportedOperationException();
114        }
115    
116        public String toXMLString(Object object) {
117            return object.toString();
118        }
119    
120        public Object fromXMLString(String string) {
121            return new LocalTime(string);
122        }
123    
124    }