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.json.JSONFactoryImpl;
018    import com.liferay.portal.kernel.json.JSONFactory;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    import java.sql.SQLException;
026    
027    import java.util.Map;
028    
029    import org.hibernate.engine.SessionImplementor;
030    import org.hibernate.type.StandardBasicTypes;
031    import org.hibernate.type.Type;
032    import org.hibernate.usertype.CompositeUserType;
033    
034    /**
035     * @author Cristina Gonz??lez
036     */
037    public class MapType implements CompositeUserType, Serializable {
038    
039            @Override
040            public Object assemble(
041                    Serializable cached, SessionImplementor session, Object owner) {
042    
043                    return cached;
044            }
045    
046            @Override
047            public Object deepCopy(Object obj) {
048                    return obj;
049            }
050    
051            @Override
052            public Serializable disassemble(Object value, SessionImplementor session) {
053                    return (Serializable)value;
054            }
055    
056            @Override
057            public boolean equals(Object x, Object y) {
058                    return Validator.equals(x, y);
059            }
060    
061            @Override
062            public String[] getPropertyNames() {
063                    return new String[0];
064            }
065    
066            @Override
067            public Type[] getPropertyTypes() {
068                    return new Type[] {StandardBasicTypes.STRING};
069            }
070    
071            @Override
072            public Object getPropertyValue(Object component, int property) {
073                    return component;
074            }
075    
076            @Override
077            public int hashCode(Object x) {
078                    return x.hashCode();
079            }
080    
081            @Override
082            public boolean isMutable() {
083                    return false;
084            }
085    
086            @Override
087            public Object nullSafeGet(
088                            ResultSet rs, String[] names, SessionImplementor session,
089                            Object owner)
090                    throws SQLException {
091    
092                    String json = (String)StandardBasicTypes.STRING.nullSafeGet(
093                            rs, names, session, owner);
094    
095                    return _jsonFactory.deserialize(json);
096            }
097    
098            @Override
099            public void nullSafeSet(
100                            PreparedStatement ps, Object target, int index,
101                            SessionImplementor session)
102                    throws SQLException {
103    
104                    String json = _jsonFactory.serialize(target);
105    
106                    StandardBasicTypes.STRING.nullSafeSet(ps, json, index, session);
107            }
108    
109            @Override
110            public Object replace(
111                    Object original, Object target, SessionImplementor session,
112                    Object owner) {
113    
114                    return original;
115            }
116    
117            @Override
118            @SuppressWarnings("rawtypes")
119            public Class<Map> returnedClass() {
120                    return Map.class;
121            }
122    
123            @Override
124            public void setPropertyValue(Object component, int property, Object value) {
125            }
126    
127            private static final JSONFactory _jsonFactory = new JSONFactoryImpl();
128    
129    }