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 com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    import java.sql.SQLException;
025    
026    import org.hibernate.engine.SessionImplementor;
027    import org.hibernate.type.StandardBasicTypes;
028    import org.hibernate.type.Type;
029    import org.hibernate.usertype.CompositeUserType;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class StringType implements CompositeUserType, Serializable {
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 (Validator.equals(x, y)) {
052                            return true;
053                    }
054                    else if (((x == null) || x.equals(StringPool.BLANK)) &&
055                                     ((y == null) || y.equals(StringPool.BLANK))) {
056    
057                            return true;
058                    }
059                    else {
060                            return false;
061                    }
062            }
063    
064            public String[] getPropertyNames() {
065                    return new String[0];
066            }
067    
068            public Type[] getPropertyTypes() {
069                    return new Type[] {StandardBasicTypes.STRING};
070            }
071    
072            public Object getPropertyValue(Object component, int property) {
073                    return component;
074            }
075    
076            public int hashCode(Object x) {
077                    return x.hashCode();
078            }
079    
080            public boolean isMutable() {
081                    return false;
082            }
083    
084            public Object nullSafeGet(
085                            ResultSet rs, String[] names, SessionImplementor session,
086                            Object owner)
087                    throws SQLException {
088    
089                    return StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
090            }
091    
092            public void nullSafeSet(
093                            PreparedStatement ps, Object target, int index,
094                            SessionImplementor session)
095                    throws SQLException {
096    
097                    StandardBasicTypes.STRING.nullSafeSet(ps, target, index, session);
098            }
099    
100            public Object replace(
101                    Object original, Object target, SessionImplementor session,
102                    Object owner) {
103    
104                    return original;
105            }
106    
107            public Class<String> returnedClass() {
108                    return String.class;
109            }
110    
111            public void setPropertyValue(Object component, int property, Object value) {
112            }
113    
114    }