View Javadoc

1   /*
2    *  Copyright 2001-2011 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.contrib.hibernate;
17  
18  import java.io.Serializable;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Types;
23  
24  import org.hibernate.HibernateException;
25  import org.hibernate.type.StandardBasicTypes;
26  import org.hibernate.usertype.EnhancedUserType;
27  import org.joda.time.YearMonthDay;
28  
29  /**
30   * Persist {@link org.joda.time.YearMonthDay} via hibernate.
31   * 
32   * @author Mario Ivankovits (mario@ops.co.at)
33   */
34  public class PersistentYearMonthDay implements EnhancedUserType, Serializable {
35  
36      public static final PersistentYearMonthDay INSTANCE = new PersistentYearMonthDay();
37  
38      private static final int[] SQL_TYPES = new int[] { Types.DATE, };
39  
40      public int[] sqlTypes() {
41          return SQL_TYPES;
42      }
43  
44      public Class returnedClass() {
45          return YearMonthDay.class;
46      }
47  
48      public boolean equals(Object x, Object y) throws HibernateException {
49          if (x == y) {
50              return true;
51          }
52          if (x == null || y == null) {
53              return false;
54          }
55          YearMonthDay dtx = (YearMonthDay) x;
56          YearMonthDay dty = (YearMonthDay) y;
57          return dtx.equals(dty);
58      }
59  
60      public int hashCode(Object object) throws HibernateException {
61          return object.hashCode();
62      }
63  
64      public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
65          return nullSafeGet(resultSet, strings[0]);
66  
67      }
68  
69      public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
70          Object date = StandardBasicTypes.DATE.nullSafeGet(resultSet, string);
71          if (date == null) {
72              return null;
73          }
74          return new YearMonthDay(date);
75      }
76  
77      public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
78          if (value == null) {
79              StandardBasicTypes.DATE.nullSafeSet(preparedStatement, null, index);
80          } else {
81              StandardBasicTypes.DATE.nullSafeSet(preparedStatement, ((YearMonthDay) value).toDateMidnight().toDate(), index);
82          }
83      }
84  
85      public Object deepCopy(Object value) throws HibernateException {
86          return value;
87      }
88  
89      public boolean isMutable() {
90          return false;
91      }
92  
93      public Serializable disassemble(Object value) throws HibernateException {
94          return (Serializable) value;
95      }
96  
97      public Object assemble(Serializable cached, Object value) throws HibernateException {
98          return cached;
99      }
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 YearMonthDay(string);
115     }
116 
117 }