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.Types;
023    
024    import org.hibernate.HibernateException;
025    import org.hibernate.type.StandardBasicTypes;
026    import org.hibernate.usertype.EnhancedUserType;
027    import org.joda.time.DateTime;
028    
029    /**
030     * Persist {@link org.joda.time.DateTime} via hibernate.
031     * 
032     * @author Mario Ivankovits (mario@ops.co.at)
033     */
034    public class PersistentDateTime implements EnhancedUserType, Serializable {
035    
036        public static final PersistentDateTime INSTANCE = new PersistentDateTime();
037    
038        private static final int[] SQL_TYPES = new int[] { Types.TIMESTAMP, };
039    
040        public int[] sqlTypes() {
041            return SQL_TYPES;
042        }
043    
044        public Class returnedClass() {
045            return DateTime.class;
046        }
047    
048        public boolean equals(Object x, Object y) throws HibernateException {
049            if (x == y) {
050                return true;
051            }
052            if (x == null || y == null) {
053                return false;
054            }
055            DateTime dtx = (DateTime) x;
056            DateTime dty = (DateTime) y;
057    
058            return dtx.equals(dty);
059        }
060    
061        public int hashCode(Object object) throws HibernateException {
062            return object.hashCode();
063        }
064    
065        public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
066            return nullSafeGet(resultSet, strings[0]);
067    
068        }
069    
070        public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
071            Object timestamp = StandardBasicTypes.TIMESTAMP.nullSafeGet(resultSet, string);
072            if (timestamp == null) {
073                return null;
074            }
075    
076            return new DateTime(timestamp);
077        }
078    
079        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
080            if (value == null) {
081                StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, null, index);
082            } else {
083                StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, ((DateTime) value).toDate(), index);
084            }
085        }
086    
087        public Object deepCopy(Object value) throws HibernateException {
088            return value;
089        }
090    
091        public boolean isMutable() {
092            return false;
093        }
094    
095        public Serializable disassemble(Object value) throws HibernateException {
096            return (Serializable) value;
097        }
098    
099        public Object assemble(Serializable cached, Object value) throws HibernateException {
100            return cached;
101        }
102    
103        public Object replace(Object original, Object target, Object owner) throws HibernateException {
104            return original;
105        }
106    
107        public String objectToSQLString(Object object) {
108            throw new UnsupportedOperationException();
109        }
110    
111        public String toXMLString(Object object) {
112            return object.toString();
113        }
114    
115        public Object fromXMLString(String string) {
116            return new DateTime(string);
117        }
118    
119    }