Project

General

Profile

Download (2.74 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
package eu.etaxonomy.cdm.hibernate;
10

    
11
import java.io.Serializable;
12
import java.sql.PreparedStatement;
13
import java.sql.ResultSet;
14
import java.sql.SQLException;
15
import java.sql.Types;
16
import java.util.UUID;
17

    
18
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
19
import org.hibernate.HibernateException;
20
import org.hibernate.engine.spi.SharedSessionContractImplementor;
21
import org.hibernate.type.StandardBasicTypes;
22
import org.hibernate.usertype.UserType;
23
import org.jadira.usertype.spi.shared.AbstractUserType;
24

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

    
32
	@SuppressWarnings("unused")
33
	private static final Logger logger = LogManager.getLogger(UUIDUserType.class);
34

    
35
	private static final int[] SQL_TYPES = { Types.VARCHAR };
36

    
37
	@Override
38
    public Object deepCopy(Object o) throws HibernateException {
39
		if (o == null) {
40
            return null;
41
        }
42

    
43
		UUID uuid = (UUID) o;
44

    
45
        try {
46
			return UUID.fromString(uuid.toString());
47
		} catch (IllegalArgumentException e) {
48
			throw new HibernateException(e);
49
		}
50
	}
51

    
52
	@Override
53
    public Serializable disassemble(Object value) throws HibernateException {
54
		if(value == null) {
55
			return null;
56
		} else {
57
		    UUID uuid = (UUID) value;
58
		    return uuid.toString();
59
		}
60
	}
61

    
62
	@Override
63
	public UUID nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
64
			throws HibernateException, SQLException {
65
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
66

    
67
		if(val == null) {
68
			return null;
69
		} else {
70

    
71
            try {
72
			    return UUID.fromString(val);
73
		    } catch (IllegalArgumentException e) {
74
			    throw new HibernateException(e);
75
		    }
76
		}
77
	}
78

    
79
	@Override
80
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor session)
81
			throws HibernateException, SQLException {
82
		if (value == null) {
83
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
84
        } else {
85
         	UUID uuid = (UUID)value;
86
            StandardBasicTypes.STRING.nullSafeSet(statement, uuid.toString(), index, session);
87
        }
88
	}
89

    
90
	@Override
91
	public Class<?> returnedClass() {
92
		return UUID.class;
93
	}
94

    
95
	@Override
96
	public int[] sqlTypes() {
97
		return SQL_TYPES;
98
	}
99
}
(12-12/13)