001    /**
002     * Copyright (c) 2000-2013 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            @Override
038            public Object assemble(
039                    Serializable cached, SessionImplementor session, Object owner) {
040    
041                    return cached;
042            }
043    
044            @Override
045            public Object deepCopy(Object obj) {
046                    return obj;
047            }
048    
049            @Override
050            public Serializable disassemble(Object value, SessionImplementor session) {
051                    return (Serializable)value;
052            }
053    
054            @Override
055            public boolean equals(Object x, Object y) {
056                    if (x == y) {
057                            return true;
058                    }
059                    else if ((x == null) || (y == null)) {
060                            return false;
061                    }
062                    else {
063                            return x.equals(y);
064                    }
065            }
066    
067            @Override
068            public String[] getPropertyNames() {
069                    return new String[0];
070            }
071    
072            @Override
073            public Type[] getPropertyTypes() {
074                    return new Type[] {StandardBasicTypes.LONG};
075            }
076    
077            @Override
078            public Object getPropertyValue(Object component, int property) {
079                    return component;
080            }
081    
082            @Override
083            public int hashCode(Object x) {
084                    return x.hashCode();
085            }
086    
087            @Override
088            public boolean isMutable() {
089                    return false;
090            }
091    
092            @Override
093            public Object nullSafeGet(
094                            ResultSet rs, String[] names, SessionImplementor session,
095                            Object owner)
096                    throws SQLException {
097    
098                    Object value = null;
099    
100                    try {
101                            value = StandardBasicTypes.LONG.nullSafeGet(rs, names[0], session);
102                    }
103                    catch (SQLException sqle1) {
104    
105                            // Some JDBC drivers do not know how to convert a VARCHAR column
106                            // with a blank entry into a BIGINT
107    
108                            try {
109                                    value = new Long(
110                                            GetterUtil.getLong(
111                                                    StandardBasicTypes.STRING.nullSafeGet(
112                                                            rs, names[0], session)));
113                            }
114                            catch (SQLException sqle2) {
115                                    throw sqle1;
116                            }
117                    }
118    
119                    if (value == null) {
120                            return DEFAULT_VALUE;
121                    }
122                    else {
123                            return value;
124                    }
125            }
126    
127            @Override
128            public void nullSafeSet(
129                            PreparedStatement ps, Object target, int index,
130                            SessionImplementor session)
131                    throws SQLException {
132    
133                    if (target == null) {
134                            target = DEFAULT_VALUE;
135                    }
136    
137                    ps.setLong(index, (Long)target);
138            }
139    
140            @Override
141            public Object replace(
142                    Object original, Object target, SessionImplementor session,
143                    Object owner) {
144    
145                    return original;
146            }
147    
148            @Override
149            public Class<Long> returnedClass() {
150                    return Long.class;
151            }
152    
153            @Override
154            public void setPropertyValue(Object component, int property, Object value) {
155            }
156    
157    }