View Javadoc
1   /*
2    *  Copyright 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.integrator;
17  
18  import org.hibernate.integrator.spi.Integrator;
19  import org.hibernate.usertype.CompositeUserType;
20  import org.hibernate.usertype.UserType;
21  import org.jadira.usertype.dateandtime.joda.PersistentDateMidnight;
22  import org.jadira.usertype.dateandtime.joda.PersistentDateTime;
23  import org.jadira.usertype.dateandtime.joda.PersistentDurationAsString;
24  import org.jadira.usertype.dateandtime.joda.PersistentInstantAsTimestamp;
25  import org.jadira.usertype.dateandtime.joda.PersistentInterval;
26  import org.jadira.usertype.dateandtime.joda.PersistentLocalDate;
27  import org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime;
28  import org.jadira.usertype.dateandtime.joda.PersistentLocalTime;
29  import org.jadira.usertype.dateandtime.joda.PersistentMinutes;
30  import org.jadira.usertype.dateandtime.joda.PersistentMonthDayAsString;
31  import org.jadira.usertype.dateandtime.joda.PersistentPeriodAsString;
32  import org.jadira.usertype.dateandtime.joda.PersistentYears;
33  import org.jadira.usertype.spi.shared.AbstractUserTypeHibernateIntegrator;
34  import org.jadira.usertype.spi.utils.reflection.ClassLoaderUtils;
35  
36  @SuppressWarnings("deprecation")
37  public class UserTypeJodaTimeHibernateIntegrator extends AbstractUserTypeHibernateIntegrator implements Integrator {
38  
39  	private static UserType[] userTypes;
40  	private static CompositeUserType[] compositeUserTypes;
41  
42  	static {
43  
44  		boolean found = false;
45  
46  		try {
47  			Class.forName("org.joda.time.DateTime", false, ClassLoaderUtils.getClassLoader());
48  			found = true;
49  		} catch (ClassNotFoundException e) {
50  		}
51  
52  		try {
53  			Class.forName("org.joda.time.DateTime");
54  			found = true;
55  		} catch (ClassNotFoundException e) {
56  		}
57  
58  		if (found) {
59  
60  			userTypes = new UserType[] {
61  					new PersistentDateTime(),
62  					new PersistentDurationAsString(),
63  					new PersistentInstantAsTimestamp(),
64  					new PersistentLocalDate(),
65  					new PersistentLocalDateTime(),
66  					new PersistentLocalTime(),
67  					new PersistentMonthDayAsString(),
68  					new PersistentPeriodAsString(),
69  					new org.jadira.usertype.dateandtime.joda.PersistentTimeOfDay(),
70  					new org.jadira.usertype.dateandtime.joda.PersistentYearMonthDay(),
71  					new PersistentYears(), new PersistentMinutes(), 
72  			};
73  			compositeUserTypes = new CompositeUserType[] {
74  					new PersistentDateMidnight(), 
75  					new PersistentInterval(), 
76  			};
77  
78  		} else {
79  			userTypes = new UserType[] {};
80  			compositeUserTypes = new CompositeUserType[] {};
81  		}
82  	}
83  	
84  	/**
85  	 * {@inheritDoc}
86  	 */
87  	@Override
88  	protected CompositeUserType[] getCompositeUserTypes() {
89  		return compositeUserTypes;
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 */
95  	@Override
96  	protected UserType[] getUserTypes() {
97  		return userTypes;
98  	}	
99  }