Project

General

Profile

Download (4.08 KB) Statistics
| Branch: | Tag: | Revision:
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.Hibernate;
21
import org.hibernate.HibernateException;
22
import org.hibernate.usertype.UserType;
23

    
24
/**
25
 * @author a.mueller
26
 * @created 22.07.2008
27
 * @version 1.0
28
 */
29
public class UUIDUserType implements UserType {
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = Logger.getLogger(UUIDUserType.class);
32

    
33
	private static final int[] TYPES = { Types.VARCHAR };
34

    
35
	/* (non-Javadoc)
36
	 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
37
	 */
38
	public Object assemble(Serializable cached, Object owner) throws HibernateException {
39
		try {
40
			if(cached == null) {
41
				return null;
42
			} else {
43
			    return UUID.fromString(cached.toString());
44
			}
45
		} catch (IllegalArgumentException e) {
46
			throw new HibernateException(e);
47
		}
48
	}
49

    
50
	/* (non-Javadoc)
51
	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
52
	 */
53
	public Object deepCopy(Object o) throws HibernateException {
54
		if (o == null) {
55
            return null;
56
        }
57
		
58
		UUID uuid = (UUID) o;
59

    
60
        try {
61
			return UUID.fromString(uuid.toString());
62
		} catch (IllegalArgumentException e) {
63
			throw new HibernateException(e);
64
		}
65
	}
66

    
67
	/* (non-Javadoc)
68
	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
69
	 */
70
	public Serializable disassemble(Object value) throws HibernateException {
71
		if(value == null) {
72
			return null;
73
		} else {
74
		    UUID uuid = (UUID) value;
75
		    return uuid.toString();
76
		}
77
	}
78

    
79
	/* (non-Javadoc)
80
	 * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
81
	 */
82
	public boolean equals(Object x, Object y) throws HibernateException {
83
		return (x == y) || (x != null && y != null && (x.equals(y)));
84
	}
85

    
86
	/* (non-Javadoc)
87
	 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
88
	 */
89
	public int hashCode(Object x) throws HibernateException {
90
		return x.hashCode();
91
	}
92

    
93
	/* (non-Javadoc)
94
	 * @see org.hibernate.usertype.UserType#isMutable()
95
	 */
96
	public boolean isMutable() {
97
		return false;
98
	}
99

    
100
	/* (non-Javadoc)
101
	 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
102
	 */
103
	public Object nullSafeGet(ResultSet resultSet, String[] names, Object o)
104
			throws HibernateException, SQLException {
105
        String val = (String) Hibernate.STRING.nullSafeGet(resultSet, names[0]);
106
		
107
		if(val == null) {
108
			return null;
109
		} else {
110

    
111
            try {
112
			    return UUID.fromString(val);
113
		    } catch (IllegalArgumentException e) {
114
			    throw new HibernateException(e);
115
		    }
116
		}
117
	}
118

    
119
	/* (non-Javadoc)
120
	 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
121
	 */
122
	public void nullSafeSet(PreparedStatement preparedStatement, Object o, int index)
123
			throws HibernateException, SQLException {
124
		if (null == o) { 
125
            preparedStatement.setNull(index, Types.VARCHAR); 
126
        } else { 
127
        	UUID uuid = (UUID)o;
128
            preparedStatement.setString(index, uuid.toString()); 
129
        }
130
	}
131

    
132
	/* (non-Javadoc)
133
	 * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
134
	 */
135
	public Object replace(Object original, Object target, Object owner)
136
			throws HibernateException {
137
		return original;
138
	}
139

    
140
	/* (non-Javadoc)
141
	 * @see org.hibernate.usertype.UserType#returnedClass()
142
	 */
143
	public Class returnedClass() {
144
		return UUID.class;
145
	}
146

    
147
	/* (non-Javadoc)
148
	 * @see org.hibernate.usertype.UserType#sqlTypes()
149
	 */
150
	public int[] sqlTypes() {
151
		return TYPES;
152
	}
153
}
(7-7/8)