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.jadira.usertype.dateandtime.joda.util.DateTimeZoneWithOffset;
19  import org.jadira.usertype.spi.shared.AbstractMultiColumnUserType;
20  import org.joda.time.DateMidnight;
21  import org.joda.time.DateTimeZone;
22  import org.joda.time.LocalDate;
23  
24  /**
25   * Persist {@link DateMidnight} via Hibernate. The offset will be stored in an extra column.
26   * @deprecated
27   */
28  @Deprecated
29  public abstract class AbstractMultiColumnDateMidnight extends AbstractMultiColumnUserType<DateMidnight> {
30  
31      private static final long serialVersionUID = 7061588330446583269L;
32  
33  	@Override
34      protected DateMidnight fromConvertedColumns(Object[] convertedColumns) {
35  
36          LocalDate datePart = (LocalDate) convertedColumns[0];
37          DateTimeZoneWithOffset offset = (DateTimeZoneWithOffset) convertedColumns[1];
38  
39          final DateMidnight result;
40  
41          if (datePart == null) {
42              result = null;
43          } else {
44              result = new DateMidnight(datePart.getYear(), datePart.getMonthOfYear(), datePart.getDayOfMonth(), offset.getStandardDateTimeZone());
45          }
46          
47          // Handling DST rollover
48          if (datePart != null && offset.getOffsetDateTimeZone() != null &&
49          		offset.getStandardDateTimeZone().getOffset(result) > offset.getOffsetDateTimeZone().getOffset(result)) {
50          	return new DateMidnight(datePart.getYear(), datePart.getMonthOfYear(), datePart.getDayOfMonth(), offset.getOffsetDateTimeZone());
51          }
52  
53          return result;
54      }
55  
56  	@Override
57      protected Object[] toConvertedColumns(DateMidnight value) {
58  
59      	return new Object[] { value.toLocalDate(), new DateTimeZoneWithOffset(value.getZone(), value.getZone().isFixed() ? null : DateTimeZone.forOffsetMillis(value.getZone().getOffset(value))) };
60      }
61  }