KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > domain > hibernate > QNameUserType


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.domain.hibernate;
18
19 import java.io.Serializable JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 import org.alfresco.service.namespace.QName;
26 import org.alfresco.util.EqualsHelper;
27 import org.hibernate.HibernateException;
28 import org.hibernate.usertype.UserType;
29
30 /**
31  * Custom type to hide the persistence of {@link org.alfresco.service.namespace.QName qname}
32  * instances.
33  *
34  * @author Derek Hulley
35  */

36 public class QNameUserType implements UserType
37 {
38     private static int[] SQL_TYPES = new int[] {Types.VARCHAR};
39     
40     public Class JavaDoc returnedClass()
41     {
42         return QName.class;
43     }
44
45     /**
46      * @see #SQL_TYPES
47      */

48     public int[] sqlTypes()
49     {
50         return SQL_TYPES;
51     }
52
53     public boolean isMutable()
54     {
55         return false;
56     }
57
58     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException
59     {
60         return EqualsHelper.nullSafeEquals(x, y);
61     }
62
63     public int hashCode(Object JavaDoc x) throws HibernateException
64     {
65         return x.hashCode();
66     }
67
68     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException
69     {
70         // the qname is immutable
71
return value;
72     }
73
74     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc
75     {
76         String JavaDoc qnameStr = rs.getString(names[0]);
77         if (qnameStr == null)
78         {
79             return null;
80         }
81         else
82         {
83             QName qname = QName.createQName(qnameStr);
84             return qname;
85         }
86     }
87
88     public void nullSafeSet(PreparedStatement JavaDoc stmt, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc
89     {
90         // convert the qname to a string
91
stmt.setString(index, value.toString());
92     }
93
94     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException
95     {
96         // qname is immutable
97
return original;
98     }
99
100     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException
101     {
102         // qname is serializable
103
return cached;
104     }
105
106     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException
107     {
108         // qname is serializable
109
return (QName) value;
110     }
111 }
112
Popular Tags