001    /**
002     * Copyright (c) 2000-2012 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    
025    import org.hibernate.engine.SessionImplementor;
026    import org.hibernate.type.StandardBasicTypes;
027    import org.hibernate.type.Type;
028    import org.hibernate.usertype.CompositeUserType;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class LongType implements CompositeUserType, Serializable {
034    
035            public static final Long DEFAULT_VALUE = Long.valueOf(0);
036    
037            public Object assemble(
038                    Serializable cached, SessionImplementor session, Object owner) {
039    
040                    return cached;
041            }
042    
043            public Object deepCopy(Object obj) {
044                    return obj;
045            }
046    
047            public Serializable disassemble(Object value, SessionImplementor session) {
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 String[] getPropertyNames() {
064                    return new String[0];
065            }
066    
067            public Type[] getPropertyTypes() {
068                    return new Type[] {StandardBasicTypes.LONG};
069            }
070    
071            public Object getPropertyValue(Object component, int property) {
072                    return component;
073            }
074    
075            public int hashCode(Object x) {
076                    return x.hashCode();
077            }
078    
079            public boolean isMutable() {
080                    return false;
081            }
082    
083            public Object nullSafeGet(
084                            ResultSet rs, String[] names, SessionImplementor session,
085                            Object owner)
086                    throws SQLException {
087    
088                    Object value = null;
089    
090                    try {
091                            value = StandardBasicTypes.LONG.nullSafeGet(rs, names[0], session);
092                    }
093                    catch (SQLException sqle1) {
094    
095                            // Some JDBC drivers do not know how to convert a VARCHAR column
096                            // with a blank entry into a BIGINT
097    
098                            try {
099                                    value = new Long(
100                                            GetterUtil.getLong(
101                                                    StandardBasicTypes.STRING.nullSafeGet(
102                                                            rs, names[0], session)));
103                            }
104                            catch (SQLException sqle2) {
105                                    throw sqle1;
106                            }
107                    }
108    
109                    if (value == null) {
110                            return DEFAULT_VALUE;
111                    }
112                    else {
113                            return value;
114                    }
115            }
116    
117            public void nullSafeSet(
118                            PreparedStatement ps, Object target, int index,
119                            SessionImplementor session)
120                    throws SQLException {
121    
122                    if (target == null) {
123                            target = DEFAULT_VALUE;
124                    }
125    
126                    ps.setLong(index, (Long)target);
127            }
128    
129            public Object replace(
130                    Object original, Object target, SessionImplementor session,
131                    Object owner) {
132    
133                    return original;
134            }
135    
136            public Class<Long> returnedClass() {
137                    return Long.class;
138            }
139    
140            public void setPropertyValue(Object component, int property, Object value) {
141            }
142    
143    }