Project

General

Profile

Download (3.18 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.HibernateException;
21
import org.hibernate.engine.spi.SharedSessionContractImplementor;
22
import org.hibernate.type.StandardBasicTypes;
23
import org.hibernate.usertype.UserType;
24
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
25

    
26
/**
27
 * @author a.mueller
28
 * @created 22.07.2008
29
 * @version 2.0
30
 */
31
public class UUIDUserType  extends AbstractUserType implements UserType {
32
	static final long serialVersionUID = -3959049831344758708L;
33

    
34
	@SuppressWarnings("unused")
35
	private static final Logger logger = Logger.getLogger(UUIDUserType.class);
36

    
37
	private static final int[] SQL_TYPES = { Types.VARCHAR };
38

    
39
	/* (non-Javadoc)
40
	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
41
	 */
42
	@Override
43
    public Object deepCopy(Object o) throws HibernateException {
44
		if (o == null) {
45
            return null;
46
        }
47

    
48
		UUID uuid = (UUID) o;
49

    
50
        try {
51
			return UUID.fromString(uuid.toString());
52
		} catch (IllegalArgumentException e) {
53
			throw new HibernateException(e);
54
		}
55
	}
56

    
57
	/* (non-Javadoc)
58
	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
59
	 */
60
	@Override
61
    public Serializable disassemble(Object value) throws HibernateException {
62
		if(value == null) {
63
			return null;
64
		} else {
65
		    UUID uuid = (UUID) value;
66
		    return uuid.toString();
67
		}
68
	}
69

    
70
	@Override
71
	public UUID nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
72
			throws HibernateException, SQLException {
73
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
74

    
75
		if(val == null) {
76
			return null;
77
		} else {
78

    
79
            try {
80
			    return UUID.fromString(val);
81
		    } catch (IllegalArgumentException e) {
82
			    throw new HibernateException(e);
83
		    }
84
		}
85
	}
86

    
87
	@Override
88
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor session)
89
			throws HibernateException, SQLException {
90
		if (value == null) {
91
//            statement.setNull(index, Types.VARCHAR); old version
92
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
93
        } else {
94
         	UUID uuid = (UUID)value;
95
//            statement.setString(index, uuid.toString()); //old version
96
            StandardBasicTypes.STRING.nullSafeSet(statement, uuid.toString(), index, session);
97
        }
98
	}
99

    
100

    
101
	/* (non-Javadoc)
102
	 * @see org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType#returnedClass()
103
	 */
104
	@Override
105
	public Class returnedClass() {
106
		return UUID.class;
107
	}
108

    
109
	@Override
110
	public int[] sqlTypes() {
111
		return SQL_TYPES;
112
	}
113

    
114

    
115

    
116

    
117

    
118
}
(10-10/11)