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 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            @Override
037            public Object assemble(
038                    Serializable cached, SessionImplementor session, Object owner) {
039    
040                    return cached;
041            }
042    
043            @Override
044            public Object deepCopy(Object obj) {
045                    return obj;
046            }
047    
048            @Override
049            public Serializable disassemble(Object value, SessionImplementor session) {
050                    return (Serializable)value;
051            }
052    
053            @Override
054            public boolean equals(Object x, Object y) {
055                    if (x == y) {
056                            return true;
057                    }
058                    else if ((x == null) || (y == null)) {
059                            return false;
060                    }
061                    else {
062                            return x.equals(y);
063                    }
064            }
065    
066            @Override
067            public String[] getPropertyNames() {
068                    return new String[0];
069            }
070    
071            @Override
072            public Type[] getPropertyTypes() {
073                    return new Type[] {StandardBasicTypes.INTEGER};
074            }
075    
076            @Override
077            public Object getPropertyValue(Object component, int property) {
078                    return component;
079            }
080    
081            @Override
082            public int hashCode(Object x) {
083                    return x.hashCode();
084            }
085    
086            @Override
087            public boolean isMutable() {
088                    return false;
089            }
090    
091            @Override
092            public Object nullSafeGet(
093                    ResultSet rs, String[] names, SessionImplementor session,
094                    Object owner) {
095    
096                    Integer value = null;
097    
098                    try {
099                            value = StandardBasicTypes.INTEGER.nullSafeGet(
100                                    rs, names[0], session);
101                    }
102                    catch (SQLException sqle) {
103                    }
104    
105                    if (value == null) {
106                            return DEFAULT_VALUE;
107                    }
108                    else {
109                            return value;
110                    }
111            }
112    
113            @Override
114            public void nullSafeSet(
115                            PreparedStatement ps, Object target, int index,
116                            SessionImplementor session)
117                    throws SQLException {
118    
119                    if (target == null) {
120                            target = DEFAULT_VALUE;
121                    }
122    
123                    ps.setInt(index, (Integer)target);
124            }
125    
126            @Override
127            public Object replace(
128                    Object original, Object target, SessionImplementor session,
129                    Object owner) {
130    
131                    return original;
132            }
133    
134            @Override
135            public Class<Integer> returnedClass() {
136                    return Integer.class;
137            }
138    
139            @Override
140            public void setPropertyValue(Object component, int property, Object value) {
141            }
142    
143    }