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