Project

General

Profile

Download (2.55 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.net.URISyntaxException;
12
import java.sql.PreparedStatement;
13
import java.sql.ResultSet;
14
import java.sql.SQLException;
15
import java.sql.Types;
16

    
17
import org.hibernate.HibernateException;
18
import org.hibernate.TypeMismatchException;
19
import org.hibernate.engine.spi.SessionImplementor;
20
import org.hibernate.type.StandardBasicTypes;
21
import org.hibernate.usertype.UserType;
22
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
23

    
24
import eu.etaxonomy.cdm.common.URI;
25

    
26
/**
27
 * This class maps eu.etaxonomy.cdm.common.URI to Types.CLOB
28
 *
29
 * @author a.mueller
30
 */
31
public class URIUserType extends AbstractUserType implements UserType {
32
	private static final long serialVersionUID = -5825017496962569105L;
33

    
34
	/**
35
     * SQL type for this usertype.
36
     */
37
    private static final int[] SQL_TYPES = {Types.CLOB};
38

    
39
	@Override
40
	public Class<?> returnedClass() {
41
		return URI.class;
42
	}
43

    
44
	@Override
45
	public int[] sqlTypes() {
46
		return SQL_TYPES;
47
	}
48

    
49
	@Override
50
	public URI nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
51
			throws HibernateException, SQLException {
52
		String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
53

    
54
		if(val == null) {
55
			return null;
56
		} else {
57
            try {
58
			    return new URI(val);
59
		    } catch (URISyntaxException e) {
60
			    throw new TypeMismatchException(e.getMessage());
61
		    }
62
		}
63
    }
64

    
65
	@Override
66
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
67
			throws HibernateException, SQLException {
68
	    if (value == null) {
69
		    StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
70
        } else {
71
            URI uri = (URI) value;
72
            StandardBasicTypes.STRING.nullSafeSet(statement, uri.toString(), index, session);
73
        }
74
	}
75

    
76
    /**
77
     * @param value value being copied
78
     * @return copied value
79
     */
80
    @Override
81
    public Object deepCopy(Object value) {
82
        if (value == null) {
83
            return null;
84
        }
85
        try {
86
            return new URI(((URI) value).toString());
87
        } catch (URISyntaxException e) {
88
            throw new TypeMismatchException(e.getMessage());
89
        }
90
    }
91
}
(11-11/13)