1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  
27  /**
28   * <a href="PortalPreferencesImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   *
32   */
33  public class PortalPreferencesImpl implements PortalPreferences {
34  
35      public PortalPreferencesImpl(
36          PortletPreferencesImpl prefs, boolean signedIn) {
37  
38          _prefs = prefs;
39          _signedIn = signedIn;
40      }
41  
42      public String getValue(String namespace, String key) {
43          return getValue(namespace, key, null);
44      }
45  
46      public String getValue(String namespace, String key, String defaultValue) {
47          key = _encodeKey(namespace, key);
48  
49          return _prefs.getValue(key, defaultValue);
50      }
51  
52      public String[] getValues(String namespace, String key) {
53          return getValues(namespace, key, null);
54      }
55  
56      public String[] getValues(
57          String namespace, String key, String[] defaultValue) {
58  
59          key = _encodeKey(namespace, key);
60  
61          return _prefs.getValues(key, defaultValue);
62      }
63  
64      public void setValue(String namespace, String key, String value) {
65          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
66              return;
67          }
68  
69          key = _encodeKey(namespace, key);
70  
71          try {
72              if (value != null) {
73                  _prefs.setValue(key, value);
74              }
75              else {
76                  _prefs.reset(key);
77              }
78  
79              if (_signedIn) {
80                  _prefs.store();
81              }
82          }
83          catch (Exception e) {
84              _log.error(e, e);
85          }
86      }
87  
88      public void setValues(String namespace, String key, String[] values) {
89          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
90              return;
91          }
92  
93          key = _encodeKey(namespace, key);
94  
95          try {
96              if (values != null) {
97                  _prefs.setValues(key, values);
98              }
99              else {
100                 _prefs.reset(key);
101             }
102 
103             if (_signedIn) {
104                 _prefs.store();
105             }
106         }
107         catch (Exception e) {
108             _log.error(e, e);
109         }
110     }
111 
112     private String _encodeKey(String namespace, String key) {
113         return namespace + StringPool.POUND + key;
114     }
115 
116     private static final String _RANDOM_KEY = "r";
117 
118     private static Log _log = LogFactoryUtil.getLog(PortalPreferences.class);
119 
120     private PortletPreferencesImpl _prefs;
121     private boolean _signedIn;
122 
123 }