Project

General

Profile

Download (4.25 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.Properties;
18

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

    
27
import eu.etaxonomy.cdm.model.common.IKeyTerm;
28
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
29
import eu.etaxonomy.cdm.model.common.TermType;
30
import eu.etaxonomy.cdm.model.molecular.SequenceDirection;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
32
import eu.etaxonomy.cdm.model.name.RankClass;
33
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
34
import eu.etaxonomy.cdm.model.reference.ReferenceType;
35

    
36
/**
37
 * User type for IEnumTerm
38
 * Partly copied from http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar
39
 * @author a.mueller
40
 * @created 15-07-2013
41
 */
42
public class EnumUserType<E extends Enum<E>>  extends AbstractUserType implements UserType, ParameterizedType {
43
	private static final long serialVersionUID = 4641078915907621907L;
44
	@SuppressWarnings("unused")
45
	private static final Logger logger = Logger.getLogger(EnumUserType.class);
46

    
47
	private Class<E> clazz = null;
48

    
49
	public EnumUserType(){}
50

    
51
    public EnumUserType(Class<E> c) {
52
    	this.clazz = c;
53
    }
54

    
55

    
56
	@Override
57
	@SuppressWarnings("unchecked")
58
	public void setParameterValues(Properties parameters) {
59
		try {
60
			this.clazz = (Class<E>) Class.forName(parameters.getProperty("enumClass"));
61
		} catch (ClassNotFoundException e) {
62
			throw new RuntimeException(e);
63
		}
64
	}
65

    
66
	private static final int[] SQL_TYPES = { Types.VARCHAR };
67

    
68

    
69
	@Override
70
	public Object deepCopy(Object o) throws HibernateException {
71
		return o;
72
	}
73

    
74
	@Override
75
	public Serializable disassemble(Object value) throws HibernateException {
76
	    return (Serializable)value;
77
	}
78

    
79
	@Override
80
	public IKeyTerm nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
81
			throws HibernateException, SQLException {
82
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
83

    
84
		if(val == null) {
85
			return null;
86
		} else {
87
			// TermType
88
			if (clazz.equals(TermType.class)){
89
				return TermType.getByKey(val);
90
			//Reference Type
91
			}else if (clazz.equals(ReferenceType.class)){
92
					return ReferenceType.getByKey(val);
93
			//OriginalSourceType
94
			}else if (clazz.equals(OriginalSourceType.class)){
95
				return OriginalSourceType.getByKey(val);
96
			//NomenclaturalCode
97
			}else if (clazz.equals(NomenclaturalCode.class)){
98
				return NomenclaturalCode.getByKey(val);
99
			//RankClass
100
			}else if (clazz.equals(RankClass.class)){
101
				return RankClass.getByKey(val);
102
			//SpecimenOrObservationType
103
			}else if (clazz.equals(SpecimenOrObservationType.class)){
104
				return SpecimenOrObservationType.getByKey(val);
105
			//SequenceDirection
106
			}else if (clazz.equals(SequenceDirection.class)){
107
				return SequenceDirection.getByKey(val);
108
			}else{
109
	        	throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
110
	        }
111
		}
112
	}
113

    
114
	@Override
115
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
116
			throws HibernateException, SQLException {
117
		if (value == null) {
118
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
119
        } else {
120
        	IKeyTerm term = (IKeyTerm)value;
121
            StandardBasicTypes.STRING.nullSafeSet(statement, term.getKey(), index, session);
122
        }
123
	}
124

    
125
	@Override
126
	public Class<E> returnedClass() {
127
		return clazz;
128
	}
129

    
130
	@Override
131
	public int[] sqlTypes() {
132
		return SQL_TYPES;
133
	}
134

    
135

    
136
}
(3-3/11)