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
.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
;
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
;
40 * User type for IEnumTerm
41 * Partly copied from http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar
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);
50 private Class
<E
> clazz
= null;
52 public EnumUserType(){}
54 public EnumUserType(Class
<E
> c
) {
60 @SuppressWarnings("unchecked")
61 public void setParameterValues(Properties parameters
) {
63 this.clazz
= (Class
<E
>) Class
.forName(parameters
.getProperty("enumClass"));
64 } catch (ClassNotFoundException e
) {
65 throw new RuntimeException(e
);
69 private static final int[] SQL_TYPES
= { Types
.VARCHAR
};
73 public Object
deepCopy(Object o
) throws HibernateException
{
78 public Serializable
disassemble(Object value
) throws HibernateException
{
79 return (Serializable
)value
;
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
);
91 if (clazz
.equals(TermType
.class)){
92 return TermType
.getByKey(val
);
94 }else if (clazz
.equals(ReferenceType
.class)){
95 return ReferenceType
.getByKey(val
);
97 }else if (clazz
.equals(OriginalSourceType
.class)){
98 return OriginalSourceType
.getByKey(val
);
100 }else if (clazz
.equals(NomenclaturalCode
.class)){
101 return NomenclaturalCode
.getByKey(val
);
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
);
109 }else if (clazz
.equals(SequenceDirection
.class)){
110 return SequenceDirection
.getByKey(val
);
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
);
118 }else if (clazz
.equals(AuthorityType
.class)){
119 return AuthorityType
.getByKey(val
);
121 throw new IllegalArgumentException(String
.format("EnumType %s not supported by %s.", clazz
.getSimpleName(), EnumUserType
.class.getSimpleName()));
127 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
, SessionImplementor session
)
128 throws HibernateException
, SQLException
{
130 StandardBasicTypes
.STRING
.nullSafeSet(statement
, value
, index
, session
);
132 IKeyTerm term
= (IKeyTerm
)value
;
133 StandardBasicTypes
.STRING
.nullSafeSet(statement
, term
.getKey(), index
, session
);
138 public Class
<E
> returnedClass() {
143 public int[] sqlTypes() {