001
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
031 public class PortletPreferencesManagedBean {
032
033 public PortletPreferencesManagedBean() {
034
035
036
037
038
039
040 FacesContext facesContext = FacesContext.getCurrentInstance();
041
042 _portletPreferences = JSFPortletUtil.getPortletPreferences(
043 facesContext);
044
045
046
047
048
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 }