rsanchez
2014-11-18 fdbc8ca146b8e3aff0425e2faf94c0b4a6e3dd28
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
package net.curisit.securis.db.common;
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.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
public abstract class PersistentEnumUserType<T extends CodedEnum> implements UserType {
    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }
    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }
    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        return x == y;
    }
    @Override
    public int hashCode(Object x) throws HibernateException {
        return x == null ? 0 : x.hashCode();
    }
    @Override
    public boolean isMutable() {
        return false;
    }
    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }
    @Override
    public abstract Class<T> returnedClass();
    @Override
    public int[] sqlTypes() {
        return new int[] {
            Types.VARCHAR
        };
    }
    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
        String code = rs.getString(names[0]);
        for (CodedEnum en : returnedClass().getEnumConstants()) {
            if (en.getCode().equals(code)) {
                return en;
            }
        }
        return null;
    }
    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
        st.setString(index, value == null ? null : ((CodedEnum) value).getCode());
    }
}