001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.jsf.common;
022:
023: import java.util.Enumeration;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.faces.context.FacesContext;
028:
029: import javax.portlet.PortletPreferences;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <a href="PortletPreferencesManagedBean.java.html"><b><i>View Source</i></b>
036: * </a>
037: *
038: * @author Neil Griffin
039: *
040: */
041: public class PortletPreferencesManagedBean {
042:
043: public PortletPreferencesManagedBean() {
044:
045: // Store the portlet preferences as a bean property because of ICE-1625.
046: // When using normal JSF, this constructor will get called each time a
047: // request is made. This is a little inefficient, but it's a coding
048: // tradeoff to make things work with both normal JSF and ICEfaces 1.6.0.
049:
050: FacesContext facesContext = FacesContext.getCurrentInstance();
051:
052: _portletPreferences = JSFPortletUtil
053: .getPortletPreferences(facesContext);
054:
055: // Portlet preferences are backed by a map of string arrays. This makes
056: // the JSP syntax a little funky, so in order to make the syntax easier,
057: // copy each name and its first value into a new map where the name and
058: // value are both strings.
059:
060: _preferences = new HashMap();
061:
062: Enumeration enu = _portletPreferences.getNames();
063:
064: while (enu.hasMoreElements()) {
065: String name = (String) enu.nextElement();
066: String value = _portletPreferences.getValue(name, null);
067:
068: _preferences.put(name, value);
069:
070: if (_log.isDebugEnabled()) {
071: _log.debug("{name=" + name + ", value=" + value + "}");
072: }
073: }
074: }
075:
076: public Map getPreferences() {
077: return _preferences;
078: }
079:
080: public String resetDefaultValues() {
081: try {
082: Enumeration enu = _portletPreferences.getNames();
083:
084: while (enu.hasMoreElements()) {
085: String name = (String) enu.nextElement();
086:
087: if (!_portletPreferences.isReadOnly(name)) {
088: _portletPreferences.reset(name);
089:
090: String value = _portletPreferences.getValue(name,
091: null);
092:
093: _preferences.put(name, value);
094:
095: _portletPreferences.store();
096: }
097: }
098:
099: addInfoMessage("you-have-successfully-reset-your-preferences");
100:
101: return ActionOutcomes.SUCCESS;
102: } catch (Exception e) {
103: _log.error(e, e);
104:
105: addErrorMessage("an-error-occurred-while-resetting-your-preferences");
106:
107: return ActionOutcomes.FAILURE;
108: }
109: }
110:
111: public String submit() {
112: try {
113: Enumeration enu = _portletPreferences.getNames();
114:
115: while (enu.hasMoreElements()) {
116: String name = (String) enu.nextElement();
117:
118: if (!_portletPreferences.isReadOnly(name)) {
119: String value = (String) _preferences.get(name);
120:
121: _portletPreferences.setValue(name, value);
122: }
123: }
124:
125: _portletPreferences.store();
126:
127: addInfoMessage("you-have-successfully-updated-your-preferences");
128:
129: return ActionOutcomes.SUCCESS;
130: } catch (Exception e) {
131: _log.error(e, e);
132:
133: addErrorMessage("an-error-occurred-while-updating-your-preferences");
134:
135: return ActionOutcomes.FAILURE;
136: }
137: }
138:
139: protected void addErrorMessage(String key) {
140: FacesContext facesContext = FacesContext.getCurrentInstance();
141:
142: FacesMessageUtil.error(facesContext, key);
143: }
144:
145: protected void addInfoMessage(String key) {
146: FacesContext facesContext = FacesContext.getCurrentInstance();
147:
148: FacesMessageUtil.info(facesContext, key);
149: }
150:
151: private static Log _log = LogFactory
152: .getLog(PortletPreferencesManagedBean.class);
153:
154: private PortletPreferences _portletPreferences;
155: private Map _preferences;
156:
157: }
|