ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.vci.server.base.persistence.history;
 
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
 
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
 
public class EnumOperationType<E extends Enum<E>> implements UserType {
 
    private Class<E> clazz = null;
    private static final int[] sqlTypes = {Types.VARCHAR};
    
    public EnumOperationType(Class<E> c) {
        clazz = c;
    }
    
    public int[] sqlTypes() {
        return sqlTypes;
    }
    
    public Class returnedClass() {
        return clazz;
    }
    
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) {
            return true;
        }
        if (x == null || y == null) {
            return false;
        } else {
            return x.equals(y);
        }
    }
    
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }
 
    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return cached;
    }
 
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }
 
    public Serializable disassemble(Object value) throws HibernateException {
        // TODO Auto-generated method stub
        return (Serializable)value;
    }
 
    public boolean isMutable() {
        return false;
    }
 
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException {
        String value = rs.getString(names[0]);
        Object result = null;
        if (!rs.wasNull()) {
            result = Enum.valueOf(clazz, value);
        }
        return result;
    }
 
    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
        if (null == value) {
            st.setNull(index, Types.VARCHAR);
        } else {
            st.setString(index, ((Enum)value).name());
        }
    }
 
    public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
        return original;
    }
}