Project

General

Profile

Download (2.73 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

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

    
25
import eu.etaxonomy.cdm.common.DOI;
26

    
27
/**
28
 * Hibernate user type for the {@link DOI} class.
29
 * @author a.mueller
30
 * @created 05.09.2013
31
 */
32
public class DOIUserType  extends AbstractUserType implements UserType {
33
	private static final long serialVersionUID = 2227841000128722278L;
34

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

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

    
40
	@Override
41
	public Object deepCopy(Object o) throws HibernateException {
42
		if (o == null) {
43
            return null;
44
        }
45
		
46
		DOI doi = (DOI) o;
47

    
48
        try {
49
			return DOI.fromString(doi.toString());
50
		} catch (IllegalArgumentException e) {
51
			throw new HibernateException(e);
52
		}
53
	}
54

    
55

    
56
	@Override
57
	public Serializable disassemble(Object value) throws HibernateException {
58
		if(value == null) {
59
			return null;
60
		} else {
61
		    DOI doi = (DOI) value;
62
		    return doi.toString();
63
		}
64
	}
65

    
66
	@Override
67
	public DOI nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) 
68
			throws HibernateException, SQLException {
69
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
70
		
71
		if(val == null) {
72
			return null;
73
		} else {
74

    
75
            try {
76
			    return DOI.fromString(val);
77
		    } catch (IllegalArgumentException e) {
78
			    throw new HibernateException(e);
79
		    }
80
		}
81
	}
82

    
83
	@Override
84
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session) 
85
			throws HibernateException, SQLException {
86
		if (value == null) { 
87
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
88
        } else { 
89
        	DOI doi = (DOI)value;
90
            StandardBasicTypes.STRING.nullSafeSet(statement, doi.toString(), index, session);
91
        }
92
	}
93

    
94
	@Override
95
	public Class returnedClass() {
96
		return DOI.class;
97
	}
98

    
99
	@Override
100
	public int[] sqlTypes() {
101
		return SQL_TYPES;
102
	}
103

    
104
	
105

    
106

    
107

    
108
}
(2-2/11)