View Javadoc
1   /*
2    *  Copyright 2010, 2011 Christopher Pheby
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.jadira.usertype.dateandtime.joda;
17  
18  import org.hibernate.usertype.ParameterizedType;
19  import org.jadira.usertype.dateandtime.joda.columnmapper.TimestampColumnDateTimeMapper;
20  import org.jadira.usertype.spi.shared.AbstractParameterizedTemporalMultiColumnUserType;
21  import org.jadira.usertype.spi.shared.IntegratorConfiguredType;
22  import org.jadira.usertype.spi.utils.reflection.ArrayUtils;
23  import org.joda.time.DateTime;
24  import org.joda.time.Interval;
25  
26  /**
27   * Persist {@link Interval} via Hibernate. The interval type is intended to be compatible with
28   * org.joda.time.contrib.hibernate.PersistentInterval and stores beginning and end values.
29   * The types are stored using the timezone as configured 
30   * using Hibernate's configuration property 'hibernate.jdbc.time_zone' and presented in the
31   * JVM using the JVM's default zone. You can optionally override or use as an alternative to this property the 
32   * parameter 'databaseZone' on this type.
33   *
34   * Alternatively provide the 'javaZone' can be used to similarly configure the zone of the
35   * value on return from the database.
36   */
37  public class PersistentInterval extends AbstractParameterizedTemporalMultiColumnUserType<Interval> implements ParameterizedType, IntegratorConfiguredType {
38  
39      private static final long serialVersionUID = 1364221029392346011L;
40  
41      private static final TimestampColumnDateTimeMapper[] COLUMN_MAPPERS = new TimestampColumnDateTimeMapper[] { new TimestampColumnDateTimeMapper(), new TimestampColumnDateTimeMapper() };
42  
43      private static final String[] PROPERTY_NAMES = new String[]{ "begin", "end" };
44      
45      @Override
46      protected Interval fromConvertedColumns(Object[] convertedColumns) {
47  
48          DateTime begin = (DateTime) convertedColumns[0];
49          DateTime end = (DateTime) convertedColumns[1];
50  
51          return new Interval(begin, end);
52      }
53  
54      @Override
55      protected TimestampColumnDateTimeMapper[] getColumnMappers() {
56          return COLUMN_MAPPERS;
57      }
58  
59      @Override
60      protected Object[] toConvertedColumns(Interval value) {
61  
62          return new Object[] { value.getStart(), value.getEnd() };
63      }
64  
65      @Override
66      public String[] getPropertyNames() {
67  
68          return ArrayUtils.copyOf(PROPERTY_NAMES);
69      }
70  }