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.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
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.ReadOnlyException;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Alexander Chow
037     */
038    public class PortalPreferencesImpl
039            extends BasePreferencesImpl
040            implements Cloneable, PortalPreferences, Serializable {
041    
042            public PortalPreferencesImpl() {
043                    this(0, 0, 0, null, Collections.<String, Preference>emptyMap(), false);
044            }
045    
046            public PortalPreferencesImpl(
047                    long companyId, long ownerId, int ownerType, String xml,
048                    Map<String, Preference> preferences, boolean signedIn) {
049    
050                    super(companyId, ownerId, ownerType, xml, preferences);
051    
052                    _signedIn = signedIn;
053            }
054    
055            @Override
056            public Object clone() {
057                    return new PortalPreferencesImpl(
058                            getCompanyId(), getOwnerId(), getOwnerType(), getOriginalXML(),
059                            getOriginalPreferences(), isSignedIn());
060            }
061    
062            @Override
063            public boolean equals(Object obj) {
064                    if (obj == null) {
065                            return false;
066                    }
067    
068                    PortalPreferencesImpl portalPreferences = (PortalPreferencesImpl)obj;
069    
070                    if (this == portalPreferences) {
071                            return true;
072                    }
073    
074                    if ((getCompanyId() == portalPreferences.getCompanyId()) &&
075                            (getOwnerId() == portalPreferences.getOwnerId()) &&
076                            (getOwnerType() == portalPreferences.getOwnerType()) &&
077                            getPreferences().equals(portalPreferences.getPreferences())) {
078    
079                            return true;
080                    }
081                    else {
082                            return false;
083                    }
084            }
085    
086            public long getUserId() {
087                    return _userId;
088            }
089    
090            @Override
091            public String getValue(String namespace, String key) {
092                    return getValue(namespace, key, null);
093            }
094    
095            public String getValue(String namespace, String key, String defaultValue) {
096                    key = _encodeKey(namespace, key);
097    
098                    return super.getValue(key, defaultValue);
099            }
100    
101            public String[] getValues(String namespace, String key) {
102                    return getValues(namespace, key, null);
103            }
104    
105            public String[] getValues(
106                    String namespace, String key, String[] defaultValue) {
107    
108                    key = _encodeKey(namespace, key);
109    
110                    return super.getValues(key, defaultValue);
111            }
112    
113            @Override
114            public int hashCode() {
115                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
116    
117                    hashCode.append(getCompanyId());
118                    hashCode.append(getOwnerId());
119                    hashCode.append(getOwnerType());
120                    hashCode.append(getPreferences());
121    
122                    return hashCode.toHashCode();
123            }
124    
125            public boolean isSignedIn() {
126                    return _signedIn;
127            }
128    
129            @Override
130            public void reset(String key) throws ReadOnlyException {
131                    if (isReadOnly(key)) {
132                            throw new ReadOnlyException(key);
133                    }
134    
135                    Map<String, Preference> modifiedPreferences = getModifiedPreferences();
136    
137                    modifiedPreferences.remove(key);
138            }
139    
140            public void resetValues(String namespace) {
141                    try {
142                            Map<String, Preference> preferences = getPreferences();
143    
144                            for (Map.Entry<String, Preference> entry : preferences.entrySet()) {
145                                    String key = entry.getKey();
146    
147                                    if (key.startsWith(namespace) && !isReadOnly(key)) {
148                                            reset(key);
149                                    }
150                            }
151    
152                            store();
153                    }
154                    catch (Exception e) {
155                            _log.error(e, e);
156                    }
157            }
158    
159            public void setSignedIn(boolean signedIn) {
160                    _signedIn = signedIn;
161            }
162    
163            public void setUserId(long userId) {
164                    _userId = userId;
165            }
166    
167            public void setValue(String namespace, String key, String value) {
168                    if (Validator.isNull(key) || key.equals(_RANDOM_KEY)) {
169                            return;
170                    }
171    
172                    key = _encodeKey(namespace, key);
173    
174                    try {
175                            if (value != null) {
176                                    super.setValue(key, value);
177                            }
178                            else {
179                                    reset(key);
180                            }
181    
182                            if (_signedIn) {
183                                    store();
184                            }
185                    }
186                    catch (Exception e) {
187                            _log.error(e, e);
188                    }
189            }
190    
191            public void setValues(String namespace, String key, String[] values) {
192                    if (Validator.isNull(key) || key.equals(_RANDOM_KEY)) {
193                            return;
194                    }
195    
196                    key = _encodeKey(namespace, key);
197    
198                    try {
199                            if (values != null) {
200                                    super.setValues(key, values);
201                            }
202                            else {
203                                    reset(key);
204                            }
205    
206                            if (_signedIn) {
207                                    store();
208                            }
209                    }
210                    catch (Exception e) {
211                            _log.error(e, e);
212                    }
213            }
214    
215            @Override
216            public void store() throws IOException {
217                    try {
218                            PortalPreferencesLocalServiceUtil.updatePreferences(
219                                    getOwnerId(), getOwnerType(), this);
220                    }
221                    catch (SystemException se) {
222                            throw new IOException(se.getMessage());
223                    }
224            }
225    
226            private String _encodeKey(String namespace, String key) {
227                    if (Validator.isNull(namespace)) {
228                            return key;
229                    }
230                    else {
231                            return namespace.concat(StringPool.POUND).concat(key);
232                    }
233            }
234    
235            private static final String _RANDOM_KEY = "r";
236    
237            private static Log _log = LogFactoryUtil.getLog(PortalPreferences.class);
238    
239            private boolean _signedIn;
240            private long _userId;
241    
242    }