001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.jsf.common;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Enumeration;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import javax.faces.context.FacesContext;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Neil Griffin
030     */
031    public class PortletPreferencesManagedBean {
032    
033            public PortletPreferencesManagedBean() {
034    
035                    // Store the portlet preferences as a bean property because of ICE-1625.
036                    // When using normal JSF, this constructor will get called each time a
037                    // request is made. This is a little inefficient, but it's a coding
038                    // tradeoff to make things work with both normal JSF and ICEfaces 1.6.0.
039    
040                    FacesContext facesContext = FacesContext.getCurrentInstance();
041    
042                    _portletPreferences =
043                            JSFPortletUtil.getPortletPreferences(facesContext);
044    
045                    // Portlet preferences are backed by a map of string arrays. This makes
046                    // the JSP syntax a little funky, so in order to make the syntax easier,
047                    // copy each name and its first value into a new map where the name and
048                    // value are both strings.
049    
050                    _preferences = new HashMap<String, String>();
051    
052                    Enumeration<String> enu = _portletPreferences.getNames();
053    
054                    while (enu.hasMoreElements()) {
055                            String name = enu.nextElement();
056    
057                            String value = _portletPreferences.getValue(name, null);
058    
059                            _preferences.put(name, value);
060    
061                            if (_log.isDebugEnabled()) {
062                                    _log.debug("{name=" + name + ", value=" + value + "}");
063                            }
064                    }
065            }
066    
067            public Map<String, String> getPreferences() {
068                    return _preferences;
069            }
070    
071            public String resetDefaultValues() {
072                    try {
073                            Enumeration<String> enu = _portletPreferences.getNames();
074    
075                            while (enu.hasMoreElements()) {
076                                    String name = enu.nextElement();
077    
078                                    if (!_portletPreferences.isReadOnly(name)) {
079                                            _portletPreferences.reset(name);
080    
081                                            String value = _portletPreferences.getValue(name, null);
082    
083                                            _preferences.put(name, value);
084    
085                                            _portletPreferences.store();
086                                    }
087                            }
088    
089                            addInfoMessage("you-have-successfully-reset-your-preferences");
090    
091                            return ActionOutcomes.SUCCESS;
092                    }
093                    catch (Exception e) {
094                            _log.error(e, e);
095    
096                            addErrorMessage(
097                                    "an-error-occurred-while-resetting-your-preferences");
098    
099                            return ActionOutcomes.FAILURE;
100                    }
101            }
102    
103            public String submit() {
104                    try {
105                            Enumeration<String> enu = _portletPreferences.getNames();
106    
107                            while (enu.hasMoreElements()) {
108                                    String name = enu.nextElement();
109    
110                                    if (!_portletPreferences.isReadOnly(name)) {
111                                            String value = _preferences.get(name);
112    
113                                            _portletPreferences.setValue(name, value);
114                                    }
115                            }
116    
117                            _portletPreferences.store();
118    
119                            addInfoMessage("you-have-successfully-updated-your-preferences");
120    
121                            return ActionOutcomes.SUCCESS;
122                    }
123                    catch (Exception e) {
124                            _log.error(e, e);
125    
126                            addErrorMessage(
127                                    "an-error-occurred-while-updating-your-preferences");
128    
129                            return ActionOutcomes.FAILURE;
130                    }
131            }
132    
133            protected void addErrorMessage(String key) {
134                    FacesContext facesContext = FacesContext.getCurrentInstance();
135    
136                    FacesMessageUtil.error(facesContext, key);
137            }
138    
139            protected void addInfoMessage(String key) {
140                    FacesContext facesContext = FacesContext.getCurrentInstance();
141    
142                    FacesMessageUtil.info(facesContext, key);
143            }
144    
145            private static Log _log = LogFactoryUtil.getLog(
146                    PortletPreferencesManagedBean.class);
147    
148            private PortletPreferences _portletPreferences;
149            private Map<String, String> _preferences;
150    
151    }