2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.hibernate
;
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
;
19 import org
.apache
.logging
.log4j
.LogManager
;
20 import org
.apache
.logging
.log4j
.Logger
;
21 import org
.hibernate
.HibernateException
;
22 import org
.hibernate
.engine
.spi
.SharedSessionContractImplementor
;
23 import org
.hibernate
.type
.StandardBasicTypes
;
24 import org
.hibernate
.usertype
.ParameterizedType
;
25 import org
.hibernate
.usertype
.UserType
;
26 import org
.jadira
.usertype
.spi
.shared
.AbstractUserType
;
28 import eu
.etaxonomy
.cdm
.model
.common
.AuthorityType
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmClass
;
30 import eu
.etaxonomy
.cdm
.model
.common
.ExternallyManagedImport
;
31 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionType
;
32 import eu
.etaxonomy
.cdm
.model
.media
.ExternalLinkType
;
33 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaDataPropertyName
;
34 import eu
.etaxonomy
.cdm
.model
.molecular
.SequenceDirection
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
36 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCodeEdition
;
37 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStanding
;
38 import eu
.etaxonomy
.cdm
.model
.name
.RankClass
;
39 import eu
.etaxonomy
.cdm
.model
.name
.RegistrationStatus
;
40 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
41 import eu
.etaxonomy
.cdm
.model
.permission
.CRUD
;
42 import eu
.etaxonomy
.cdm
.model
.permission
.PermissionClass
;
43 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.ConceptDefinition
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.ConceptStatus
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymType
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeStatus
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonType
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicOperationType
;
51 import eu
.etaxonomy
.cdm
.model
.term
.IKeyTerm
;
52 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
55 * User type for IEnumTerm
56 * Partly copied from http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar
60 public class EnumUserType
<E
extends Enum
<E
>>
61 extends AbstractUserType
62 implements UserType
, ParameterizedType
{
64 private static final long serialVersionUID
= 4641078915907621907L;
65 @SuppressWarnings("unused")
66 private static final Logger logger
= LogManager
.getLogger();
68 private static final int[] SQL_TYPES
= { Types
.VARCHAR
};
70 private Class
<E
> clazz
= null;
72 public EnumUserType(){}
74 public EnumUserType(Class
<E
> c
) {
79 @SuppressWarnings("unchecked")
80 public void setParameterValues(Properties parameters
) {
82 this.clazz
= (Class
<E
>) Class
.forName(parameters
.getProperty("enumClass"));
83 } catch (ClassNotFoundException e
) {
84 throw new RuntimeException(e
);
89 public Object
deepCopy(Object o
) throws HibernateException
{
94 public Serializable
disassemble(Object value
) throws HibernateException
{
95 return (Serializable
)value
;
99 public IKeyTerm
nullSafeGet(ResultSet rs
, String
[] names
, SharedSessionContractImplementor session
, Object owner
)
100 throws HibernateException
, SQLException
{
101 String val
= (String
) StandardBasicTypes
.STRING
.nullSafeGet(rs
, names
, session
, owner
);
107 return getTerm(clazz
, val
);
111 public static <E
extends Enum
<E
>> IKeyTerm
getTerm(Class
<E
> clazz
, String val
) {
113 if (clazz
.equals(TermType
.class)){
114 return TermType
.getByKey(val
);
116 }else if (clazz
.equals(ReferenceType
.class)){
117 return ReferenceType
.getByKey(val
);
119 }else if (clazz
.equals(OriginalSourceType
.class)){
120 return OriginalSourceType
.getByKey(val
);
122 }else if (clazz
.equals(NomenclaturalCode
.class)){
123 return NomenclaturalCode
.getByKey(val
);
125 }else if (clazz
.equals(NomenclaturalCodeEdition
.class)){
126 return NomenclaturalCodeEdition
.getByKey(val
);
128 }else if (clazz
.equals(RankClass
.class)){
129 return RankClass
.getByKey(val
);
131 }else if (clazz
.equals(SynonymType
.class)){
132 return SynonymType
.getByKey(val
);
133 //SpecimenOrObservationType
134 }else if (clazz
.equals(SpecimenOrObservationType
.class)){
135 return SpecimenOrObservationType
.getByKey(val
);
137 }else if (clazz
.equals(SequenceDirection
.class)){
138 return SequenceDirection
.getByKey(val
);
140 }else if (clazz
.equals(RegistrationStatus
.class)){
141 return RegistrationStatus
.getByKey(val
);
142 //CdmMetaDataPropertyName
143 }else if (clazz
.equals(CdmMetaDataPropertyName
.class)){
144 return CdmMetaDataPropertyName
.getByKey(val
);
146 }else if (clazz
.equals(AuthorityType
.class)){
147 return AuthorityType
.getByKey(val
);
149 }else if (clazz
.equals(ExternalLinkType
.class)){
150 return ExternalLinkType
.getByKey(val
);
152 }else if (clazz
.equals(PermissionClass
.class)){
153 return PermissionClass
.getByKey(val
);
155 }else if (clazz
.equals(CRUD
.class)){
156 return CRUD
.getByKey(val
);
158 }else if (clazz
.equals(TaxonNodeStatus
.class)){
159 return TaxonNodeStatus
.getByKey(val
);
161 }else if (clazz
.equals(DescriptionType
.class)){
162 return DescriptionType
.getByKey(val
);
163 //NomenclaturalStanding
164 }else if (clazz
.equals(NomenclaturalStanding
.class)){
165 return NomenclaturalStanding
.getByKey(val
);
167 }else if (clazz
.equals(CdmClass
.class)){
168 return CdmClass
.getByKey(val
);
170 }else if (clazz
.equals(ConceptDefinition
.class)){
171 return ConceptDefinition
.getByKey(val
);
173 }else if (clazz
.equals(ConceptStatus
.class)){
174 return ConceptStatus
.getByKey(val
);
176 }else if (clazz
.equals(TaxonType
.class)){
177 return TaxonType
.getByKey(val
);
179 }else if (clazz
.equals(TaxonomicOperationType
.class)){
180 return TaxonomicOperationType
.getByKey(val
);
182 }else if (clazz
.equals(ExternallyManagedImport
.class)){
183 return ExternallyManagedImport
.getByKey(val
);
185 }else if (clazz
.equals(AuthorityType
.class)){
186 return AuthorityType
.getByKey(val
);
188 throw new IllegalArgumentException(String
.format("EnumType %s not supported by %s.", clazz
.getSimpleName(), EnumUserType
.class.getSimpleName()));
193 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
, SharedSessionContractImplementor session
)
194 throws HibernateException
, SQLException
{
196 StandardBasicTypes
.STRING
.nullSafeSet(statement
, value
, index
, session
);
198 IKeyTerm term
= (IKeyTerm
)value
;
199 StandardBasicTypes
.STRING
.nullSafeSet(statement
, term
.getKey(), index
, session
);
204 public Class
<E
> returnedClass() {
209 public int[] sqlTypes() {