001    /**
002     * Copyright (c) 2000-present 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            @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 (Validator.equals(x, y)) {
056                            return true;
057                    }
058                    else if (((x == null) || x.equals(StringPool.BLANK)) &&
059                                     ((y == null) || y.equals(StringPool.BLANK))) {
060    
061                            return true;
062                    }
063                    else {
064                            return false;
065                    }
066            }
067    
068            @Override
069            public String[] getPropertyNames() {
070                    return new String[0];
071            }
072    
073            @Override
074            public Type[] getPropertyTypes() {
075                    return new Type[] {StandardBasicTypes.STRING};
076            }
077    
078            @Override
079            public Object getPropertyValue(Object component, int property) {
080                    return component;
081            }
082    
083            @Override
084            public int hashCode(Object x) {
085                    return x.hashCode();
086            }
087    
088            @Override
089            public boolean isMutable() {
090                    return false;
091            }
092    
093            @Override
094            public Object nullSafeGet(
095                            ResultSet rs, String[] names, SessionImplementor session,
096                            Object owner)
097                    throws SQLException {
098    
099                    return StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
100            }
101    
102            @Override
103            public void nullSafeSet(
104                            PreparedStatement ps, Object target, int index,
105                            SessionImplementor session)
106                    throws SQLException {
107    
108                    if (target instanceof String) {
109                            String targetString = (String)target;
110    
111                            if (targetString.isEmpty()) {
112                                    target = null;
113                            }
114                    }
115    
116                    StandardBasicTypes.STRING.nullSafeSet(ps, target, index, session);
117            }
118    
119            @Override
120            public Object replace(
121                    Object original, Object target, SessionImplementor session,
122                    Object owner) {
123    
124                    return original;
125            }
126    
127            @Override
128            public Class<String> returnedClass() {
129                    return String.class;
130            }
131    
132            @Override
133            public void setPropertyValue(Object component, int property, Object value) {
134            }
135    
136    }