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.HashCode;
021    import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.service.PortletLocalServiceUtil;
024    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.io.IOException;
028    import java.io.Serializable;
029    
030    import java.util.Collections;
031    import java.util.Map;
032    
033    import javax.portlet.PortletPreferences;
034    import javax.portlet.PreferencesValidator;
035    import javax.portlet.ReadOnlyException;
036    import javax.portlet.ValidatorException;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Alexander Chow
041     */
042    public class PortletPreferencesImpl
043            extends BasePreferencesImpl
044            implements Cloneable, PortletPreferences, Serializable {
045    
046            public PortletPreferencesImpl() {
047                    this(
048                            0, 0, 0, 0, null, null, Collections.<String, Preference>emptyMap());
049            }
050    
051            public PortletPreferencesImpl(
052                    long companyId, long ownerId, int ownerType, long plid,
053                    String portletId, String xml, Map<String, Preference> preferences) {
054    
055                    super(companyId, ownerId, ownerType, xml, preferences);
056    
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                            getCompanyId(), 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 ((getCompanyId() == portletPreferences.getCompanyId()) &&
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                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
107    
108                    hashCode.append(getCompanyId());
109                    hashCode.append(getOwnerId());
110                    hashCode.append(getOwnerType());
111                    hashCode.append(_plid);
112                    hashCode.append(_portletId);
113                    hashCode.append(getPreferences());
114    
115                    return hashCode.toHashCode();
116            }
117    
118            @Override
119            public void reset(String key) throws ReadOnlyException {
120                    if (isReadOnly(key)) {
121                            throw new ReadOnlyException(key);
122                    }
123    
124                    if ((_defaultPreferences == null) && (_portletId != null)) {
125                            try {
126                                    _defaultPreferences =
127                                            PortletPreferencesLocalServiceUtil.getDefaultPreferences(
128                                                    getCompanyId(), _portletId);
129                            }
130                            catch (Exception e) {
131                                    if (_log.isWarnEnabled()) {
132                                            _log.warn(e, e);
133                                    }
134                            }
135                    }
136    
137                    String[] defaultValues = null;
138    
139                    if (_defaultPreferences != null) {
140                            defaultValues = _defaultPreferences.getValues(key, defaultValues);
141                    }
142    
143                    if (defaultValues != null) {
144                            setValues(key, defaultValues);
145                    }
146                    else {
147                            Map<String, Preference> modifiedPreferences =
148                                    getModifiedPreferences();
149    
150                            modifiedPreferences.remove(key);
151                    }
152            }
153    
154            @Override
155            public void store() throws IOException, ValidatorException {
156                    if (_portletId == null) {
157                            throw new UnsupportedOperationException();
158                    }
159    
160                    try {
161                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
162                                    getCompanyId(), _portletId);
163    
164                            PreferencesValidator preferencesValidator =
165                                    PortalUtil.getPreferencesValidator(portlet);
166    
167                            if (preferencesValidator != null) {
168                                    preferencesValidator.validate(this);
169                            }
170    
171                            PortletPreferencesLocalServiceUtil.updatePreferences(
172                                    getOwnerId(), getOwnerType(), _plid, _portletId, this);
173                    }
174                    catch (SystemException se) {
175                            throw new IOException(se.getMessage());
176                    }
177            }
178    
179            protected String getPortletId() {
180                    return _portletId;
181            }
182    
183            private static Log _log = LogFactoryUtil.getLog(
184                    PortletPreferencesImpl.class);
185    
186            private PortletPreferences _defaultPreferences;
187            private long _plid;
188            private String _portletId;
189    
190    }