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.spi.shared;
17  
18  import java.sql.Timestamp;
19  import java.sql.Types;
20  import java.util.TimeZone;
21  
22  import org.hibernate.engine.spi.SharedSessionContractImplementor;
23  import org.hibernate.type.TimestampType;
24  import org.jadira.usertype.spi.timezone.proxy.TimeZoneProvidingSharedSessionContractImplementor;
25  
26  public abstract class AbstractTimestampColumnMapper<T> extends AbstractColumnMapper<T, Timestamp> implements ColumnMapper<T, Timestamp>, DatabaseZoneConfigured {
27  
28      private static final long serialVersionUID = -3070239764121234482L;
29  
30  	private TimeZone databaseZone;
31      
32      @Override
33      public final TimestampType getHibernateType() {    	
34      	return TimestampType.INSTANCE;
35      }
36      
37      @Override
38      public final int getSqlType() {
39          return Types.TIMESTAMP;
40      }
41  
42      @Override
43      public abstract T fromNonNullValue(Timestamp value);
44  
45      @Override
46      public abstract T fromNonNullString(String s);
47  
48      @Override
49      public abstract Timestamp toNonNullValue(T value);
50  
51      @Override
52      public abstract String toNonNullString(T value);
53      
54  	@Override
55  	public void setDatabaseZone(TimeZone databaseZone) {
56  		this.databaseZone = databaseZone;
57  	}
58  
59  	@Override
60  	public TimeZone parseZone(String zoneString) {
61  		return TimeZone.getTimeZone(zoneString);
62  	}
63  
64  	@Override
65  	public TimeZone getDatabaseZone() {
66  		return databaseZone;
67  	}
68  
69  	public SharedSessionContractImplementor wrapSession(SharedSessionContractImplementor session) {
70  		
71  		final SharedSessionContractImplementor mySession;
72  
73  		if (databaseZone != null) {
74  			mySession = new TimeZoneProvidingSharedSessionContractImplementor(session, databaseZone);
75  		} else {
76  			mySession = session;
77  		}
78  		
79  		return mySession;
80  	}
81  }
82