1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  /**
23   * <a href="PortalPreferencesImpl.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class PortalPreferencesImpl implements PortalPreferences {
28  
29      public PortalPreferencesImpl(
30          PortletPreferencesImpl preferences, boolean signedIn) {
31  
32          _preferences = preferences;
33          _signedIn = signedIn;
34      }
35  
36      public String getValue(String namespace, String key) {
37          return getValue(namespace, key, null);
38      }
39  
40      public String getValue(String namespace, String key, String defaultValue) {
41          key = _encodeKey(namespace, key);
42  
43          return _preferences.getValue(key, defaultValue);
44      }
45  
46      public String[] getValues(String namespace, String key) {
47          return getValues(namespace, key, null);
48      }
49  
50      public String[] getValues(
51          String namespace, String key, String[] defaultValue) {
52  
53          key = _encodeKey(namespace, key);
54  
55          return _preferences.getValues(key, defaultValue);
56      }
57  
58      public void setValue(String namespace, String key, String value) {
59          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
60              return;
61          }
62  
63          key = _encodeKey(namespace, key);
64  
65          try {
66              if (value != null) {
67                  _preferences.setValue(key, value);
68              }
69              else {
70                  _preferences.reset(key);
71              }
72  
73              if (_signedIn) {
74                  _preferences.store();
75              }
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80      }
81  
82      public void setValues(String namespace, String key, String[] values) {
83          if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
84              return;
85          }
86  
87          key = _encodeKey(namespace, key);
88  
89          try {
90              if (values != null) {
91                  _preferences.setValues(key, values);
92              }
93              else {
94                  _preferences.reset(key);
95              }
96  
97              if (_signedIn) {
98                  _preferences.store();
99              }
100         }
101         catch (Exception e) {
102             _log.error(e, e);
103         }
104     }
105 
106     private String _encodeKey(String namespace, String key) {
107         return namespace + StringPool.POUND + key;
108     }
109 
110     private static final String _RANDOM_KEY = "r";
111 
112     private static Log _log = LogFactoryUtil.getLog(PortalPreferences.class);
113 
114     private PortletPreferencesImpl _preferences;
115     private boolean _signedIn;
116 
117 }