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
.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
;
41 * User type for IEnumTerm
42 * Partly copied from http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar
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);
51 private Class
<E
> clazz
= null;
53 public EnumUserType(){}
55 public EnumUserType(Class
<E
> c
) {
61 @SuppressWarnings("unchecked")
62 public void setParameterValues(Properties parameters
) {
64 this.clazz
= (Class
<E
>) Class
.forName(parameters
.getProperty("enumClass"));
65 } catch (ClassNotFoundException e
) {
66 throw new RuntimeException(e
);
70 private static final int[] SQL_TYPES
= { Types
.VARCHAR
};
74 public Object
deepCopy(Object o
) throws HibernateException
{
79 public Serializable
disassemble(Object value
) throws HibernateException
{
80 return (Serializable
)value
;
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
);
92 return getTerm(clazz
, val
);
100 public static <E
extends Enum
<E
>> IKeyTerm
getTerm(Class
<E
> clazz
, String val
) {
102 if (clazz
.equals(TermType
.class)){
103 return TermType
.getByKey(val
);
105 }else if (clazz
.equals(ReferenceType
.class)){
106 return ReferenceType
.getByKey(val
);
108 }else if (clazz
.equals(OriginalSourceType
.class)){
109 return OriginalSourceType
.getByKey(val
);
111 }else if (clazz
.equals(NomenclaturalCode
.class)){
112 return NomenclaturalCode
.getByKey(val
);
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
);
120 }else if (clazz
.equals(SequenceDirection
.class)){
121 return SequenceDirection
.getByKey(val
);
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
);
129 }else if (clazz
.equals(AuthorityType
.class)){
130 return AuthorityType
.getByKey(val
);
132 }else if (clazz
.equals(ExternalLinkType
.class)){
133 return ExternalLinkType
.getByKey(val
);
135 throw new IllegalArgumentException(String
.format("EnumType %s not supported by %s.", clazz
.getSimpleName(), EnumUserType
.class.getSimpleName()));
140 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
, SessionImplementor session
)
141 throws HibernateException
, SQLException
{
143 StandardBasicTypes
.STRING
.nullSafeSet(statement
, value
, index
, session
);
145 IKeyTerm term
= (IKeyTerm
)value
;
146 StandardBasicTypes
.STRING
.nullSafeSet(statement
, term
.getKey(), index
, session
);
151 public Class
<E
> returnedClass() {
156 public int[] sqlTypes() {