1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util.bridges.jsf.common;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.faces.context.FacesContext;
33  
34  import javax.portlet.PortletPreferences;
35  
36  /**
37   * <a href="PortletPreferencesManagedBean.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Neil Griffin
41   */
42  public class PortletPreferencesManagedBean {
43  
44      public PortletPreferencesManagedBean() {
45  
46          // Store the portlet preferences as a bean property because of ICE-1625.
47          // When using normal JSF, this constructor will get called each time a
48          // request is made. This is a little inefficient, but it's a coding
49          // tradeoff to make things work with both normal JSF and ICEfaces 1.6.0.
50  
51          FacesContext facesContext = FacesContext.getCurrentInstance();
52  
53          _portletPreferences =
54              JSFPortletUtil.getPortletPreferences(facesContext);
55  
56          // Portlet preferences are backed by a map of string arrays. This makes
57          // the JSP syntax a little funky, so in order to make the syntax easier,
58          // copy each name and its first value into a new map where the name and
59          // value are both strings.
60  
61          _preferences = new HashMap<String, String>();
62  
63          Enumeration<String> enu = _portletPreferences.getNames();
64  
65          while (enu.hasMoreElements()) {
66              String name = enu.nextElement();
67  
68              String value = _portletPreferences.getValue(name, null);
69  
70              _preferences.put(name, value);
71  
72              if (_log.isDebugEnabled()) {
73                  _log.debug("{name=" + name + ", value=" + value + "}");
74              }
75          }
76      }
77  
78      public Map<String, String> getPreferences() {
79          return _preferences;
80      }
81  
82      public String resetDefaultValues() {
83          try {
84              Enumeration<String> enu = _portletPreferences.getNames();
85  
86              while (enu.hasMoreElements()) {
87                  String name = enu.nextElement();
88  
89                  if (!_portletPreferences.isReadOnly(name)) {
90                      _portletPreferences.reset(name);
91  
92                      String value = _portletPreferences.getValue(name, null);
93  
94                      _preferences.put(name, value);
95  
96                      _portletPreferences.store();
97                  }
98              }
99  
100             addInfoMessage("you-have-successfully-reset-your-preferences");
101 
102             return ActionOutcomes.SUCCESS;
103         }
104         catch (Exception e) {
105             _log.error(e, e);
106 
107             addErrorMessage(
108                 "an-error-occurred-while-resetting-your-preferences");
109 
110             return ActionOutcomes.FAILURE;
111         }
112     }
113 
114     public String submit() {
115         try {
116             Enumeration<String> enu = _portletPreferences.getNames();
117 
118             while (enu.hasMoreElements()) {
119                 String name = enu.nextElement();
120 
121                 if (!_portletPreferences.isReadOnly(name)) {
122                     String value = _preferences.get(name);
123 
124                     _portletPreferences.setValue(name, value);
125                 }
126             }
127 
128             _portletPreferences.store();
129 
130             addInfoMessage("you-have-successfully-updated-your-preferences");
131 
132             return ActionOutcomes.SUCCESS;
133         }
134         catch (Exception e) {
135             _log.error(e, e);
136 
137             addErrorMessage(
138                 "an-error-occurred-while-updating-your-preferences");
139 
140             return ActionOutcomes.FAILURE;
141         }
142     }
143 
144     protected void addErrorMessage(String key) {
145         FacesContext facesContext = FacesContext.getCurrentInstance();
146 
147         FacesMessageUtil.error(facesContext, key);
148     }
149 
150     protected void addInfoMessage(String key) {
151         FacesContext facesContext = FacesContext.getCurrentInstance();
152 
153         FacesMessageUtil.info(facesContext, key);
154     }
155 
156     private static Log _log =
157         LogFactoryUtil.getLog(PortletPreferencesManagedBean.class);
158 
159     private PortletPreferences _portletPreferences;
160     private Map<String, String> _preferences;
161 
162 }