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