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                    PortletPreferencesImpl portletPreferences = (PortletPreferencesImpl)obj;
077    
078                    if (this == portletPreferences) {
079                            return true;
080                    }
081    
082                    if ((getCompanyId() == portletPreferences.getCompanyId()) &&
083                            (getOwnerId() == portletPreferences.getOwnerId()) &&
084                            (getOwnerType() == portletPreferences.getOwnerType()) &&
085                            (getPlid() == portletPreferences.getPlid()) &&
086                            getPortletId().equals(portletPreferences.getPortletId()) &&
087                            getPreferences().equals(portletPreferences.getPreferences())) {
088    
089                            return true;
090                    }
091                    else {
092                            return false;
093                    }
094            }
095    
096            public long getPlid() {
097                    return _plid;
098            }
099    
100            @Override
101            public int hashCode() {
102                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
103    
104                    hashCode.append(getCompanyId());
105                    hashCode.append(getOwnerId());
106                    hashCode.append(getOwnerType());
107                    hashCode.append(_plid);
108                    hashCode.append(_portletId);
109                    hashCode.append(getPreferences());
110    
111                    return hashCode.toHashCode();
112            }
113    
114            @Override
115            public void reset(String key) throws ReadOnlyException {
116                    if (isReadOnly(key)) {
117                            throw new ReadOnlyException(key);
118                    }
119    
120                    if ((_defaultPreferences == null) && (_portletId != null)) {
121                            try {
122                                    _defaultPreferences = PortletPreferencesLocalServiceUtil.
123                                            getDefaultPreferences(getCompanyId(), _portletId);
124                            }
125                            catch (Exception e) {
126                                    if (_log.isWarnEnabled()) {
127                                            _log.warn(e, e);
128                                    }
129                            }
130                    }
131    
132                    String[] defaultValues = null;
133    
134                    if (_defaultPreferences != null) {
135                            defaultValues = _defaultPreferences.getValues(key, defaultValues);
136                    }
137    
138                    if (defaultValues != null) {
139                            setValues(key, defaultValues);
140                    }
141                    else {
142                            Map<String, Preference> modifiedPreferences =
143                                    getModifiedPreferences();
144    
145                            modifiedPreferences.remove(key);
146                    }
147            }
148    
149            @Override
150            public void store() throws IOException, ValidatorException {
151                    if (_portletId == null) {
152                            throw new UnsupportedOperationException();
153                    }
154    
155                    try {
156                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
157                                    getCompanyId(), _portletId);
158    
159                            PreferencesValidator preferencesValidator =
160                                    PortalUtil.getPreferencesValidator(portlet);
161    
162                            if (preferencesValidator != null) {
163                                    preferencesValidator.validate(this);
164                            }
165    
166                            PortletPreferencesLocalServiceUtil.updatePreferences(
167                                    getOwnerId(), getOwnerType(), _plid, _portletId, this);
168                    }
169                    catch (SystemException se) {
170                            throw new IOException(se.getMessage());
171                    }
172            }
173    
174            protected String getPortletId() {
175                    return _portletId;
176            }
177    
178            private static Log _log = LogFactoryUtil.getLog(
179                    PortletPreferencesImpl.class);
180    
181            private PortletPreferences _defaultPreferences;
182            private long _plid;
183            private String _portletId;
184    
185    }