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 static org.jadira.usertype.spi.utils.reflection.ArrayUtils.copyOf;
19  
20  import java.io.Serializable;
21  import java.sql.PreparedStatement;
22  import java.sql.ResultSet;
23  import java.sql.SQLException;
24  
25  import org.hibernate.HibernateException;
26  import org.hibernate.engine.spi.SharedSessionContractImplementor;
27  import org.hibernate.usertype.EnhancedUserType;
28  import org.jadira.usertype.spi.utils.reflection.TypeHelper;
29  
30  public abstract class AbstractSingleColumnUserType<T, J, C extends ColumnMapper<T, J>> extends AbstractUserType implements EnhancedUserType, Serializable {
31  
32      private static final long serialVersionUID = -8258683760413283329L;
33  
34      private final C columnMapper;
35      private final int[] sqlTypes;
36  
37      @SuppressWarnings("unchecked")
38      public AbstractSingleColumnUserType() {
39  
40          try {
41              columnMapper = (C) TypeHelper.getTypeArguments(AbstractSingleColumnUserType.class, getClass()).get(2).newInstance();
42          } catch (InstantiationException ex) {
43              throw new HibernateException("Could not initialise column mapper for " + getClass(), ex);
44          } catch (IllegalAccessException ex) {
45              throw new HibernateException("Could not access column mapper for " + getClass(), ex);
46          }
47          sqlTypes = new int[] { getColumnMapper().getSqlType() };
48      }
49  
50      public final C getColumnMapper() {
51          return columnMapper;
52      }
53  
54      @Override
55      public Class<T> returnedClass() {
56          return getColumnMapper().returnedClass();
57      }
58  
59      @Override
60      public final int[] sqlTypes() {
61          return copyOf(sqlTypes);
62      }
63  
64      @Override
65      public T nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor session, Object object) throws SQLException {
66          
67      	beforeNullSafeOperation(session);
68          	
69      	final SharedSessionContractImplementor mySession = doWrapSession(session);
70      	
71      	try {
72  	    	J converted = doNullSafeGet(resultSet, strings, mySession, object);
73  	
74  	        if (converted == null) {
75  	            return null;
76  	        }
77  	
78  	        return getColumnMapper().fromNonNullValue(converted);
79  	        
80      	} finally {
81      		afterNullSafeOperation(session);
82      	}
83      }
84  
85      protected J doNullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor session, Object object) throws SQLException {
86  		@SuppressWarnings("unchecked")
87  		final J converted = (J) getColumnMapper().getHibernateType().nullSafeGet(resultSet, strings[0], session, object);
88  		return converted;
89  	}
90  
91  	@Override
92      public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws SQLException {
93  		
94      	beforeNullSafeOperation(session);
95      	    	
96      	final SharedSessionContractImplementor mySession = doWrapSession(session);
97      	
98      	try {
99  	        final J transformedValue;
100 	        if (value == null) {
101 	            transformedValue = null;
102 	        } else {
103 	            @SuppressWarnings("unchecked") T myValue = (T) value;
104 	            transformedValue = getColumnMapper().toNonNullValue(myValue);
105 	        }
106 	
107 	        doNullSafeSet(preparedStatement, transformedValue, index, mySession);
108 	        
109     	} finally {
110     		afterNullSafeOperation(session);
111     	}
112     }
113 
114     protected void doNullSafeSet(PreparedStatement preparedStatement, J transformedValue, int index, SharedSessionContractImplementor session) throws SQLException {
115     	getColumnMapper().getHibernateType().nullSafeSet(preparedStatement, transformedValue, index, session);
116 	}
117 
118 	@Override
119     public String objectToSQLString(Object object) {
120         @SuppressWarnings("unchecked") final T myObject = (T) object;
121         J convertedObject = myObject == null ? null : getColumnMapper().toNonNullValue(myObject);
122         
123         return getColumnMapper().getHibernateType().toString(convertedObject);
124     }
125 
126     @Override
127     public String toXMLString(Object object) {
128         @SuppressWarnings("unchecked") final T myObject = (T) object;
129         return getColumnMapper().toNonNullString(myObject);
130     }
131 
132     @Override
133     public T fromXMLString(String string) {
134         return getColumnMapper().fromNonNullString(string);
135     }
136     
137 	protected SharedSessionContractImplementor doWrapSession(SharedSessionContractImplementor session) {
138 		return session;
139 	}
140 }