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
|
|
10
|
package eu.etaxonomy.cdm.hibernate;
|
11
|
|
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;
|
20
|
import java.util.Set;
|
21
|
|
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;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.IKeyTerm;
|
32
|
|
33
|
/**
|
34
|
* User type for EnumSet
|
35
|
* @author a.mueller
|
36
|
* @since 25-02-2019
|
37
|
*/
|
38
|
public class EnumSetUserType<E extends Enum<E>> extends AbstractUserType implements UserType, ParameterizedType {
|
39
|
|
40
|
private static final long serialVersionUID = 1060802925284271666L;
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = Logger.getLogger(EnumSetUserType.class);
|
43
|
|
44
|
private static final String SEP = "#";
|
45
|
|
46
|
|
47
|
private Class<E> clazz = null;
|
48
|
|
49
|
public EnumSetUserType(){}
|
50
|
|
51
|
public EnumSetUserType(Class<E> c) {
|
52
|
this.clazz = c;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
@SuppressWarnings("unchecked")
|
57
|
public void setParameterValues(Properties parameters) {
|
58
|
try {
|
59
|
this.clazz = (Class<E>) Class.forName(parameters.getProperty("enumClass"));
|
60
|
} catch (ClassNotFoundException e) {
|
61
|
throw new RuntimeException(e);
|
62
|
}
|
63
|
}
|
64
|
|
65
|
private static final int[] SQL_TYPES = { Types.VARCHAR };
|
66
|
|
67
|
|
68
|
@Override
|
69
|
public Object deepCopy(Object o) throws HibernateException {
|
70
|
return o;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public Serializable disassemble(Object value) throws HibernateException {
|
75
|
return (Serializable)value;
|
76
|
}
|
77
|
|
78
|
@Override
|
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);
|
82
|
|
83
|
if(val == null) {
|
84
|
return null;
|
85
|
} else {
|
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));
|
91
|
}
|
92
|
}
|
93
|
EnumSet<E> enumSet = EnumSet.copyOf(result);
|
94
|
return enumSet;
|
95
|
}
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
|
100
|
throws HibernateException, SQLException {
|
101
|
if (value == null) {
|
102
|
StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
|
103
|
} else {
|
104
|
@SuppressWarnings("unchecked")
|
105
|
EnumSet<E> enumSet = (EnumSet<E>)value;
|
106
|
String key = "#";
|
107
|
for(Enum<E> e: enumSet){
|
108
|
key += ((IKeyTerm)e).getKey()+"#";
|
109
|
}
|
110
|
StandardBasicTypes.STRING.nullSafeSet(statement, key, index, session);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public Class<E> returnedClass() {
|
116
|
return clazz;
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public int[] sqlTypes() {
|
121
|
return SQL_TYPES;
|
122
|
}
|
123
|
|
124
|
|
125
|
}
|