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.Hibernate;
025: import org.hibernate.HibernateException;
026: import org.hibernate.usertype.UserType;
027:
028: import java.io.Serializable;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Types;
033:
034: /**
035: * An hibernate custom type that handles the fact that oracle stores an empty string as null. That types changes the
036: * semantic of string with the fact that a returned null string is equals to an empty string.
037: * <p/>
038: * I did not found a good name for that class so I choosed MagicString but it has nothing magic.
039: *
040: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
041: * @version $Revision: 8784 $
042: */
043: public class MagicString implements UserType, Serializable {
044:
045: private static final int[] TYPES = { Types.VARCHAR };
046:
047: public int[] sqlTypes() {
048: return TYPES;
049: }
050:
051: public Class returnedClass() {
052: return String.class;
053: }
054:
055: public boolean equals(Object x, Object y) {
056: if (x == y) {
057: return true;
058: }
059: if (x == null) {
060: return ((String) y).length() == 0;
061: } else if (y == null) {
062: return ((String) x).length() == 0;
063: }
064: return x.equals(y);
065: }
066:
067: public Object deepCopy(Object value) {
068: return value;
069: }
070:
071: public boolean isMutable() {
072: return false;
073: }
074:
075: public Object nullSafeGet(ResultSet resultSet, String[] names,
076: Object owner) throws HibernateException, SQLException {
077: String value = (String) Hibernate.STRING.nullSafeGet(resultSet,
078: names[0]);
079: if (value != null) {
080: return value;
081: } else {
082: return "";
083: }
084: }
085:
086: public void nullSafeSet(PreparedStatement statement, Object value,
087: int index) throws HibernateException, SQLException {
088: Hibernate.STRING.nullSafeSet(statement, value, index);
089: }
090:
091: public Object assemble(Serializable cached, Object owner)
092: throws HibernateException {
093: return cached;
094: }
095:
096: public Serializable disassemble(Object value) {
097: return (Serializable) value;
098: }
099:
100: public int hashCode(Object object) throws HibernateException {
101: return object.hashCode();
102: }
103:
104: public Object replace(Object original, Object target, Object owner)
105: throws HibernateException {
106: return original;
107: }
108: }
|