Project

General

Profile

Revision 9b7205bb

ID9b7205bbf5401be3402c0dfe15ff3c44d5b89455
Parent 3987e8b8
Child 9d893279

Added by Andreas Müller over 1 year ago

ref #8128 first implementation to support EnumSet hibernate user types (not yet ready)

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/hibernate/EnumSetUserType.java
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
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/hibernate/EnumUserType.java
88 88
		if(val == null) {
89 89
			return null;
90 90
		} else {
91
			// TermType
92
			if (clazz.equals(TermType.class)){
93
				return TermType.getByKey(val);
94
			//Reference Type
95
			}else if (clazz.equals(ReferenceType.class)){
96
					return ReferenceType.getByKey(val);
97
			//OriginalSourceType
98
			}else if (clazz.equals(OriginalSourceType.class)){
99
				return OriginalSourceType.getByKey(val);
100
			//NomenclaturalCode
101
			}else if (clazz.equals(NomenclaturalCode.class)){
102
				return NomenclaturalCode.getByKey(val);
103
			//RankClass
104
			}else if (clazz.equals(RankClass.class)){
105
				return RankClass.getByKey(val);
106
			//SpecimenOrObservationType
107
			}else if (clazz.equals(SpecimenOrObservationType.class)){
108
				return SpecimenOrObservationType.getByKey(val);
109
			//SequenceDirection
110
			}else if (clazz.equals(SequenceDirection.class)){
111
				return SequenceDirection.getByKey(val);
112
            //RegistrationStatus
113
			}else if (clazz.equals(RegistrationStatus.class)){
114
                return RegistrationStatus.getByKey(val);
115
            //CdmMetaDataPropertyName
116
            }else if (clazz.equals(CdmMetaDataPropertyName.class)){
117
                return CdmMetaDataPropertyName.getByKey(val);
118
            //EntityAuthority
119
            }else if (clazz.equals(AuthorityType.class)){
120
                return AuthorityType.getByKey(val);
121
            //ExternalLinkType
122
            }else if (clazz.equals(ExternalLinkType.class)){
123
                return ExternalLinkType.getByKey(val);
124
            }else{
125
	        	throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
126
	        }
91

  
92
		    return getTerm(clazz, val);
127 93
		}
128 94
	}
129 95

  
96
    /**
97
     * @param val
98
     * @return
99
     */
100
    public static <E extends Enum<E>> IKeyTerm getTerm(Class<E> clazz, String val) {
101
        // TermType
102
        if (clazz.equals(TermType.class)){
103
        	return TermType.getByKey(val);
104
        //Reference Type
105
        }else if (clazz.equals(ReferenceType.class)){
106
        		return ReferenceType.getByKey(val);
107
        //OriginalSourceType
108
        }else if (clazz.equals(OriginalSourceType.class)){
109
        	return OriginalSourceType.getByKey(val);
110
        //NomenclaturalCode
111
        }else if (clazz.equals(NomenclaturalCode.class)){
112
        	return NomenclaturalCode.getByKey(val);
113
        //RankClass
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);
119
        //SequenceDirection
120
        }else if (clazz.equals(SequenceDirection.class)){
121
        	return SequenceDirection.getByKey(val);
122
        //RegistrationStatus
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);
128
        //EntityAuthority
129
        }else if (clazz.equals(AuthorityType.class)){
130
            return AuthorityType.getByKey(val);
131
        //ExternalLinkType
132
        }else if (clazz.equals(ExternalLinkType.class)){
133
            return ExternalLinkType.getByKey(val);
134
        }else{
135
        	throw new IllegalArgumentException(String.format("EnumType %s not supported by %s.", clazz.getSimpleName(), EnumUserType.class.getSimpleName()));
136
        }
137
    }
138

  
130 139
	@Override
131 140
	public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
132 141
			throws HibernateException, SQLException {

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)