001    /**
002     * Copyright (c) 2000-2012 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(0, 0, 0, 0, null, Collections.<String, Preference>emptyMap());
048            }
049    
050            public PortletPreferencesImpl(
051                    long companyId, long ownerId, int ownerType, long plid,
052                    String portletId, Map<String, Preference> preferences) {
053    
054                    super(companyId, ownerId, ownerType, preferences);
055    
056                    _plid = plid;
057                    _portletId = portletId;
058            }
059    
060            @Override
061            public Object clone() {
062                    return new PortletPreferencesImpl(
063                            getCompanyId(), getOwnerId(), getOwnerType(), _plid, _portletId,
064                            getOriginalPreferences());
065            }
066    
067            @Override
068            public boolean equals(Object obj) {
069                    PortletPreferencesImpl portletPreferences = (PortletPreferencesImpl)obj;
070    
071                    if (this == portletPreferences) {
072                            return true;
073                    }
074    
075                    if ((getCompanyId() == portletPreferences.getCompanyId()) &&
076                            (getOwnerId() == portletPreferences.getOwnerId()) &&
077                            (getOwnerType() == portletPreferences.getOwnerType()) &&
078                            (getPlid() == portletPreferences.getPlid()) &&
079                            (getPortletId().equals(portletPreferences.getPortletId())) &&
080                            (getMap().equals(portletPreferences.getMap()))) {
081    
082                            return true;
083                    }
084                    else {
085                            return false;
086                    }
087            }
088    
089            public long getPlid() {
090                    return _plid;
091            }
092    
093            @Override
094            public int hashCode() {
095                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
096    
097                    hashCode.append(getCompanyId());
098                    hashCode.append(getOwnerId());
099                    hashCode.append(getOwnerType());
100                    hashCode.append(_plid);
101                    hashCode.append(_portletId);
102                    hashCode.append(getPreferences());
103    
104                    return hashCode.toHashCode();
105            }
106    
107            @Override
108            public void reset(String key) throws ReadOnlyException {
109                    if (isReadOnly(key)) {
110                            throw new ReadOnlyException(key);
111                    }
112    
113                    if (_defaultPreferences == null) {
114                            try {
115                                    if (_portletId != null) {
116                                            _defaultPreferences = PortletPreferencesLocalServiceUtil.
117                                                    getDefaultPreferences(getCompanyId(), _portletId);
118                                    }
119                            }
120                            catch (Exception e) {
121                                    if (_log.isWarnEnabled()) {
122                                            _log.warn(e, e);
123                                    }
124                            }
125                    }
126    
127                    String[] defaultValues = null;
128    
129                    if (_defaultPreferences != null) {
130                            defaultValues = _defaultPreferences.getValues(key, defaultValues);
131                    }
132    
133                    if (defaultValues != null) {
134                            setValues(key, defaultValues);
135                    }
136                    else {
137                            Map<String, Preference> modifiedPreferences =
138                                    getModifiedPreferences();
139    
140                            modifiedPreferences.remove(key);
141                    }
142            }
143    
144            @Override
145            public void store() throws IOException, ValidatorException {
146                    if (_portletId == null) {
147                            throw new UnsupportedOperationException();
148                    }
149    
150                    try {
151                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
152                                    getCompanyId(), _portletId);
153    
154                            PreferencesValidator preferencesValidator =
155                                    PortalUtil.getPreferencesValidator(portlet);
156    
157                            if (preferencesValidator != null) {
158                                    preferencesValidator.validate(this);
159                            }
160    
161                            PortletPreferencesLocalServiceUtil.updatePreferences(
162                                    getOwnerId(), getOwnerType(), _plid, _portletId, this);
163                    }
164                    catch (SystemException se) {
165                            throw new IOException(se.getMessage());
166                    }
167            }
168    
169            protected String getPortletId() {
170                    return _portletId;
171            }
172    
173            private static Log _log = LogFactoryUtil.getLog(
174                    PortletPreferencesImpl.class);
175    
176            private PortletPreferences _defaultPreferences;
177            private long _plid;
178            private String _portletId;
179    
180    }