001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    
019    import java.io.Serializable;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    import java.sql.SQLException;
024    import java.sql.Types;
025    
026    import org.hibernate.Hibernate;
027    import org.hibernate.HibernateException;
028    import org.hibernate.usertype.UserType;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class LongType implements Serializable, UserType {
034    
035            public static final long DEFAULT_VALUE = 0;
036    
037            public static final int[] SQL_TYPES = new int[] {Types.BIGINT};
038    
039            public Object assemble(Serializable cached, Object owner) {
040                    return cached;
041            }
042    
043            public Object deepCopy(Object obj) {
044                    return obj;
045            }
046    
047            public Serializable disassemble(Object value) {
048                    return (Serializable)value;
049            }
050    
051            public boolean equals(Object x, Object y) {
052                    if (x == y) {
053                            return true;
054                    }
055                    else if (x == null || y == null) {
056                            return false;
057                    }
058                    else {
059                            return x.equals(y);
060                    }
061            }
062    
063            public int hashCode(Object x) {
064                    return x.hashCode();
065            }
066    
067            public boolean isMutable() {
068                    return false;
069            }
070    
071            public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
072                    throws HibernateException, SQLException {
073    
074                    Object value = null;
075    
076                    try {
077                            value = Hibernate.LONG.nullSafeGet(rs, names[0]);
078                    }
079                    catch (SQLException sqle1) {
080    
081                            // Some JDBC drivers do not know how to convert a VARCHAR column
082                            // with a blank entry into a BIGINT
083    
084                            try {
085                                    value = new Long(GetterUtil.getLong(
086                                            (String)Hibernate.STRING.nullSafeGet(rs, names[0])));
087                            }
088                            catch (SQLException sqle2) {
089                                    throw sqle1;
090                            }
091                    }
092    
093                    if (value == null) {
094                            return new Long(DEFAULT_VALUE);
095                    }
096                    else {
097                            return value;
098                    }
099            }
100    
101            public void nullSafeSet(PreparedStatement ps, Object obj, int index)
102                    throws HibernateException, SQLException {
103    
104                    if (obj == null) {
105                            obj = new Long(DEFAULT_VALUE);
106                    }
107    
108                    Hibernate.LONG.nullSafeSet(ps, obj, index);
109            }
110    
111            public Object replace(Object original, Object target, Object owner) {
112                    return original;
113            }
114    
115            public Class<Long> returnedClass() {
116                    return Long.class;
117            }
118    
119            public int[] sqlTypes() {
120                    return SQL_TYPES;
121            }
122    
123    }