package org.splat.dal.bo.kernel;
+
/**
*
* @author Daniel Brunier-Coulin
* @copyright OPEN CASCADE 2012
*/
-
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
-
+
import org.hibernate.HibernateException;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.ParameterizedType;
+import org.splat.common.Constants;
-
+/**
+ * Base class for persistent enumerations. <BR>
+ * Parameter enumClassName defines the enumeration implementation class in <BR>
+ * hibernate mapping like this:<BR>
+ * <code>
+ * <typedef name="ProgressState" class="<i>org.splat.dal.bo.kernel.GenericEnumType</i>"><BR>
+ * <param name="<i>enumClassName</i>">org.splat.dal.bo.som.ProgressState</param><BR>
+ * </typedef><BR>
+ * </code>
+ */
public class GenericEnumType implements EnhancedUserType, ParameterizedType {
-
- @SuppressWarnings("unchecked")
- private Class<Enum> enumClass;
-
- @SuppressWarnings("unchecked")
- public void setParameterValues (Properties parameters) {
-// ------------------------------------------------------
- String enumClassName = parameters.getProperty("enumClassName");
- try {
- enumClass = (Class<Enum>) Class.forName(enumClassName);
- }
- catch (ClassNotFoundException cnfe) {
- throw new HibernateException("Enum class not found", cnfe);
- }
- }
-
- public Object assemble (Serializable cached, Object owner) throws HibernateException {
-// ----------------------------------------------------------
- return cached;
- }
-
- public Object deepCopy (Object value) throws HibernateException {
-// -------------------------------------
- return value;
- }
-
- @SuppressWarnings("unchecked")
- public Serializable disassemble (Object value) throws HibernateException {
-// ----------------------------------------------
- return (Enum) value;
- }
-
- public boolean equals (Object x, Object y) throws HibernateException {
-// ------------------------------------------
- return x==y;
- }
-
- public int hashCode (Object x) throws HibernateException {
-// ------------------------------
- return x.hashCode();
- }
-
- public boolean isMutable () {
-// ---------------------------
- return false;
- }
-
- @SuppressWarnings("unchecked")
- public Object nullSafeGet (ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
-// ----------------------------------------------------------------------
- String name = rs.getString( names[0] );
- return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
- }
-
- @SuppressWarnings("unchecked")
- public void nullSafeSet (PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
-// -----------------------------------------------------------------------
- if (value==null) {
- 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;
- }
-
- @SuppressWarnings("unchecked")
- public Class returnedClass () {
-// -----------------------------
- return enumClass;
- }
-
- public int[] sqlTypes () {
-// ------------------------
- return new int[] { Types.VARCHAR };
- }
-
- @SuppressWarnings("unchecked")
- public Object fromXMLString (String xmlValue) {
-// ---------------------------------------------
- return Enum.valueOf(enumClass, xmlValue);
- }
-
- @SuppressWarnings("unchecked")
- public String objectToSQLString (Object value) {
-// ----------------------------------------------
- return '\'' + ( (Enum) value ).name() + '\'';
- }
-
- @SuppressWarnings("unchecked")
- public String toXMLString (Object value) {
-// ----------------------------------------
- return ( (Enum) value ).name();
- }
-
+ /**
+ * Enumeration implementation class.
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ private transient Class<Enum> _enumClass;
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.ParameterizedType#setParameterValues(java.util.Properties)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public void setParameterValues(final Properties parameters) {
+ String enumClassName = parameters.getProperty("enumClassName");
+ try {
+ _enumClass = (Class<Enum>) Class.forName(enumClassName);
+ } catch (ClassNotFoundException cnfe) {
+ throw new HibernateException("Enum class not found", cnfe);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
+ */
+ public Object assemble(final Serializable cached, final Object owner)
+ throws HibernateException {
+ return cached;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
+ */
+ public Object deepCopy(final Object value) throws HibernateException {
+ return value;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public Serializable disassemble(final Object value)
+ throws HibernateException {
+ return (Enum) value;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
+ */
+ public boolean equals(final Object x, final Object y)
+ throws HibernateException {
+ return x.equals(y);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
+ */
+ public int hashCode(final Object x) throws HibernateException {
+ return x.hashCode();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#isMutable()
+ */
+ public boolean isMutable() {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public Object nullSafeGet(final ResultSet rs, final String[] names,
+ final Object owner) throws HibernateException, SQLException {
+ Object res = null;
+ if (!rs.wasNull()) {
+ String name = rs.getString(names[0]);
+ res = Enum.valueOf(_enumClass, name);
+ }
+ return res;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public void nullSafeSet(final PreparedStatement st, final Object value,
+ final int index) throws HibernateException, SQLException {
+ if (value == null) {
+ st.setNull(index, Types.VARCHAR);
+ } else {
+ st.setString(index, ((Enum) value).name());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
+ */
+ public Object replace(final Object original, final Object target,
+ final Object owner) throws HibernateException {
+ return original;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#returnedClass()
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public Class returnedClass() {
+ return _enumClass;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.UserType#sqlTypes()
+ */
+ public int[] sqlTypes() {
+ return new int[] { Types.VARCHAR };
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.EnhancedUserType#fromXMLString(java.lang.String)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public Object fromXMLString(final String xmlValue) {
+ return Enum.valueOf(_enumClass, xmlValue);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.EnhancedUserType#objectToSQLString(java.lang.Object)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public String objectToSQLString(final Object value) {
+ return '\'' + ((Enum) value).name() + '\'';
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.hibernate.usertype.EnhancedUserType#toXMLString(java.lang.Object)
+ */
+ @SuppressWarnings(Constants.UNCHECKED)
+ public String toXMLString(final Object value) {
+ return ((Enum) value).name();
+ }
}
\ No newline at end of file