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