001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.Validator;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * <a href="PortalPreferencesImpl.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class PortalPreferencesImpl implements PortalPreferences {
036:
037: public PortalPreferencesImpl(PortletPreferencesImpl prefs,
038: boolean signedIn) {
039:
040: _prefs = prefs;
041: _signedIn = signedIn;
042: }
043:
044: public String getValue(String namespace, String key) {
045: return getValue(namespace, key, null);
046: }
047:
048: public String getValue(String namespace, String key,
049: String defaultValue) {
050: key = _encodeKey(namespace, key);
051:
052: return _prefs.getValue(key, defaultValue);
053: }
054:
055: public String[] getValues(String namespace, String key) {
056: return getValues(namespace, key, null);
057: }
058:
059: public String[] getValues(String namespace, String key,
060: String[] defaultValue) {
061:
062: key = _encodeKey(namespace, key);
063:
064: return _prefs.getValues(key, defaultValue);
065: }
066:
067: public void setValue(String namespace, String key, String value) {
068: if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
069: return;
070: }
071:
072: key = _encodeKey(namespace, key);
073:
074: try {
075: if (value != null) {
076: _prefs.setValue(key, value);
077: } else {
078: _prefs.reset(key);
079: }
080:
081: if (_signedIn) {
082: _prefs.store();
083: }
084: } catch (Exception e) {
085: _log.error(e, e);
086: }
087: }
088:
089: public void setValues(String namespace, String key, String[] values) {
090: if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
091: return;
092: }
093:
094: key = _encodeKey(namespace, key);
095:
096: try {
097: if (values != null) {
098: _prefs.setValues(key, values);
099: } else {
100: _prefs.reset(key);
101: }
102:
103: if (_signedIn) {
104: _prefs.store();
105: }
106: } catch (Exception e) {
107: _log.error(e, e);
108: }
109: }
110:
111: private String _encodeKey(String namespace, String key) {
112: return namespace + StringPool.POUND + key;
113: }
114:
115: private static final String _RANDOM_KEY = "r";
116:
117: private static Log _log = LogFactory
118: .getLog(PortalPreferences.class);
119:
120: private PortletPreferencesImpl _prefs;
121: private boolean _signedIn;
122:
123: }
|