Salome HOME
3f3ba07603cb04d17ba1484316f88d9406a008b5
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / GenericEnumType.java
1 package org.splat.dal.bo.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.Serializable;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13 import java.util.Properties;
14  
15 import org.hibernate.HibernateException;
16 import org.hibernate.usertype.EnhancedUserType;
17 import org.hibernate.usertype.ParameterizedType;
18
19
20 public class GenericEnumType implements EnhancedUserType, ParameterizedType {
21         
22     @SuppressWarnings("unchecked")
23         private Class<Enum> enumClass;
24     
25     @SuppressWarnings("unchecked")
26         public void setParameterValues (Properties parameters) {
27 //  ------------------------------------------------------
28         String enumClassName = parameters.getProperty("enumClassName");
29         try {
30             enumClass = (Class<Enum>) Class.forName(enumClassName);
31         }
32         catch (ClassNotFoundException cnfe) {
33             throw new HibernateException("Enum class not found", cnfe);
34         }
35     }
36  
37     public Object assemble (Serializable cached, Object owner) throws HibernateException {
38 //  ----------------------------------------------------------
39         return cached;
40     }
41  
42     public Object deepCopy (Object value) throws HibernateException {
43 //  -------------------------------------
44         return value;
45     }
46  
47     @SuppressWarnings("unchecked")
48         public Serializable disassemble (Object value) throws HibernateException {
49 //  ----------------------------------------------
50         return (Enum) value;
51     }
52  
53     public boolean equals (Object x, Object y) throws HibernateException {
54 //  ------------------------------------------
55         return x==y;
56     }
57  
58     public int hashCode (Object x) throws HibernateException {
59 //  ------------------------------
60         return x.hashCode();
61     }
62  
63     public boolean isMutable () {
64 //  ---------------------------
65         return false;
66     }
67  
68     @SuppressWarnings("unchecked")
69         public Object nullSafeGet (ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
70 //  ----------------------------------------------------------------------
71         String name = rs.getString( names[0] );
72         return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
73     }
74  
75     @SuppressWarnings("unchecked")
76         public void nullSafeSet (PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
77 //  -----------------------------------------------------------------------
78         if (value==null) {
79             st.setNull(index, Types.VARCHAR);
80         }
81         else {
82             st.setString( index, ( (Enum) value ).name() ); 
83         }
84     }
85  
86     public Object replace (Object original, Object target, Object owner) throws HibernateException {
87 //  --------------------------------------------------------------------
88         return original;
89     }
90  
91     @SuppressWarnings("unchecked")
92         public Class returnedClass () {
93 //  -----------------------------
94         return enumClass;
95     }
96  
97     public int[] sqlTypes () {
98 //  ------------------------
99         return new int[] { Types.VARCHAR };
100     }
101  
102     @SuppressWarnings("unchecked")
103         public Object fromXMLString (String xmlValue) {
104 //  ---------------------------------------------
105         return Enum.valueOf(enumClass, xmlValue);
106     }
107  
108     @SuppressWarnings("unchecked")
109         public String objectToSQLString (Object value) {
110 //  ----------------------------------------------
111         return '\'' + ( (Enum) value ).name() + '\'';
112     }
113  
114     @SuppressWarnings("unchecked")
115         public String toXMLString (Object value) {
116 //  ----------------------------------------
117         return ( (Enum) value ).name();
118     }
119  
120
121 }