Project

General

Profile

Download (5.08 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.media.ExternalLinkType;
31
import eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName;
32
import eu.etaxonomy.cdm.model.molecular.SequenceDirection;
33
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
34
import eu.etaxonomy.cdm.model.name.RankClass;
35
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
36
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
37
import eu.etaxonomy.cdm.model.reference.AuthorityType;
38
import eu.etaxonomy.cdm.model.reference.ReferenceType;
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
			// TermType
92
			if (clazz.equals(TermType.class)){
93
				return TermType.getByKey(val);
94
			//Reference Type
95
			}else if (clazz.equals(ReferenceType.class)){
96
					return ReferenceType.getByKey(val);
97
			//OriginalSourceType
98
			}else if (clazz.equals(OriginalSourceType.class)){
99
				return OriginalSourceType.getByKey(val);
100
			//NomenclaturalCode
101
			}else if (clazz.equals(NomenclaturalCode.class)){
102
				return NomenclaturalCode.getByKey(val);
103
			//RankClass
104
			}else if (clazz.equals(RankClass.class)){
105
				return RankClass.getByKey(val);
106
			//SpecimenOrObservationType
107
			}else if (clazz.equals(SpecimenOrObservationType.class)){
108
				return SpecimenOrObservationType.getByKey(val);
109
			//SequenceDirection
110
			}else if (clazz.equals(SequenceDirection.class)){
111
				return SequenceDirection.getByKey(val);
112
            //RegistrationStatus
113
			}else if (clazz.equals(RegistrationStatus.class)){
114
                return RegistrationStatus.getByKey(val);
115
            //CdmMetaDataPropertyName
116
            }else if (clazz.equals(CdmMetaDataPropertyName.class)){
117
                return CdmMetaDataPropertyName.getByKey(val);
118
            //EntityAuthority
119
            }else if (clazz.equals(AuthorityType.class)){
120
                return AuthorityType.getByKey(val);
121
              //ExternalLinkType
122
            }else if (clazz.equals(ExternalLinkType.class)){
123
                return ExternalLinkType.getByKey(val);
124
            }else{
125
	        	throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
126
	        }
127
		}
128
	}
129

    
130
	@Override
131
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
132
			throws HibernateException, SQLException {
133
		if (value == null) {
134
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
135
        } else {
136
        	IKeyTerm term = (IKeyTerm)value;
137
            StandardBasicTypes.STRING.nullSafeSet(statement, term.getKey(), index, session);
138
        }
139
	}
140

    
141
	@Override
142
	public Class<E> returnedClass() {
143
		return clazz;
144
	}
145

    
146
	@Override
147
	public int[] sqlTypes() {
148
		return SQL_TYPES;
149
	}
150

    
151

    
152
}
(3-3/11)