1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.bridges.jsf.common;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.faces.context.FacesContext;
25  
26  import javax.portlet.PortletPreferences;
27  
28  /**
29   * <a href="PortletPreferencesManagedBean.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Neil Griffin
33   */
34  public class PortletPreferencesManagedBean {
35  
36      public PortletPreferencesManagedBean() {
37  
38          // Store the portlet preferences as a bean property because of ICE-1625.
39          // When using normal JSF, this constructor will get called each time a
40          // request is made. This is a little inefficient, but it's a coding
41          // tradeoff to make things work with both normal JSF and ICEfaces 1.6.0.
42  
43          FacesContext facesContext = FacesContext.getCurrentInstance();
44  
45          _portletPreferences =
46              JSFPortletUtil.getPortletPreferences(facesContext);
47  
48          // Portlet preferences are backed by a map of string arrays. This makes
49          // the JSP syntax a little funky, so in order to make the syntax easier,
50          // copy each name and its first value into a new map where the name and
51          // value are both strings.
52  
53          _preferences = new HashMap<String, String>();
54  
55          Enumeration<String> enu = _portletPreferences.getNames();
56  
57          while (enu.hasMoreElements()) {
58              String name = enu.nextElement();
59  
60              String value = _portletPreferences.getValue(name, null);
61  
62              _preferences.put(name, value);
63  
64              if (_log.isDebugEnabled()) {
65                  _log.debug("{name=" + name + ", value=" + value + "}");
66              }
67          }
68      }
69  
70      public Map<String, String> getPreferences() {
71          return _preferences;
72      }
73  
74      public String resetDefaultValues() {
75          try {
76              Enumeration<String> enu = _portletPreferences.getNames();
77  
78              while (enu.hasMoreElements()) {
79                  String name = enu.nextElement();
80  
81                  if (!_portletPreferences.isReadOnly(name)) {
82                      _portletPreferences.reset(name);
83  
84                      String value = _portletPreferences.getValue(name, null);
85  
86                      _preferences.put(name, value);
87  
88                      _portletPreferences.store();
89                  }
90              }
91  
92              addInfoMessage("you-have-successfully-reset-your-preferences");
93  
94              return ActionOutcomes.SUCCESS;
95          }
96          catch (Exception e) {
97              _log.error(e, e);
98  
99              addErrorMessage(
100                 "an-error-occurred-while-resetting-your-preferences");
101 
102             return ActionOutcomes.FAILURE;
103         }
104     }
105 
106     public String submit() {
107         try {
108             Enumeration<String> enu = _portletPreferences.getNames();
109 
110             while (enu.hasMoreElements()) {
111                 String name = enu.nextElement();
112 
113                 if (!_portletPreferences.isReadOnly(name)) {
114                     String value = _preferences.get(name);
115 
116                     _portletPreferences.setValue(name, value);
117                 }
118             }
119 
120             _portletPreferences.store();
121 
122             addInfoMessage("you-have-successfully-updated-your-preferences");
123 
124             return ActionOutcomes.SUCCESS;
125         }
126         catch (Exception e) {
127             _log.error(e, e);
128 
129             addErrorMessage(
130                 "an-error-occurred-while-updating-your-preferences");
131 
132             return ActionOutcomes.FAILURE;
133         }
134     }
135 
136     protected void addErrorMessage(String key) {
137         FacesContext facesContext = FacesContext.getCurrentInstance();
138 
139         FacesMessageUtil.error(facesContext, key);
140     }
141 
142     protected void addInfoMessage(String key) {
143         FacesContext facesContext = FacesContext.getCurrentInstance();
144 
145         FacesMessageUtil.info(facesContext, key);
146     }
147 
148     private static Log _log = LogFactoryUtil.getLog(
149         PortletPreferencesManagedBean.class);
150 
151     private PortletPreferences _portletPreferences;
152     private Map<String, String> _preferences;
153 
154 }