1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * <a href="PortalPreferencesImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class PortalPreferencesImpl implements PortalPreferences {
38  
39      public PortalPreferencesImpl(
40          PortletPreferencesImpl prefs, boolean signedIn) {
41  
42          _prefs = prefs;
43          _signedIn = signedIn;
44      }
45  
46      public String getValue(String namespace, String key) {
47          return getValue(namespace, key, null);
48      }
49  
50      public String getValue(String namespace, String key, String defaultValue) {
51          key = _encodeKey(namespace, key);
52  
53          return _prefs.getValue(key, defaultValue);
54      }
55  
56      public String[] getValues(String namespace, String key) {
57          return getValues(namespace, key, null);
58      }
59  
60      public String[] getValues(
61          String namespace, String key, String[] defaultValue) {
62  
63          key = _encodeKey(namespace, key);
64  
65          return _prefs.getValues(key, defaultValue);
66      }
67  
68      public void setValue(String namespace, String key, String value) {
69          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
70              return;
71          }
72  
73          key = _encodeKey(namespace, key);
74  
75          try {
76              if (value != null) {
77                  _prefs.setValue(key, value);
78              }
79              else {
80                  _prefs.reset(key);
81              }
82  
83              if (_signedIn) {
84                  _prefs.store();
85              }
86          }
87          catch (Exception e) {
88              _log.error(e, e);
89          }
90      }
91  
92      public void setValues(String namespace, String key, String[] values) {
93          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
94              return;
95          }
96  
97          key = _encodeKey(namespace, key);
98  
99          try {
100             if (values != null) {
101                 _prefs.setValues(key, values);
102             }
103             else {
104                 _prefs.reset(key);
105             }
106 
107             if (_signedIn) {
108                 _prefs.store();
109             }
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113         }
114     }
115 
116     private String _encodeKey(String namespace, String key) {
117         return namespace + StringPool.POUND + key;
118     }
119 
120     private static final String _RANDOM_KEY = "r";
121 
122     private static Log _log = LogFactory.getLog(PortalPreferences.class);
123 
124     private PortletPreferencesImpl _prefs;
125     private boolean _signedIn;
126 
127 }