make Sequence.sequence a clob (#3325)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / URIUserType.java
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 import java.util.UUID;
19
20 import org.hibernate.HibernateException;
21 import org.hibernate.TypeMismatchException;
22 import org.hibernate.engine.spi.SessionImplementor;
23 import org.hibernate.type.StandardBasicTypes;
24 import org.hibernate.usertype.UserType;
25 import org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType;
26 import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
27 import org.jadira.usertype.dateandtime.shared.spi.ColumnMapper;
28
29 /**
30 * This class maps java.net.URI to varchar(255)
31 * @author a.mueller
32 *
33 */
34 public class URIUserType extends AbstractUserType implements UserType {
35 private static final long serialVersionUID = -5825017496962569105L;
36
37 /**
38 * SQL type for this usertype.
39 */
40 private static final int[] SQL_TYPES = {Types.VARCHAR};
41
42 /* (non-Javadoc)
43 * @see org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType#returnedClass()
44 */
45 @Override
46 public Class returnedClass() {
47 return URI.class;
48 }
49
50 @Override
51 public int[] sqlTypes() {
52 return SQL_TYPES;
53 }
54
55
56 @Override
57 public URI nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
58 throws HibernateException, SQLException {
59 String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
60
61 if(val == null) {
62 return null;
63 } else {
64 try {
65 return new URI(val);
66 } catch (URISyntaxException e) {
67 throw new TypeMismatchException(e);
68 }
69 }
70 }
71
72
73
74 @Override
75 public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
76 throws HibernateException, SQLException {
77
78 if (value == null) {
79 // statement.setString(index, null); old version
80 StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
81 } else {
82 URI uri = (URI) value;
83 // statement.setString(index, uri.toString()); //old version
84 StandardBasicTypes.STRING.nullSafeSet(statement, uri.toString(), index, session);
85 }
86
87 }
88
89 /**
90 * @param value value being copied
91 * @return copied value
92 */
93 public Object deepCopy(Object value) {
94 if (value == null) {
95 return null;
96 }
97 try {
98 return new URI(((URI) value).toString());
99 } catch (URISyntaxException e) {
100 throw new TypeMismatchException(e);
101 }
102 }
103
104
105
106 }