001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.hibernate;
023:
024: import org.hibernate.Hibernate;
025: import org.hibernate.HibernateException;
026: import org.hibernate.usertype.UserType;
027:
028: import javax.xml.namespace.QName;
029: import java.io.Serializable;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033:
034: /**
035: * An hibernate user type to persist the <code>javax.xml.namespace.QName</code> class on two database columns.
036: *
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: */
040: public class QNameUserType implements UserType {
041:
042: /** . */
043: private static final int[] SQL_TYPES = { Hibernate.STRING.sqlType() };
044:
045: public int[] sqlTypes() {
046: return SQL_TYPES;
047: }
048:
049: public Class returnedClass() {
050: return QName.class;
051: }
052:
053: public boolean equals(Object x, Object y) throws HibernateException {
054: if (x == y) {
055: return true;
056: } else if (x == null || y == null) {
057: return false;
058: } else {
059: return x.equals(y);
060: }
061: }
062:
063: public int hashCode(Object x) throws HibernateException {
064: return x.hashCode();
065: }
066:
067: public Object nullSafeGet(ResultSet resultSet, String[] names,
068: Object owner) throws HibernateException, SQLException {
069: // Test nullity
070: String value = resultSet.getString(names[0]);
071: if (resultSet.wasNull()) {
072: return null;
073: }
074:
075: //
076: int pos = value.indexOf(':');
077: if (pos == -1) {
078: return new QName(value);
079: } else {
080: String namespaceURI = value.substring(0, pos);
081: String localPart = value.substring(pos + 1);
082: return new QName(namespaceURI, localPart);
083: }
084: }
085:
086: public void nullSafeSet(PreparedStatement statement, Object value,
087: int index) throws HibernateException, SQLException {
088: if (value == null) {
089: statement.setNull(index, SQL_TYPES[0]);
090: } else {
091: QName qname = (QName) value;
092: statement.setString(index, qname.getNamespaceURI() + ":"
093: + qname.getLocalPart());
094: }
095: }
096:
097: public Object deepCopy(Object value) throws HibernateException {
098: return value;
099: }
100:
101: public boolean isMutable() {
102: return false;
103: }
104:
105: public Serializable disassemble(Object value)
106: throws HibernateException {
107: return (QName) value;
108: }
109:
110: public Object assemble(Serializable cached, Object owner)
111: throws HibernateException {
112: return cached;
113: }
114:
115: public Object replace(Object original, Object target, Object owner)
116: throws HibernateException {
117: return original;
118: }
119: }
|