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.migration.helper;
023:
024: import org.hibernate.HibernateException;
025: import org.hibernate.usertype.UserType;
026:
027: import java.io.Serializable;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.sql.Types;
032:
033: /**
034: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
035: * @version $Revision: 8784 $
036: */
037: public abstract class StringWrapperUserType implements UserType,
038: Serializable {
039:
040: private static final int[] SQL_TYPES = { Types.VARCHAR };
041:
042: public abstract Object fromString(String name);
043:
044: public abstract String toString(Object value);
045:
046: public int[] sqlTypes() {
047: return SQL_TYPES;
048: }
049:
050: public boolean equals(Object x, Object y) throws HibernateException {
051: if (x == y) {
052: return true;
053: } else if (x == null || y == null) {
054: return false;
055: } else {
056: return x.equals(y);
057: }
058: }
059:
060: public int hashCode(Object object) throws HibernateException {
061: return object.hashCode();
062: }
063:
064: public Object nullSafeGet(ResultSet resultSet, String[] names,
065: Object owner) throws HibernateException, SQLException {
066: if (resultSet.wasNull()) {
067: return null;
068: }
069: String value = resultSet.getString(names[0]);
070: return fromString(value);
071: }
072:
073: public void nullSafeSet(PreparedStatement statement, Object value,
074: int index) throws HibernateException, SQLException {
075: if (value == null) {
076: statement.setNull(index, Types.VARCHAR);
077: } else {
078: String name = toString(value);
079: statement.setString(index, name);
080: }
081: }
082:
083: public Object deepCopy(Object value) throws HibernateException {
084: return value;
085: }
086:
087: public boolean isMutable() {
088: return false;
089: }
090:
091: public Serializable disassemble(Object value)
092: throws HibernateException {
093: return (Serializable) value;
094: }
095:
096: public Object assemble(Serializable cached, Object owner)
097: throws HibernateException {
098: return cached;
099: }
100:
101: public Object replace(Object original, Object target, Object owner)
102: throws HibernateException {
103: return original;
104: }
105: }
|