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
.EnumSet
;
18 import java
.util
.HashSet
;
19 import java
.util
.Properties
;
22 import org
.apache
.commons
.lang3
.StringUtils
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.hibernate
.HibernateException
;
25 import org
.hibernate
.engine
.spi
.SessionImplementor
;
26 import org
.hibernate
.type
.StandardBasicTypes
;
27 import org
.hibernate
.usertype
.ParameterizedType
;
28 import org
.hibernate
.usertype
.UserType
;
29 import org
.jadira
.usertype
.dateandtime
.shared
.spi
.AbstractUserType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.IKeyTerm
;
34 * User type for EnumSet
38 public class EnumSetUserType
<E
extends Enum
<E
>> extends AbstractUserType
implements UserType
, ParameterizedType
{
40 private static final long serialVersionUID
= 1060802925284271666L;
41 @SuppressWarnings("unused")
42 private static final Logger logger
= Logger
.getLogger(EnumSetUserType
.class);
44 private static final String SEP
= "#";
47 private Class
<E
> clazz
= null;
49 public EnumSetUserType(){}
51 public EnumSetUserType(Class
<E
> c
) {
56 @SuppressWarnings("unchecked")
57 public void setParameterValues(Properties parameters
) {
59 this.clazz
= (Class
<E
>) Class
.forName(parameters
.getProperty("enumClass"));
60 } catch (ClassNotFoundException e
) {
61 throw new RuntimeException(e
);
65 private static final int[] SQL_TYPES
= { Types
.VARCHAR
};
69 public Object
deepCopy(Object o
) throws HibernateException
{
74 public Serializable
disassemble(Object value
) throws HibernateException
{
75 return (Serializable
)value
;
79 public EnumSet
<E
> nullSafeGet(ResultSet rs
, String
[] names
, SessionImplementor session
, Object owner
)
80 throws HibernateException
, SQLException
{
81 String val
= (String
) StandardBasicTypes
.STRING
.nullSafeGet(rs
, names
, session
, owner
);
86 Set
<E
> result
= new HashSet
<>();
87 String
[] splits
= val
.split(SEP
);
88 for (String split
:splits
){
89 if (StringUtils
.isNotEmpty(split
)) {
90 result
.add((E
)EnumUserType
.getTerm(clazz
, split
));
93 EnumSet
<E
> enumSet
= EnumSet
.copyOf(result
);
99 public void nullSafeSet(PreparedStatement statement
, Object value
, int index
, SessionImplementor session
)
100 throws HibernateException
, SQLException
{
102 StandardBasicTypes
.STRING
.nullSafeSet(statement
, value
, index
, session
);
104 @SuppressWarnings("unchecked")
105 EnumSet
<E
> enumSet
= (EnumSet
<E
>)value
;
107 for(Enum
<E
> e
: enumSet
){
108 key
+= ((IKeyTerm
)e
).getKey()+"#";
110 StandardBasicTypes
.STRING
.nullSafeSet(statement
, key
, index
, session
);
115 public Class
<E
> returnedClass() {
120 public int[] sqlTypes() {