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.Hibernate;
025 import org.hibernate.HibernateException;
026 import org.hibernate.usertype.EnhancedUserType;
027 import org.joda.time.LocalTime;
028
029 /**
030 * Persist {@link org.joda.time.LocalDate} via hibernate.
031 *
032 * @author Mario Ivankovits (mario@ops.co.at)
033 */
034 public class PersistentLocalTimeAsString implements EnhancedUserType, Serializable {
035
036 public static final PersistentLocalTimeAsString INSTANCE = new PersistentLocalTimeAsString();
037
038 private static final int[] SQL_TYPES = new int[] { Types.VARCHAR, };
039
040 public int[] sqlTypes() {
041 return SQL_TYPES;
042 }
043
044 public Class returnedClass() {
045 return LocalTime.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 LocalTime dtx = (LocalTime) x;
056 LocalTime dty = (LocalTime) 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 = Hibernate.STRING.nullSafeGet(resultSet, string);
071 if (timestamp == null) {
072 return null;
073 }
074
075 return new LocalTime(timestamp.toString());
076 }
077
078 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
079 if (value == null) {
080 Hibernate.STRING.nullSafeSet(preparedStatement, null, index);
081 } else {
082 LocalTime lt = ((LocalTime) value);
083 Hibernate.STRING.nullSafeSet(preparedStatement, lt.toString(), 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 LocalTime(string);
117 }
118
119 }