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.Instant;
028    
029    /**
030     * Persist {@link org.joda.time.Instant} via hibernate as a BIGINT.
031     * 
032     * @author Martin Grove (marting@optrak.co.uk))
033     */
034    public class PersistentInstantAsBigInt implements EnhancedUserType, Serializable {
035    
036        public static final PersistentInstantAsBigInt INSTANCE = new PersistentInstantAsBigInt();
037    
038        private static final int[] SQL_TYPES = new int[] { Types.BIGINT };
039    
040        public int[] sqlTypes() {
041            return SQL_TYPES;
042        }
043    
044        public Class returnedClass() {
045            return Instant.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            Instant ix = (Instant) x;
056            Instant iy = (Instant) y;
057            return ix.equals(iy);
058        }
059    
060        public int hashCode(Object object) throws HibernateException {
061            return object.hashCode();
062        }
063    
064        public Object nullSafeGet(ResultSet resultSet, String[] names, Object object) throws HibernateException, SQLException {
065            return nullSafeGet(resultSet, names[0]);
066        }
067    
068        public Object nullSafeGet(ResultSet resultSet, String name) throws HibernateException, SQLException {
069            Object value = StandardBasicTypes.LONG.nullSafeGet(resultSet, name);
070            if (value == null) {
071                return null;
072            }
073            return new Instant(value);
074        }
075    
076        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
077            if (value == null) {
078                StandardBasicTypes.LONG.nullSafeSet(preparedStatement, null, index);
079            } else {
080                StandardBasicTypes.LONG.nullSafeSet(preparedStatement, new Long(((Instant) value).getMillis()), index);
081            }
082        }
083    
084        public Object deepCopy(Object value) throws HibernateException {
085            return value;
086        }
087    
088        public boolean isMutable() {
089            return false;
090        }
091    
092        public Serializable disassemble(Object value) throws HibernateException {
093            return (Serializable) value;
094        }
095    
096        public Object assemble(Serializable serializable, Object value) throws HibernateException {
097            return serializable;
098        }
099    
100        public Object replace(Object original, Object target, Object owner) throws HibernateException {
101            return original;
102        }
103    
104        // __________ EnhancedUserType ____________________
105    
106        public String objectToSQLString(Object object) {
107            throw new UnsupportedOperationException();
108        }
109    
110        public String toXMLString(Object object) {
111            return object.toString();
112        }
113    
114        public Object fromXMLString(String string) {
115            return new Instant(string);
116        }
117    
118    }