1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.hibernate;
|
11
|
|
12
|
import java.io.Serializable;
|
13
|
import java.sql.PreparedStatement;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.sql.Types;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.hibernate.HibernateException;
|
21
|
import org.hibernate.engine.spi.SessionImplementor;
|
22
|
import org.hibernate.type.StandardBasicTypes;
|
23
|
import org.hibernate.usertype.UserType;
|
24
|
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
|
25
|
|
26
|
/**
|
27
|
* @author a.mueller
|
28
|
* @created 22.07.2008
|
29
|
* @version 2.0
|
30
|
*/
|
31
|
public class UUIDUserType extends AbstractUserType implements UserType {
|
32
|
static final long serialVersionUID = -3959049831344758708L;
|
33
|
|
34
|
@SuppressWarnings("unused")
|
35
|
private static final Logger logger = Logger.getLogger(UUIDUserType.class);
|
36
|
|
37
|
private static final int[] SQL_TYPES = { Types.VARCHAR };
|
38
|
|
39
|
/* (non-Javadoc)
|
40
|
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
|
41
|
*/
|
42
|
public Object deepCopy(Object o) throws HibernateException {
|
43
|
if (o == null) {
|
44
|
return null;
|
45
|
}
|
46
|
|
47
|
UUID uuid = (UUID) o;
|
48
|
|
49
|
try {
|
50
|
return UUID.fromString(uuid.toString());
|
51
|
} catch (IllegalArgumentException e) {
|
52
|
throw new HibernateException(e);
|
53
|
}
|
54
|
}
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
|
58
|
*/
|
59
|
public Serializable disassemble(Object value) throws HibernateException {
|
60
|
if(value == null) {
|
61
|
return null;
|
62
|
} else {
|
63
|
UUID uuid = (UUID) value;
|
64
|
return uuid.toString();
|
65
|
}
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public UUID nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
|
70
|
throws HibernateException, SQLException {
|
71
|
String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
|
72
|
|
73
|
if(val == null) {
|
74
|
return null;
|
75
|
} else {
|
76
|
|
77
|
try {
|
78
|
return UUID.fromString(val);
|
79
|
} catch (IllegalArgumentException e) {
|
80
|
throw new HibernateException(e);
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
|
87
|
throws HibernateException, SQLException {
|
88
|
if (value == null) {
|
89
|
// statement.setNull(index, Types.VARCHAR); old version
|
90
|
StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
|
91
|
} else {
|
92
|
UUID uuid = (UUID)value;
|
93
|
// statement.setString(index, uuid.toString()); //old version
|
94
|
StandardBasicTypes.STRING.nullSafeSet(statement, uuid.toString(), index, session);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
|
99
|
/* (non-Javadoc)
|
100
|
* @see org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType#returnedClass()
|
101
|
*/
|
102
|
@Override
|
103
|
public Class returnedClass() {
|
104
|
return UUID.class;
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public int[] sqlTypes() {
|
109
|
return SQL_TYPES;
|
110
|
}
|
111
|
|
112
|
|
113
|
|
114
|
|
115
|
|
116
|
}
|