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