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