2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.hibernate
;
12 import java
.io
.Serializable
;
14 import java
.net
.URISyntaxException
;
15 import java
.sql
.PreparedStatement
;
16 import java
.sql
.ResultSet
;
17 import java
.sql
.SQLException
;
18 import java
.sql
.Types
;
20 import org
.hibernate
.TypeMismatchException
;
21 import org
.hibernate
.usertype
.UserType
;
24 * This class maps java.net.URI to varchar(255)
28 public class URIUserType
implements UserType
, Serializable
{
29 private static final long serialVersionUID
= -5825017496962569105L;
32 * SQL type for this usertype.
34 private static final int[] SQL_TYPES
= {Types
.VARCHAR
};
37 * @return returns SQL type for the column to which the UserType is mapped
39 public int[] sqlTypes() {
44 * @return returns the class of the instance being managed by the UserType
46 public Class
returnedClass() {
51 * @param x first object to compare
52 * @param y second object to compare
53 * @return comparison result
54 * @throws HibernateException
56 public boolean equals(Object x
, Object y
) {
59 } else if (x
== null || y
== null) {
67 * @param resultSet resultset object
68 * @param names names of the columns in the resultset
69 * @param owner parent object on which the value is to be set
70 * @return returns URI object
71 * @throws SQLException throws exception when error occurs in accessing the resultSet
73 public Object
nullSafeGet(ResultSet resultSet
, String
[] names
, Object owner
)
75 String strURI
= resultSet
.getString(names
[0]);
79 uri
= new URI(strURI
);
80 } catch (URISyntaxException e
) {
81 throw new TypeMismatchException(e
);
88 * @param statement prepared statement object
89 * @param value value being set in the statement
90 * @param index location of the value in the statement
91 * @throws SQLException throws exception when error occurs in accessing the statement
93 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
)
96 statement
.setString(index
, null);
98 URI uri
= (URI
) value
;
99 statement
.setString(index
, uri
.toString());
104 * @param value value being copied
105 * @return copied value
107 public Object
deepCopy(Object value
) {
112 return new URI(((URI
) value
).toString());
113 } catch (URISyntaxException e
) {
114 throw new TypeMismatchException(e
);
119 * @return returns false
121 public boolean isMutable() {
126 * @param arg0 object to be assembled
127 * @param arg1 object to be assembled
128 * @return returns assembled object
130 public Object
assemble(Serializable arg0
, Object arg1
) {
131 return (Serializable
) deepCopy(arg0
);
135 * @param arg0 object to be disassembled
136 * @return returns disassembled object
138 public Serializable
disassemble(Object arg0
) {
139 return (Serializable
) deepCopy(arg0
);
143 * @param arg0 object whose hashcode is to be determined
146 public int hashCode(Object arg0
) {
151 * @param arg0 argument 0
152 * @param arg1 argument 1
153 * @param arg2 argument 2
154 * @return returns arg0
156 public Object
replace(Object arg0
, Object arg1
, Object arg2
) {