(no commit message)
[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.io.Serializable;
13 import java.net.URI;
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;
19
20 import org.hibernate.TypeMismatchException;
21 import org.hibernate.usertype.UserType;
22
23 /**
24 * This class maps java.net.URI to varchar(255)
25 * @author a.mueller
26 *
27 */
28 public class URIUserType implements UserType, Serializable {
29 private static final long serialVersionUID = -5825017496962569105L;
30
31 /**
32 * SQL type for this usertype.
33 */
34 private static final int[] SQL_TYPES = {Types.VARCHAR};
35
36 /**
37 * @return returns SQL type for the column to which the UserType is mapped
38 */
39 public int[] sqlTypes() {
40 return SQL_TYPES;
41 }
42
43 /**
44 * @return returns the class of the instance being managed by the UserType
45 */
46 public Class returnedClass() {
47 return URI.class;
48 }
49
50 /**
51 * @param x first object to compare
52 * @param y second object to compare
53 * @return comparison result
54 * @throws HibernateException
55 */
56 public boolean equals(Object x, Object y) {
57 if (x == y) {
58 return true;
59 } else if (x == null || y == null) {
60 return false;
61 } else {
62 return x.equals(y);
63 }
64 }
65
66 /**
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
72 */
73 public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
74 throws SQLException {
75 String strURI = resultSet.getString(names[0]);
76 URI uri = null;
77 if (null != strURI) {
78 try {
79 uri = new URI(strURI);
80 } catch (URISyntaxException e) {
81 throw new TypeMismatchException(e);
82 }
83 }
84 return uri;
85 }
86
87 /**
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
92 */
93 public void nullSafeSet(PreparedStatement statement, Object value, int index)
94 throws SQLException {
95 if (value == null) {
96 statement.setString(index, null);
97 } else {
98 URI uri = (URI) value;
99 statement.setString(index, uri.toString());
100 }
101 }
102
103 /**
104 * @param value value being copied
105 * @return copied value
106 */
107 public Object deepCopy(Object value) {
108 if (value == null) {
109 return null;
110 }
111 try {
112 return new URI(((URI) value).toString());
113 } catch (URISyntaxException e) {
114 throw new TypeMismatchException(e);
115 }
116 }
117
118 /**
119 * @return returns false
120 */
121 public boolean isMutable() {
122 return false;
123 }
124
125 /**
126 * @param arg0 object to be assembled
127 * @param arg1 object to be assembled
128 * @return returns assembled object
129 */
130 public Object assemble(Serializable arg0, Object arg1) {
131 return (Serializable) deepCopy(arg0);
132 }
133
134 /**
135 * @param arg0 object to be disassembled
136 * @return returns disassembled object
137 */
138 public Serializable disassemble(Object arg0) {
139 return (Serializable) deepCopy(arg0);
140 }
141
142 /**
143 * @param arg0 object whose hashcode is to be determined
144 * @return returns 0
145 */
146 public int hashCode(Object arg0) {
147 return 0;
148 }
149
150 /**
151 * @param arg0 argument 0
152 * @param arg1 argument 1
153 * @param arg2 argument 2
154 * @return returns arg0
155 */
156 public Object replace(Object arg0, Object arg1, Object arg2) {
157 return arg0;
158 }
159 }