Project

General

Profile

Download (5.32 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.OriginalSourceType;
28
import eu.etaxonomy.cdm.model.media.ExternalLinkType;
29
import eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName;
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.name.RegistrationStatus;
34
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
35
import eu.etaxonomy.cdm.model.reference.AuthorityType;
36
import eu.etaxonomy.cdm.model.reference.ReferenceType;
37
import eu.etaxonomy.cdm.model.term.IKeyTerm;
38
import eu.etaxonomy.cdm.model.term.TermType;
39

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

    
51
	private Class<E> clazz = null;
52

    
53
	public EnumUserType(){}
54

    
55
    public EnumUserType(Class<E> c) {
56
    	this.clazz = c;
57
    }
58

    
59

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

    
70
	private static final int[] SQL_TYPES = { Types.VARCHAR };
71

    
72

    
73
	@Override
74
	public Object deepCopy(Object o) throws HibernateException {
75
		return o;
76
	}
77

    
78
	@Override
79
	public Serializable disassemble(Object value) throws HibernateException {
80
	    return (Serializable)value;
81
	}
82

    
83
	@Override
84
	public IKeyTerm nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
85
			throws HibernateException, SQLException {
86
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
87

    
88
		if(val == null) {
89
			return null;
90
		} else {
91

    
92
		    return getTerm(clazz, val);
93
		}
94
	}
95

    
96
    /**
97
     * @param val
98
     * @return
99
     */
100
    public static <E extends Enum<E>> IKeyTerm getTerm(Class<E> clazz, String val) {
101
        // TermType
102
        if (clazz.equals(TermType.class)){
103
        	return TermType.getByKey(val);
104
        //Reference Type
105
        }else if (clazz.equals(ReferenceType.class)){
106
        		return ReferenceType.getByKey(val);
107
        //OriginalSourceType
108
        }else if (clazz.equals(OriginalSourceType.class)){
109
        	return OriginalSourceType.getByKey(val);
110
        //NomenclaturalCode
111
        }else if (clazz.equals(NomenclaturalCode.class)){
112
        	return NomenclaturalCode.getByKey(val);
113
        //RankClass
114
        }else if (clazz.equals(RankClass.class)){
115
        	return RankClass.getByKey(val);
116
        //SpecimenOrObservationType
117
        }else if (clazz.equals(SpecimenOrObservationType.class)){
118
        	return SpecimenOrObservationType.getByKey(val);
119
        //SequenceDirection
120
        }else if (clazz.equals(SequenceDirection.class)){
121
        	return SequenceDirection.getByKey(val);
122
        //RegistrationStatus
123
        }else if (clazz.equals(RegistrationStatus.class)){
124
            return RegistrationStatus.getByKey(val);
125
        //CdmMetaDataPropertyName
126
        }else if (clazz.equals(CdmMetaDataPropertyName.class)){
127
            return CdmMetaDataPropertyName.getByKey(val);
128
        //EntityAuthority
129
        }else if (clazz.equals(AuthorityType.class)){
130
            return AuthorityType.getByKey(val);
131
        //ExternalLinkType
132
        }else if (clazz.equals(ExternalLinkType.class)){
133
            return ExternalLinkType.getByKey(val);
134
        }else{
135
        	throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
136
        }
137
    }
138

    
139
	@Override
140
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
141
			throws HibernateException, SQLException {
142
		if (value == null) {
143
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
144
        } else {
145
        	IKeyTerm term = (IKeyTerm)value;
146
            StandardBasicTypes.STRING.nullSafeSet(statement, term.getKey(), index, session);
147
        }
148
	}
149

    
150
	@Override
151
	public Class<E> returnedClass() {
152
		return clazz;
153
	}
154

    
155
	@Override
156
	public int[] sqlTypes() {
157
		return SQL_TYPES;
158
	}
159

    
160

    
161
}
(4-4/12)