Merge branch 'release/4.8.0'
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / EnumUserType.java
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.metadata.CdmMetaDataPropertyName;
31 import eu.etaxonomy.cdm.model.molecular.SequenceDirection;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33 import eu.etaxonomy.cdm.model.name.RankClass;
34 import eu.etaxonomy.cdm.model.name.RegistrationStatus;
35 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
36 import eu.etaxonomy.cdm.model.reference.AuthorityType;
37 import eu.etaxonomy.cdm.model.reference.ReferenceType;
38
39 /**
40 * User type for IEnumTerm
41 * Partly copied from http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar
42 * @author a.mueller
43 * @created 15-07-2013
44 */
45 public class EnumUserType<E extends Enum<E>> extends AbstractUserType implements UserType, ParameterizedType {
46 private static final long serialVersionUID = 4641078915907621907L;
47 @SuppressWarnings("unused")
48 private static final Logger logger = Logger.getLogger(EnumUserType.class);
49
50 private Class<E> clazz = null;
51
52 public EnumUserType(){}
53
54 public EnumUserType(Class<E> c) {
55 this.clazz = c;
56 }
57
58
59 @Override
60 @SuppressWarnings("unchecked")
61 public void setParameterValues(Properties parameters) {
62 try {
63 this.clazz = (Class<E>) Class.forName(parameters.getProperty("enumClass"));
64 } catch (ClassNotFoundException e) {
65 throw new RuntimeException(e);
66 }
67 }
68
69 private static final int[] SQL_TYPES = { Types.VARCHAR };
70
71
72 @Override
73 public Object deepCopy(Object o) throws HibernateException {
74 return o;
75 }
76
77 @Override
78 public Serializable disassemble(Object value) throws HibernateException {
79 return (Serializable)value;
80 }
81
82 @Override
83 public IKeyTerm nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
84 throws HibernateException, SQLException {
85 String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
86
87 if(val == null) {
88 return null;
89 } else {
90 // TermType
91 if (clazz.equals(TermType.class)){
92 return TermType.getByKey(val);
93 //Reference Type
94 }else if (clazz.equals(ReferenceType.class)){
95 return ReferenceType.getByKey(val);
96 //OriginalSourceType
97 }else if (clazz.equals(OriginalSourceType.class)){
98 return OriginalSourceType.getByKey(val);
99 //NomenclaturalCode
100 }else if (clazz.equals(NomenclaturalCode.class)){
101 return NomenclaturalCode.getByKey(val);
102 //RankClass
103 }else if (clazz.equals(RankClass.class)){
104 return RankClass.getByKey(val);
105 //SpecimenOrObservationType
106 }else if (clazz.equals(SpecimenOrObservationType.class)){
107 return SpecimenOrObservationType.getByKey(val);
108 //SequenceDirection
109 }else if (clazz.equals(SequenceDirection.class)){
110 return SequenceDirection.getByKey(val);
111 //RegistrationStatus
112 }else if (clazz.equals(RegistrationStatus.class)){
113 return RegistrationStatus.getByKey(val);
114 //CdmMetaDataPropertyName
115 }else if (clazz.equals(CdmMetaDataPropertyName.class)){
116 return CdmMetaDataPropertyName.getByKey(val);
117 //EntityAuthority
118 }else if (clazz.equals(AuthorityType.class)){
119 return AuthorityType.getByKey(val);
120 }else{
121 throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
122 }
123 }
124 }
125
126 @Override
127 public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
128 throws HibernateException, SQLException {
129 if (value == null) {
130 StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
131 } else {
132 IKeyTerm term = (IKeyTerm)value;
133 StandardBasicTypes.STRING.nullSafeSet(statement, term.getKey(), index, session);
134 }
135 }
136
137 @Override
138 public Class<E> returnedClass() {
139 return clazz;
140 }
141
142 @Override
143 public int[] sqlTypes() {
144 return SQL_TYPES;
145 }
146
147
148 }