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 java.io.Serializable;
018    
019    import java.sql.PreparedStatement;
020    import java.sql.ResultSet;
021    import java.sql.SQLException;
022    
023    import org.hibernate.engine.SessionImplementor;
024    import org.hibernate.type.StandardBasicTypes;
025    import org.hibernate.type.Type;
026    import org.hibernate.usertype.CompositeUserType;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Bruno Farache
031     */
032    public class IntegerType implements CompositeUserType, Serializable {
033    
034            public static final Integer DEFAULT_VALUE = Integer.valueOf(0);
035    
036            public Object assemble(
037                    Serializable cached, SessionImplementor session, Object owner) {
038    
039                    return cached;
040            }
041    
042            public Object deepCopy(Object obj) {
043                    return obj;
044            }
045    
046            public Serializable disassemble(Object value, SessionImplementor session) {
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 String[] getPropertyNames() {
063                    return new String[0];
064            }
065    
066            public Type[] getPropertyTypes() {
067                    return new Type[] {StandardBasicTypes.INTEGER};
068            }
069    
070            public Object getPropertyValue(Object component, int property) {
071                    return component;
072            }
073    
074            public int hashCode(Object x) {
075                    return x.hashCode();
076            }
077    
078            public boolean isMutable() {
079                    return false;
080            }
081    
082            public Object nullSafeGet(
083                    ResultSet rs, String[] names, SessionImplementor session,
084                    Object owner) {
085    
086                    Integer value = null;
087    
088                    try {
089                            value = StandardBasicTypes.INTEGER.nullSafeGet(
090                                    rs, names[0], session);
091                    }
092                    catch (SQLException sqle) {
093                    }
094    
095                    if (value == null) {
096                            return DEFAULT_VALUE;
097                    }
098                    else {
099                            return value;
100                    }
101            }
102    
103            public void nullSafeSet(
104                            PreparedStatement ps, Object target, int index,
105                            SessionImplementor session)
106                    throws SQLException {
107    
108                    if (target == null) {
109                            target = DEFAULT_VALUE;
110                    }
111    
112                    ps.setInt(index, (Integer)target);
113            }
114    
115            public Object replace(
116                    Object original, Object target, SessionImplementor session,
117                    Object owner) {
118    
119                    return original;
120            }
121    
122            public Class<Integer> returnedClass() {
123                    return Integer.class;
124            }
125    
126            public void setPropertyValue(Object component, int property, Object value) {
127            }
128    
129    }