1   /**
2    * Copyright (c) 2000-2008 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 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  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
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();
63  
64          Enumeration enu = _portletPreferences.getNames();
65  
66          while (enu.hasMoreElements()) {
67              String name = (String)enu.nextElement();
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 getPreferences() {
79          return _preferences;
80      }
81  
82      public String resetDefaultValues() {
83          try {
84              Enumeration enu = _portletPreferences.getNames();
85  
86              while (enu.hasMoreElements()) {
87                  String name = (String)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 enu = _portletPreferences.getNames();
117 
118             while (enu.hasMoreElements()) {
119                 String name = (String)enu.nextElement();
120 
121                 if (!_portletPreferences.isReadOnly(name)) {
122                     String value = (String)_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         LogFactory.getLog(PortletPreferencesManagedBean.class);
158 
159     private PortletPreferences _portletPreferences;
160     private Map _preferences;
161 
162 }