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.sql.Timestamp;
19  import java.util.Calendar;
20  import java.util.Comparator;
21  import java.util.Date;
22  
23  import org.hibernate.HibernateException;
24  import org.hibernate.dialect.Dialect;
25  import org.hibernate.engine.spi.SharedSessionContractImplementor;
26  import org.hibernate.type.AbstractSingleColumnStandardBasicType;
27  import org.hibernate.type.LiteralType;
28  import org.hibernate.type.StringType;
29  import org.hibernate.type.VersionType;
30  import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor;
31  import org.jadira.usertype.spi.shared.descriptor.sql.DstSafeTimestampTypeDescriptor;
32  
33  /**
34   * @deprecated This class should no longer be used now that Hibernate allows configuration of  hibernate.jdbc.time_zone configuration property 
35   */
36  @Deprecated
37  public class DstSafeTimestampType extends
38  		AbstractSingleColumnStandardBasicType<Date> implements
39  		VersionType<Date>, LiteralType<Date> {
40  
41  	private static final long serialVersionUID = -3665273920874664942L;
42  
43  	public static final DstSafeTimestampType INSTANCE = new DstSafeTimestampType();
44  
45  	public DstSafeTimestampType() {
46  		super(DstSafeTimestampTypeDescriptor.INSTANCE,
47  				JdbcTimestampTypeDescriptor.INSTANCE);
48  	}
49  	
50  	public DstSafeTimestampType(Calendar cal) {
51  		super(cal == null ? DstSafeTimestampTypeDescriptor.INSTANCE : new DstSafeTimestampTypeDescriptor(cal),
52  				JdbcTimestampTypeDescriptor.INSTANCE);
53  	}
54  
55  	public String getName() {
56  		return "dstSafeTimestamp";
57  	}
58  
59  	@Override
60  	public String[] getRegistrationKeys() {
61  		return new String[] { getName(), Timestamp.class.getName(),
62  				java.util.Date.class.getName() };
63  	}
64  
65  	public Date next(Date current, SharedSessionContractImplementor session) {
66  		return seed(session);
67  	}
68  
69  	public Date seed(SharedSessionContractImplementor session) {
70  		return new Timestamp(System.currentTimeMillis());
71  	}
72  
73  	public Comparator<Date> getComparator() {
74  		return getJavaTypeDescriptor().getComparator();
75  	}
76  
77  	public String objectToSQLString(Date value, Dialect dialect)
78  			throws Exception {
79  		final Timestamp ts = Timestamp.class.isInstance(value) ? (Timestamp) value
80  				: new Timestamp(value.getTime());
81  		// TODO : use JDBC date literal escape syntax? -> {d 'date-string'} in
82  		// yyyy-mm-dd hh:mm:ss[.f...] format
83  		return StringType.INSTANCE.objectToSQLString(ts.toString(), dialect);
84  	}
85  
86  	public Date fromStringValue(String xml) throws HibernateException {
87  		return fromString(xml);
88  	}
89  }