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    
019    import java.io.Serializable;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    import java.sql.SQLException;
024    
025    import java.util.Objects;
026    
027    import org.hibernate.engine.SessionImplementor;
028    import org.hibernate.type.StandardBasicTypes;
029    import org.hibernate.type.Type;
030    import org.hibernate.usertype.CompositeUserType;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class StringType implements CompositeUserType, Serializable {
036    
037            @Override
038            public Object assemble(
039                    Serializable cached, SessionImplementor session, Object owner) {
040    
041                    return cached;
042            }
043    
044            @Override
045            public Object deepCopy(Object obj) {
046                    return obj;
047            }
048    
049            @Override
050            public Serializable disassemble(Object value, SessionImplementor session) {
051                    return (Serializable)value;
052            }
053    
054            @Override
055            public boolean equals(Object x, Object y) {
056                    if (Objects.equals(x, y)) {
057                            return true;
058                    }
059                    else if (((x == null) || x.equals(StringPool.BLANK)) &&
060                                     ((y == null) || y.equals(StringPool.BLANK))) {
061    
062                            return true;
063                    }
064                    else {
065                            return false;
066                    }
067            }
068    
069            @Override
070            public String[] getPropertyNames() {
071                    return new String[0];
072            }
073    
074            @Override
075            public Type[] getPropertyTypes() {
076                    return new Type[] {StandardBasicTypes.STRING};
077            }
078    
079            @Override
080            public Object getPropertyValue(Object component, int property) {
081                    return component;
082            }
083    
084            @Override
085            public int hashCode(Object x) {
086                    return x.hashCode();
087            }
088    
089            @Override
090            public boolean isMutable() {
091                    return false;
092            }
093    
094            @Override
095            public Object nullSafeGet(
096                            ResultSet rs, String[] names, SessionImplementor session,
097                            Object owner)
098                    throws SQLException {
099    
100                    return StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
101            }
102    
103            @Override
104            public void nullSafeSet(
105                            PreparedStatement ps, Object target, int index,
106                            SessionImplementor session)
107                    throws SQLException {
108    
109                    if (target instanceof String) {
110                            String targetString = (String)target;
111    
112                            if (targetString.isEmpty()) {
113                                    target = null;
114                            }
115                    }
116    
117                    StandardBasicTypes.STRING.nullSafeSet(ps, target, index, session);
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<String> returnedClass() {
130                    return String.class;
131            }
132    
133            @Override
134            public void setPropertyValue(Object component, int property, Object value) {
135            }
136    
137    }