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
|
package eu.etaxonomy.cdm.hibernate;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.sql.Types;
|
16
|
import java.util.EnumSet;
|
17
|
import java.util.Properties;
|
18
|
|
19
|
import org.apache.commons.lang3.StringUtils;
|
20
|
import org.apache.logging.log4j.LogManager;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;
|
27
|
|
28
|
import eu.etaxonomy.cdm.model.term.IKeyTerm;
|
29
|
|
30
|
/**
|
31
|
* User type for EnumSet
|
32
|
* @author a.mueller
|
33
|
* @since 25-02-2019
|
34
|
*/
|
35
|
public class EnumSetUserType<E extends Enum<E>>
|
36
|
extends AbstractUserType
|
37
|
implements UserType, ParameterizedType {
|
38
|
|
39
|
/*
|
40
|
* For current usage with hibernate Criterion see DescriptionDaoImpl.addDescriptionTypesCriterion()
|
41
|
*/
|
42
|
|
43
|
private static final long serialVersionUID = 1060802925284271666L;
|
44
|
@SuppressWarnings("unused")
|
45
|
private static final Logger logger = LogManager.getLogger(EnumSetUserType.class);
|
46
|
|
47
|
private static final String SEP = "#";
|
48
|
|
49
|
|
50
|
private Class<E> clazz = null;
|
51
|
|
52
|
public EnumSetUserType(){}
|
53
|
|
54
|
public EnumSetUserType(Class<E> c) {
|
55
|
this.clazz = c;
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
@SuppressWarnings("unchecked")
|
60
|
public void setParameterValues(Properties parameters) {
|
61
|
try {
|
62
|
this.clazz = (Class<E>) Class.forName(parameters.getProperty("enumClass"));
|
63
|
} catch (ClassNotFoundException e) {
|
64
|
throw new RuntimeException(e);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
private static final int[] SQL_TYPES = { Types.VARCHAR };
|
69
|
|
70
|
@Override
|
71
|
public Object deepCopy(Object o) throws HibernateException {
|
72
|
return o;
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public Serializable disassemble(Object value) throws HibernateException {
|
77
|
return (Serializable)value;
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public EnumSet<E> nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
|
82
|
throws HibernateException, SQLException {
|
83
|
String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
|
84
|
|
85
|
EnumSet<E> result = EnumSet.noneOf(clazz);
|
86
|
if(val == null) {
|
87
|
return result;
|
88
|
} else {
|
89
|
String[] splits = val.split(SEP);
|
90
|
for (String split:splits){
|
91
|
if (StringUtils.isNotEmpty(split)) {
|
92
|
@SuppressWarnings("unchecked")
|
93
|
E term = (E)EnumUserType.getTerm(clazz, split);
|
94
|
if (term == null){
|
95
|
throw new IllegalArgumentException(split + " is not a valid key value for enumeration " + clazz.getCanonicalName());
|
96
|
}
|
97
|
result.add(term);
|
98
|
}
|
99
|
}
|
100
|
return result;
|
101
|
}
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor session)
|
106
|
throws HibernateException, SQLException {
|
107
|
if (value == null) {
|
108
|
StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
|
109
|
} else {
|
110
|
@SuppressWarnings("unchecked")
|
111
|
EnumSet<E> enumSet = (EnumSet<E>)value;
|
112
|
String key = "#";
|
113
|
for(Enum<E> e: enumSet){
|
114
|
key += ((IKeyTerm)e).getKey()+"#";
|
115
|
}
|
116
|
StandardBasicTypes.STRING.nullSafeSet(statement, key, index, session);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public Class<E> returnedClass() {
|
122
|
return clazz;
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
public int[] sqlTypes() {
|
127
|
return SQL_TYPES;
|
128
|
}
|
129
|
}
|