View Javadoc
1   /*
2    *  Copyright 2013 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.spi.shared;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  
21  import org.hibernate.dialect.Dialect;
22  import org.hibernate.type.AbstractSingleColumnStandardBasicType;
23  import org.hibernate.type.IdentifierType;
24  import org.hibernate.type.LiteralType;
25  import org.hibernate.type.StringType;
26  import org.hibernate.type.descriptor.java.JdbcDateTypeDescriptor;
27  import org.jadira.usertype.spi.shared.descriptor.sql.DstSafeDateTypeDescriptor;
28  
29  /**
30   * @deprecated This class should no longer be used now that Hibernate allows configuration of  hibernate.jdbc.time_zone configuration property 
31   */
32  @Deprecated
33  public class DstSafeDateType
34  		extends AbstractSingleColumnStandardBasicType<Date>
35  		implements IdentifierType<Date>, LiteralType<Date> {
36  
37  	private static final long serialVersionUID = 669738618020424223L;
38  
39  	public static final DstSafeDateType INSTANCE = new DstSafeDateType();
40  
41  	public DstSafeDateType() {
42  		super(DstSafeDateTypeDescriptor.INSTANCE,
43  				JdbcDateTypeDescriptor.INSTANCE);
44  	}
45  	
46  	public DstSafeDateType(Calendar cal) {
47  		super(cal == null ? DstSafeDateTypeDescriptor.INSTANCE : new DstSafeDateTypeDescriptor(cal),
48  				JdbcDateTypeDescriptor.INSTANCE);
49  	}
50  	public String getName() {
51  		return "dstSafeDate";
52  	}
53  
54  	@Override
55  	public String[] getRegistrationKeys() {
56  		return new String[] {
57  				getName(),
58  				java.sql.Date.class.getName()
59  		};
60  	}
61  
62  	public String objectToSQLString(Date value, Dialect dialect) throws Exception {
63  		final java.sql.Date jdbcDate = java.sql.Date.class.isInstance( value )
64  				? ( java.sql.Date ) value
65  				: new java.sql.Date( value.getTime() );
66  		// TODO : use JDBC date literal escape syntax? -> {d 'date-string'} in yyyy-mm-dd format
67  		return StringType.INSTANCE.objectToSQLString( jdbcDate.toString(), dialect );
68  	}
69  
70  	public Date stringToObject(String xml) {
71  		return fromString( xml );
72  	}
73  }