KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > hibernate > ScopeStateType


1 package org.jbpm.bpel.hibernate;
2
3 import java.io.Serializable JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Types JavaDoc;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.HibernateException;
11 import org.hibernate.usertype.UserType;
12
13 import org.jbpm.bpel.exe.state.ScopeState;
14
15 /**
16  * Maps a commons-lang <code>Enum</code> to a Hibernate type.
17  * @author Juan Cantu
18  * @version $Revision: 1.1 $ $Date: 2005/06/23 02:22:57 $
19  */

20 public class ScopeStateType implements UserType {
21
22   static final int[] SQLTYPES = new int[]{Types.SMALLINT};
23   
24   public ScopeStateType() {
25   }
26
27   public boolean equals(Object JavaDoc o1, Object JavaDoc o2) { return (o1==o2); }
28   public int hashCode(Object JavaDoc o) throws HibernateException { return o.hashCode(); }
29   public Object JavaDoc deepCopy(Object JavaDoc o) throws HibernateException { return o; }
30   public boolean isMutable() { return false; }
31   public Serializable JavaDoc disassemble(Object JavaDoc o) throws HibernateException { return (Serializable JavaDoc) o; }
32   public Object JavaDoc assemble(Serializable JavaDoc s, Object JavaDoc o) throws HibernateException { return s; }
33   public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) { return target; }
34   public int[] sqlTypes() { return SQLTYPES; }
35
36   /**{@inheritDoc}*/
37   public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner)
38     throws HibernateException, SQLException JavaDoc {
39     int enumCode = ((Integer JavaDoc) Hibernate.INTEGER.nullSafeGet(rs, names[0])).intValue();
40
41     return fromInt(enumCode);
42   }
43
44   /**{@inheritDoc}*/
45   public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
46     throws HibernateException, SQLException JavaDoc {
47     // make sure the received value is of the right type
48
if ((value != null) && !returnedClass().isAssignableFrom(value.getClass())) {
49       throw new IllegalArgumentException JavaDoc("Received value is not a[" +
50         returnedClass().getName() + "] but [" + value.getClass() + "]");
51     }
52
53     // set the value into the resultset
54
st.setInt(index, toInt(value));
55   }
56
57   /**{@inheritDoc}*/
58   public Class JavaDoc returnedClass() {
59     return ScopeState.class;
60   }
61
62   public int toInt(Object JavaDoc object) {
63     return ((ScopeState) object).toInt();
64   }
65
66   public Object JavaDoc fromInt(int code) {
67     return ScopeState.fromInt(code);
68   }
69 }
70
Popular Tags