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