001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import java.io.Serializable;
018    
019    import java.sql.PreparedStatement;
020    import java.sql.ResultSet;
021    import java.sql.SQLException;
022    import java.sql.Types;
023    
024    import org.hibernate.Hibernate;
025    import org.hibernate.HibernateException;
026    import org.hibernate.usertype.UserType;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Bruno Farache
031     */
032    public class IntegerType implements Serializable, UserType {
033    
034            public static final int DEFAULT_VALUE = 0;
035    
036            public static final int[] SQL_TYPES = new int[] {Types.INTEGER};
037    
038            public Object assemble(Serializable cached, Object owner) {
039                    return cached;
040            }
041    
042            public Object deepCopy(Object obj) {
043                    return obj;
044            }
045    
046            public Serializable disassemble(Object value) {
047                    return (Serializable)value;
048            }
049    
050            public boolean equals(Object x, Object y) {
051                    if (x == y) {
052                            return true;
053                    }
054                    else if (x == null || y == null) {
055                            return false;
056                    }
057                    else {
058                            return x.equals(y);
059                    }
060            }
061    
062            public int hashCode(Object x) {
063                    return x.hashCode();
064            }
065    
066            public boolean isMutable() {
067                    return false;
068            }
069    
070            public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
071                    throws HibernateException {
072    
073                    Integer value = null;
074    
075                    try {
076                            value = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
077                    }
078                    catch (SQLException sqle) {
079                    }
080    
081                    if (value == null) {
082                            return new Integer(DEFAULT_VALUE);
083                    }
084                    else {
085                            return value;
086                    }
087            }
088    
089            public void nullSafeSet(PreparedStatement ps, Object obj, int index)
090                    throws HibernateException, SQLException {
091    
092                    if (obj == null) {
093                            obj = new Integer(DEFAULT_VALUE);
094                    }
095    
096                    Hibernate.INTEGER.nullSafeSet(ps, obj, index);
097            }
098    
099            public Object replace(Object original, Object target, Object owner) {
100                    return original;
101            }
102    
103            public Class<Integer> returnedClass() {
104                    return Integer.class;
105            }
106    
107            public int[] sqlTypes() {
108                    return SQL_TYPES;
109            }
110    
111    }