Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a026fde8a8be4dee9a28782dd0d3f45bb65a3673 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * <copyright>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Brian Vetter
 *   Martin Taal
 *   Alexandros Karypidis (bugzilla 207799)
 * </copyright>
 *
 * $Id: XSDDateTime.java,v 1.4 2010/11/12 09:33:33 mtaal Exp $
 */
package org.eclipse.emf.teneo.hibernate.mapping;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.eclipse.emf.teneo.hibernate.HbStoreException;
import org.eclipse.emf.teneo.util.EcoreDataTypes;
import org.hibernate.HibernateException;
import org.hibernate.type.MutableType;

/**
 * Implements the hibernate UserType for EMF's XMLGregorianCalendar ("datetime"
 * type in XSD).
 * 
 * @author <a href="mailto:bvetter@alterpoint.com">Brian Vetter</a>
 * @version $Revision
 */
@SuppressWarnings("deprecation")
public class XSDDateTime extends MutableType {

	static final long serialVersionUID = 1;

	// local copy of the datatype facatory
	private final DatatypeFactory dataTypeFactory;

	public XSDDateTime() {
		try {
			dataTypeFactory = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			throw new HbStoreException("Exception ", e);
		}
	}

	/*
	 * Returns the DATETIME type that maps to the sql TIMESTAMP type
	 * 
	 * @see org.hibernate.type.NullableType#sqlType()
	 */
	@Override
	public int sqlType() {
		return Types.TIMESTAMP;
	}

	/*
	 * Copy the XMLGregorianCalendar object
	 * 
	 * @see org.hibernate.type.MutableType#deepCopyNotNull(java.lang.Object)
	 */
	@Override
	public Object deepCopyNotNull(Object value) {
		return dataTypeFactory
				.newXMLGregorianCalendar(((XMLGregorianCalendar) value)
						.toGregorianCalendar());
	}

	/*
	 * returns a name for the user type
	 * 
	 * @see org.hibernate.type.Type#getName()
	 */
	public String getName() {
		return "xmldatetime";
	}

	/*
	 * This returns an XMLGregorianCalendar.class type
	 * 
	 * @see org.hibernate.type.Type#getReturnedClass()
	 */
	public Class<?> getReturnedClass() {
		return XMLGregorianCalendar.class;
	}

	/*
	 * @see org.hibernate.type.NullableType#isEqual(java.lang.Object,
	 * java.lang.Object)
	 */
	@Override
	public boolean isEqual(Object x, Object y) throws HibernateException {
		if (x == y) {
			return true;
		}
		if (x == null || y == null) {
			return false;
		}
		if (x.getClass() != y.getClass()) {
			return false;
		}
		return x.equals(y);
	}

	/*
	 * Transform the date in the resultSet into a XMLGregorianCalendar instance.
	 * 
	 * @see org.hibernate.type.NullableType#get(java.sql.ResultSet,
	 * java.lang.String)
	 */
	@Override
	public Object get(ResultSet resultSet, String name) throws SQLException {
		// MT: changed this to timestamp to get the seconds right
		Timestamp ts = resultSet.getTimestamp(name);
		if (ts == null) {
			return null;
		}
		return EcoreDataTypes.INSTANCE.getXMLGregorianCalendarDateTime(ts);
	}

	/*
	 * Transform the XMLGregorianCalendar into a timestamp type to store in the
	 * database
	 * 
	 * @see org.hibernate.type.NullableType#set(java.sql.PreparedStatement,
	 * java.lang.Object, int)
	 */
	@Override
	public void set(PreparedStatement statement, Object value, int index)
			throws SQLException {
		Timestamp d = new Timestamp(((XMLGregorianCalendar) value)
				.toGregorianCalendar().getTime().getTime());
		statement.setTimestamp(index, d);
	}

	/*
	 * @see org.hibernate.type.NullableType#toString(java.lang.Object)
	 */
	@Override
	public String toString(Object val) {

		return ((XMLGregorianCalendar) val).toString();
	}

	/*
	 * @see org.hibernate.type.NullableType#fromStringValue(java.lang.String)
	 */
	@Override
	public Object fromStringValue(String s) throws HibernateException {
		return dataTypeFactory.newXMLGregorianCalendar(s);
	}
}

Back to the top