001    /**
002     * Copyright (c) 2000-2010 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class PortalPreferencesImpl implements PortalPreferences {
026    
027            public PortalPreferencesImpl(
028                    PortletPreferencesImpl preferences, boolean signedIn) {
029    
030                    _preferences = preferences;
031                    _signedIn = signedIn;
032            }
033    
034            public String getValue(String namespace, String key) {
035                    return getValue(namespace, key, null);
036            }
037    
038            public String getValue(String namespace, String key, String defaultValue) {
039                    key = _encodeKey(namespace, key);
040    
041                    return _preferences.getValue(key, defaultValue);
042            }
043    
044            public String[] getValues(String namespace, String key) {
045                    return getValues(namespace, key, null);
046            }
047    
048            public String[] getValues(
049                    String namespace, String key, String[] defaultValue) {
050    
051                    key = _encodeKey(namespace, key);
052    
053                    return _preferences.getValues(key, defaultValue);
054            }
055    
056            public void setValue(String namespace, String key, String value) {
057                    if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
058                            return;
059                    }
060    
061                    key = _encodeKey(namespace, key);
062    
063                    try {
064                            if (value != null) {
065                                    _preferences.setValue(key, value);
066                            }
067                            else {
068                                    _preferences.reset(key);
069                            }
070    
071                            if (_signedIn) {
072                                    _preferences.store();
073                            }
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077                    }
078            }
079    
080            public void setValues(String namespace, String key, String[] values) {
081                    if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
082                            return;
083                    }
084    
085                    key = _encodeKey(namespace, key);
086    
087                    try {
088                            if (values != null) {
089                                    _preferences.setValues(key, values);
090                            }
091                            else {
092                                    _preferences.reset(key);
093                            }
094    
095                            if (_signedIn) {
096                                    _preferences.store();
097                            }
098                    }
099                    catch (Exception e) {
100                            _log.error(e, e);
101                    }
102            }
103    
104            private String _encodeKey(String namespace, String key) {
105                    return namespace + StringPool.POUND + key;
106            }
107    
108            private static final String _RANDOM_KEY = "r";
109    
110            private static Log _log = LogFactoryUtil.getLog(PortalPreferences.class);
111    
112            private PortletPreferencesImpl _preferences;
113            private boolean _signedIn;
114    
115    }