KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.namespace.QName JavaDoc;
10
11 import org.hibernate.Hibernate;
12 import org.hibernate.HibernateException;
13 import org.hibernate.usertype.UserType;
14
15 /**
16  * @author Juan Cantu
17  * @version $Revision: 1.1 $ $Date: 2005/06/23 02:22:57 $
18  */

19 public class QNameType implements UserType {
20   
21   private static final int[] SQL_TYPES = new int[] {Types.VARCHAR, Types.VARCHAR};
22   public int[] sqlTypes() {
23     return SQL_TYPES;
24   }
25
26   public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
27     return (x == null) ? (y == null) : ((QName JavaDoc)x).equals(y);
28   }
29
30   public boolean isMutable() {
31     return false;
32   }
33   
34   public Class JavaDoc returnedClass() {
35     return QName JavaDoc.class;
36   }
37   
38   public int hashCode(Object JavaDoc x) throws HibernateException {
39     return x.hashCode();
40   }
41
42   public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
43     return value;
44   }
45
46   public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
47     return (Serializable JavaDoc) value;
48   }
49
50   public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
51     return cached;
52   }
53
54   public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
55     return target;
56   }
57
58   /**
59    *{@inheritDoc}
60    */

61   public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner)
62     throws HibernateException, SQLException JavaDoc {
63     String JavaDoc namespaceURI = (String JavaDoc) Hibernate.STRING.nullSafeGet(rs, names[0]);
64     String JavaDoc localPart = (String JavaDoc) Hibernate.STRING.nullSafeGet(rs, names[1]);
65     return (namespaceURI != null || localPart != null) ? new QName JavaDoc(namespaceURI, localPart) : null;
66   }
67
68   /**
69    *{@inheritDoc}
70    */

71   public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
72     throws HibernateException, SQLException JavaDoc {
73     if(value == null) {
74       st.setNull(index, Types.VARCHAR);
75       st.setNull(index + 1, Types.VARCHAR);
76     }
77     else if (returnedClass().isAssignableFrom(value.getClass())) {
78       QName JavaDoc qname = (QName JavaDoc) value;
79       // set the value into the resultset
80
st.setString(index, qname.getNamespaceURI());
81       st.setString(index + 1, qname.getLocalPart());
82     }
83     else {
84       // the received value is not of the expected type
85
throw new IllegalArgumentException JavaDoc("Received value is not a[" +
86           returnedClass().getName() + "] but [" + value.getClass() + "]");
87     }
88   }
89 }
90
Popular Tags