001
014
015 package com.liferay.portal.kernel.portlet;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.servlet.SessionMessages;
020 import com.liferay.portal.kernel.util.Constants;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.PropertiesParamUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.util.WebKeys;
027 import com.liferay.portal.model.Portlet;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.service.PortletLocalServiceUtil;
030 import com.liferay.portal.service.permission.PortletPermissionUtil;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portlet.PortletConfigFactoryUtil;
033 import com.liferay.portlet.PortletPreferencesFactoryUtil;
034
035 import java.util.HashMap;
036 import java.util.Map;
037
038 import javax.portlet.ActionRequest;
039 import javax.portlet.ActionResponse;
040 import javax.portlet.PortletConfig;
041 import javax.portlet.PortletPreferences;
042 import javax.portlet.PortletRequest;
043 import javax.portlet.RenderRequest;
044 import javax.portlet.RenderResponse;
045 import javax.portlet.ResourceRequest;
046 import javax.portlet.ResourceResponse;
047 import javax.portlet.ValidatorException;
048
049 import javax.servlet.ServletContext;
050
051
055 public class DefaultConfigurationAction
056 implements ConfigurationAction, ResourceServingConfigurationAction {
057
058 public static final String PREFERENCES_PREFIX = "preferences--";
059
060 public String getLocalizedParameter(
061 PortletRequest portletRequest, String name) {
062
063 String languageId = ParamUtil.getString(portletRequest, "languageId");
064
065 return getParameter(
066 portletRequest,
067 name.concat(StringPool.UNDERLINE).concat(languageId));
068 }
069
070 public String getParameter(PortletRequest portletRequest, String name) {
071 name = PREFERENCES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
072
073 return ParamUtil.getString(portletRequest, name);
074 }
075
076 public void processAction(
077 PortletConfig portletConfig, ActionRequest actionRequest,
078 ActionResponse actionResponse)
079 throws Exception {
080
081 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
082
083 if (!cmd.equals(Constants.UPDATE)) {
084 return;
085 }
086
087 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
088 WebKeys.THEME_DISPLAY);
089
090 UnicodeProperties properties = PropertiesParamUtil.getProperties(
091 actionRequest, PREFERENCES_PREFIX);
092
093 String portletResource = ParamUtil.getString(
094 actionRequest, "portletResource");
095
096 PortletPermissionUtil.check(
097 themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
098 portletResource, ActionKeys.CONFIGURATION);
099
100 PortletPreferences portletPreferences =
101 PortletPreferencesFactoryUtil.getPortletSetup(
102 actionRequest, portletResource);
103
104 for (Map.Entry<String, String> entry : properties.entrySet()) {
105 String name = entry.getKey();
106 String value = entry.getValue();
107
108 portletPreferences.setValue(name, value);
109 }
110
111 Map<String, String[]> portletPreferencesMap =
112 (Map<String, String[]>)actionRequest.getAttribute(
113 WebKeys.PORTLET_PREFERENCES_MAP);
114
115 if (portletPreferencesMap != null) {
116 for (Map.Entry<String, String[]> entry :
117 portletPreferencesMap.entrySet()) {
118
119 String name = entry.getKey();
120 String[] values = entry.getValue();
121
122 portletPreferences.setValues(name, values);
123 }
124 }
125
126 if (SessionErrors.isEmpty(actionRequest)) {
127 try {
128 portletPreferences.store();
129 }
130 catch (ValidatorException ve) {
131 SessionErrors.add(
132 actionRequest, ValidatorException.class.getName(), ve);
133
134 return;
135 }
136
137 LiferayPortletConfig liferayPortletConfig =
138 (LiferayPortletConfig)portletConfig;
139
140 SessionMessages.add(
141 actionRequest,
142 liferayPortletConfig.getPortletId() +
143 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
144 portletResource);
145
146 SessionMessages.add(
147 actionRequest,
148 liferayPortletConfig.getPortletId() +
149 SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
150 }
151 }
152
153 public String render(
154 PortletConfig portletConfig, RenderRequest renderRequest,
155 RenderResponse renderResponse)
156 throws Exception {
157
158 PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
159
160 String configTemplate = selPortletConfig.getInitParameter(
161 "config-template");
162
163 if (Validator.isNotNull(configTemplate)) {
164 return configTemplate;
165 }
166
167 String configJSP = selPortletConfig.getInitParameter("config-jsp");
168
169 if (Validator.isNotNull(configJSP)) {
170 return configJSP;
171 }
172
173 return "/configuration.jsp";
174 }
175
176 public void serveResource(
177 PortletConfig portletConfig, ResourceRequest resourceRequest,
178 ResourceResponse resourceResponse)
179 throws Exception {
180 }
181
182 public void setPreference(
183 PortletRequest portletRequest, String name, String value) {
184
185 setPreference(portletRequest, name, new String[] {value});
186 }
187
188 public void setPreference(
189 PortletRequest portletRequest, String name, String[] values) {
190
191 Map<String, String[]> portletPreferencesMap =
192 (Map<String, String[]>)portletRequest.getAttribute(
193 WebKeys.PORTLET_PREFERENCES_MAP);
194
195 if (portletPreferencesMap == null) {
196 portletPreferencesMap = new HashMap<String, String[]>();
197
198 portletRequest.setAttribute(
199 WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
200 }
201
202 portletPreferencesMap.put(name, values);
203 }
204
205 protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
206 throws SystemException {
207
208 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
209 WebKeys.THEME_DISPLAY);
210
211 String portletResource = ParamUtil.getString(
212 portletRequest, "portletResource");
213
214 Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
215 themeDisplay.getCompanyId(), portletResource);
216
217 ServletContext servletContext =
218 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
219
220 PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
221 selPortlet, servletContext);
222
223 return selPortletConfig;
224 }
225
226 }