Project

General

Profile

Download (3.62 KB) Statistics
| Branch: | Tag: | Revision:
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.Properties;
19

    
20
import org.apache.commons.lang3.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.hibernate.HibernateException;
23
import org.hibernate.engine.spi.SessionImplementor;
24
import org.hibernate.type.StandardBasicTypes;
25
import org.hibernate.usertype.ParameterizedType;
26
import org.hibernate.usertype.UserType;
27
import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
28

    
29
import eu.etaxonomy.cdm.model.term.IKeyTerm;
30

    
31
/**
32
 * User type for EnumSet
33
 * @author a.mueller
34
 * @since 25-02-2019
35
 */
36
public class EnumSetUserType<E extends Enum<E>>
37
        extends AbstractUserType
38
        implements UserType, ParameterizedType {
39

    
40
    /*
41
     * For current usage with hibernate Criterion see DescriptionDaoImpl.addDescriptionTypesCriterion()
42
     */
43

    
44
    private static final long serialVersionUID = 1060802925284271666L;
45
    @SuppressWarnings("unused")
46
	private static final Logger logger = Logger.getLogger(EnumSetUserType.class);
47

    
48
    private static final String SEP = "#";
49

    
50

    
51
	private Class<E> clazz = null;
52

    
53
	public EnumSetUserType(){}
54

    
55
    public EnumSetUserType(Class<E> c) {
56
    	this.clazz = c;
57
    }
58

    
59
	@Override
60
	@SuppressWarnings("unchecked")
61
	public void setParameterValues(Properties parameters) {
62
		try {
63
			this.clazz = (Class<E>) Class.forName(parameters.getProperty("enumClass"));
64
		} catch (ClassNotFoundException e) {
65
			throw new RuntimeException(e);
66
		}
67
	}
68

    
69
	private static final int[] SQL_TYPES = { Types.VARCHAR };
70

    
71
	@Override
72
	public Object deepCopy(Object o) throws HibernateException {
73
		return o;
74
	}
75

    
76
	@Override
77
	public Serializable disassemble(Object value) throws HibernateException {
78
	    return (Serializable)value;
79
	}
80

    
81
	@Override
82
	public EnumSet<E> nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
83
			throws HibernateException, SQLException {
84
        String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
85

    
86
		if(val == null) {
87
			return null;
88
		} else {
89
		    EnumSet<E> result = EnumSet.noneOf(clazz);
90
			String[] splits = val.split(SEP);
91
			for (String split:splits){
92
			    if (StringUtils.isNotEmpty(split)) {
93
			        @SuppressWarnings("unchecked")
94
                    E term = (E)EnumUserType.getTerm(clazz, split);
95
			        if (term == null){
96
			            throw new IllegalArgumentException(split + " is not a valid key value for enumeration " + clazz.getCanonicalName());
97
			        }
98
                    result.add(term);
99
                }
100
			}
101
			return result;
102
		}
103
	}
104

    
105
	@Override
106
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
107
			throws HibernateException, SQLException {
108
		if (value == null) {
109
            StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
110
        } else {
111
        	@SuppressWarnings("unchecked")
112
            EnumSet<E> enumSet = (EnumSet<E>)value;
113
        	String key = "#";
114
        	for(Enum<E> e: enumSet){
115
        	    key += ((IKeyTerm)e).getKey()+"#";
116
        	}
117
            StandardBasicTypes.STRING.nullSafeSet(statement, key, index, session);
118
        }
119
	}
120

    
121
	@Override
122
	public Class<E> returnedClass() {
123
		return clazz;
124
	}
125

    
126
	@Override
127
	public int[] sqlTypes() {
128
		return SQL_TYPES;
129
	}
130
}
(3-3/13)