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
|
package eu.etaxonomy.cdm.hibernate;
|
10
|
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.sql.PreparedStatement;
|
15
|
import java.sql.ResultSet;
|
16
|
import java.sql.SQLException;
|
17
|
import java.sql.Types;
|
18
|
|
19
|
import org.hibernate.HibernateException;
|
20
|
import org.hibernate.TypeMismatchException;
|
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
|
* This class maps java.net.URI to varchar(255)
|
28
|
* @author a.mueller
|
29
|
*
|
30
|
*/
|
31
|
public class URIUserType extends AbstractUserType implements UserType {
|
32
|
private static final long serialVersionUID = -5825017496962569105L;
|
33
|
|
34
|
/**
|
35
|
* SQL type for this usertype.
|
36
|
*/
|
37
|
private static final int[] SQL_TYPES = {Types.CLOB};
|
38
|
|
39
|
@Override
|
40
|
public Class<?> returnedClass() {
|
41
|
return URI.class;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public int[] sqlTypes() {
|
46
|
return SQL_TYPES;
|
47
|
}
|
48
|
|
49
|
|
50
|
@Override
|
51
|
public URI nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
|
52
|
throws HibernateException, SQLException {
|
53
|
String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
|
54
|
|
55
|
if(val == null) {
|
56
|
return null;
|
57
|
} else {
|
58
|
try {
|
59
|
return new URI(val);
|
60
|
} catch (URISyntaxException e) {
|
61
|
throw new TypeMismatchException(e.getMessage());
|
62
|
}
|
63
|
}
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
|
68
|
throws HibernateException, SQLException {
|
69
|
if (value == null) {
|
70
|
StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
|
71
|
} else {
|
72
|
URI uri = (URI) value;
|
73
|
StandardBasicTypes.STRING.nullSafeSet(statement, uri.toString(), index, session);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* @param value value being copied
|
79
|
* @return copied value
|
80
|
*/
|
81
|
@Override
|
82
|
public Object deepCopy(Object value) {
|
83
|
if (value == null) {
|
84
|
return null;
|
85
|
}
|
86
|
try {
|
87
|
return new URI(((URI) value).toString());
|
88
|
} catch (URISyntaxException e) {
|
89
|
throw new TypeMismatchException(e.getMessage());
|
90
|
}
|
91
|
}
|
92
|
|
93
|
}
|