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.portlet;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.HashUtil;
021    import com.liferay.portal.model.Portlet;
022    import com.liferay.portal.service.PortletLocalServiceUtil;
023    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.IOException;
027    import java.io.Serializable;
028    
029    import java.util.Collections;
030    import java.util.Map;
031    
032    import javax.portlet.PortletPreferences;
033    import javax.portlet.PreferencesValidator;
034    import javax.portlet.ReadOnlyException;
035    import javax.portlet.ValidatorException;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Alexander Chow
040     */
041    public class PortletPreferencesImpl
042            extends BasePreferencesImpl
043            implements Cloneable, PortletPreferences, Serializable {
044    
045            public PortletPreferencesImpl() {
046                    this(
047                            0, 0, 0, 0, null, null, Collections.<String, Preference>emptyMap());
048            }
049    
050            public PortletPreferencesImpl(
051                    long companyId, long ownerId, int ownerType, long plid,
052                    String portletId, String xml, Map<String, Preference> preferences) {
053    
054                    super(ownerId, ownerType, xml, preferences);
055    
056                    this.companyId = companyId;
057                    _plid = plid;
058                    _portletId = portletId;
059            }
060    
061            public PortletPreferencesImpl(
062                    String xml, Map<String, Preference> preferences) {
063    
064                    this(0, 0, 0, 0, null, xml, preferences);
065            }
066    
067            @Override
068            public Object clone() {
069                    return new PortletPreferencesImpl(
070                            companyId, getOwnerId(), getOwnerType(), _plid, _portletId,
071                            getOriginalXML(), getOriginalPreferences());
072            }
073    
074            @Override
075            public boolean equals(Object obj) {
076                    if (this == obj) {
077                            return true;
078                    }
079    
080                    if (!(obj instanceof PortletPreferencesImpl)) {
081                            return false;
082                    }
083    
084                    PortletPreferencesImpl portletPreferences = (PortletPreferencesImpl)obj;
085    
086                    if ((companyId == portletPreferences.companyId) &&
087                            (getOwnerId() == portletPreferences.getOwnerId()) &&
088                            (getOwnerType() == portletPreferences.getOwnerType()) &&
089                            (getPlid() == portletPreferences.getPlid()) &&
090                            getPortletId().equals(portletPreferences.getPortletId()) &&
091                            getPreferences().equals(portletPreferences.getPreferences())) {
092    
093                            return true;
094                    }
095                    else {
096                            return false;
097                    }
098            }
099    
100            public long getPlid() {
101                    return _plid;
102            }
103    
104            @Override
105            public int hashCode() {
106                    int hashCode = HashUtil.hash(0, companyId);
107    
108                    hashCode = HashUtil.hash(hashCode, getOwnerId());
109                    hashCode = HashUtil.hash(hashCode, getOwnerType());
110                    hashCode = HashUtil.hash(hashCode, _plid);
111                    hashCode = HashUtil.hash(hashCode, _portletId);
112                    hashCode = HashUtil.hash(hashCode, getPreferences());
113    
114                    return hashCode;
115            }
116    
117            @Override
118            public void reset(String key) throws ReadOnlyException {
119                    if (isReadOnly(key)) {
120                            throw new ReadOnlyException(key);
121                    }
122    
123                    if ((_defaultPreferences == null) && (_portletId != null)) {
124                            try {
125                                    _defaultPreferences =
126                                            PortletPreferencesLocalServiceUtil.getDefaultPreferences(
127                                                    companyId, _portletId);
128                            }
129                            catch (Exception e) {
130                                    if (_log.isWarnEnabled()) {
131                                            _log.warn(e, e);
132                                    }
133                            }
134                    }
135    
136                    String[] defaultValues = null;
137    
138                    if (_defaultPreferences != null) {
139                            defaultValues = _defaultPreferences.getValues(key, defaultValues);
140                    }
141    
142                    if (defaultValues != null) {
143                            setValues(key, defaultValues);
144                    }
145                    else {
146                            Map<String, Preference> modifiedPreferences =
147                                    getModifiedPreferences();
148    
149                            modifiedPreferences.remove(key);
150                    }
151            }
152    
153            public void setPlid(long plid) {
154                    _plid = plid;
155            }
156    
157            @Override
158            public void store() throws IOException, ValidatorException {
159                    if (_portletId == null) {
160                            throw new UnsupportedOperationException();
161                    }
162    
163                    try {
164                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
165                                    companyId, _portletId);
166    
167                            PreferencesValidator preferencesValidator =
168                                    PortalUtil.getPreferencesValidator(portlet);
169    
170                            if (preferencesValidator != null) {
171                                    preferencesValidator.validate(this);
172                            }
173    
174                            PortletPreferencesLocalServiceUtil.updatePreferences(
175                                    getOwnerId(), getOwnerType(), _plid, _portletId, this);
176                    }
177                    catch (SystemException se) {
178                            throw new IOException(se.getMessage());
179                    }
180            }
181    
182            protected String getPortletId() {
183                    return _portletId;
184            }
185    
186            protected long companyId;
187    
188            private static Log _log = LogFactoryUtil.getLog(
189                    PortletPreferencesImpl.class);
190    
191            private PortletPreferences _defaultPreferences;
192            private long _plid;
193            private String _portletId;
194    
195    }