001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.settings.ModifiableSettings;
021    import com.liferay.portal.kernel.settings.Settings;
022    import com.liferay.portal.kernel.settings.SettingsFactory;
023    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
024    import com.liferay.portal.kernel.util.Constants;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.LocaleUtil;
027    import com.liferay.portal.kernel.util.LocalizationUtil;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.PropertiesParamUtil;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.UnicodeProperties;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.util.WebKeys;
035    import com.liferay.portal.model.Layout;
036    import com.liferay.portal.model.Portlet;
037    import com.liferay.portal.security.permission.ActionKeys;
038    import com.liferay.portal.service.PortletLocalServiceUtil;
039    import com.liferay.portal.service.permission.PortletPermissionUtil;
040    import com.liferay.portal.theme.ThemeDisplay;
041    import com.liferay.portal.util.PortalUtil;
042    import com.liferay.portlet.PortletConfigFactoryUtil;
043    
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Map;
047    
048    import javax.portlet.ActionRequest;
049    import javax.portlet.ActionResponse;
050    import javax.portlet.PortletConfig;
051    import javax.portlet.PortletRequest;
052    import javax.portlet.RenderRequest;
053    import javax.portlet.RenderResponse;
054    import javax.portlet.ResourceRequest;
055    import javax.portlet.ResourceResponse;
056    import javax.portlet.ValidatorException;
057    
058    import javax.servlet.ServletContext;
059    
060    /**
061     * @author Iv??n Zaera
062     */
063    public class SettingsConfigurationAction
064            extends LiferayPortlet
065            implements ConfigurationAction, ResourceServingConfigurationAction {
066    
067            public SettingsConfigurationAction() {
068                    setParameterNamePrefix("preferences--");
069            }
070    
071            public String getLocalizedParameter(
072                    PortletRequest portletRequest, String name) {
073    
074                    String languageId = ParamUtil.getString(portletRequest, "languageId");
075    
076                    return getLocalizedParameter(portletRequest, name, languageId);
077            }
078    
079            public String getLocalizedParameter(
080                    PortletRequest portletRequest, String name, String languageId) {
081    
082                    return getParameter(
083                            portletRequest,
084                            LocalizationUtil.getLocalizedName(name, languageId));
085            }
086    
087            public String getParameter(PortletRequest portletRequest, String name) {
088                    name = _parameterNamePrefix + name + StringPool.DOUBLE_DASH;
089    
090                    return ParamUtil.getString(portletRequest, name);
091            }
092    
093            @Override
094            public void processAction(
095                            PortletConfig portletConfig, ActionRequest actionRequest,
096                            ActionResponse actionResponse)
097                    throws Exception {
098    
099                    updateMultiValuedKeys(actionRequest);
100    
101                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
102    
103                    if (!cmd.equals(Constants.UPDATE)) {
104                            return;
105                    }
106    
107                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
108                            WebKeys.THEME_DISPLAY);
109    
110                    Layout layout = PortletConfigurationLayoutUtil.getLayout(themeDisplay);
111    
112                    String portletResource = ParamUtil.getString(
113                            actionRequest, "portletResource");
114    
115                    PortletPermissionUtil.check(
116                            themeDisplay.getPermissionChecker(), themeDisplay.getScopeGroupId(),
117                            layout, portletResource, ActionKeys.CONFIGURATION);
118    
119                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
120                            actionRequest, _parameterNamePrefix);
121    
122                    Settings settings = getSettings(actionRequest);
123    
124                    ModifiableSettings modifiableSettings =
125                            settings.getModifiableSettings();
126    
127                    for (Map.Entry<String, String> entry : properties.entrySet()) {
128                            String name = entry.getKey();
129                            String value = entry.getValue();
130    
131                            String oldValue = settings.getValue(name, null);
132    
133                            if (!StringUtil.equalsIgnoreBreakLine(value, oldValue)) {
134                                    modifiableSettings.setValue(name, value);
135                            }
136                    }
137    
138                    Map<String, String[]> portletPreferencesMap =
139                            (Map<String, String[]>)actionRequest.getAttribute(
140                                    WebKeys.PORTLET_PREFERENCES_MAP);
141    
142                    if (portletPreferencesMap != null) {
143                            for (Map.Entry<String, String[]> entry :
144                                            portletPreferencesMap.entrySet()) {
145    
146                                    String name = entry.getKey();
147                                    String[] values = entry.getValue();
148    
149                                    String[] oldValues = settings.getValues(name, null);
150    
151                                    if (!Validator.equals(values, oldValues)) {
152                                            modifiableSettings.setValues(name, values);
153                                    }
154                            }
155                    }
156    
157                    postProcess(themeDisplay.getCompanyId(), actionRequest, settings);
158    
159                    if (SessionErrors.isEmpty(actionRequest)) {
160                            try {
161                                    modifiableSettings.store();
162                            }
163                            catch (ValidatorException ve) {
164                                    SessionErrors.add(
165                                            actionRequest, ValidatorException.class.getName(), ve);
166    
167                                    return;
168                            }
169    
170                            SessionMessages.add(
171                                    actionRequest,
172                                    PortalUtil.getPortletId(actionRequest) +
173                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
174                                    portletResource);
175    
176                            SessionMessages.add(
177                                    actionRequest,
178                                    PortalUtil.getPortletId(actionRequest) +
179                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
180    
181                            String redirect = PortalUtil.escapeRedirect(
182                                    ParamUtil.getString(actionRequest, "redirect"));
183    
184                            if (Validator.isNotNull(redirect)) {
185                                    actionResponse.sendRedirect(redirect);
186                            }
187                    }
188            }
189    
190            @Override
191            public String render(
192                            PortletConfig portletConfig, RenderRequest renderRequest,
193                            RenderResponse renderResponse)
194                    throws Exception {
195    
196                    PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
197    
198                    String configTemplate = selPortletConfig.getInitParameter(
199                            "config-template");
200    
201                    if (Validator.isNotNull(configTemplate)) {
202                            return configTemplate;
203                    }
204    
205                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
206    
207                    if (Validator.isNotNull(configJSP)) {
208                            return configJSP;
209                    }
210    
211                    return "/configuration.jsp";
212            }
213    
214            @Override
215            public void serveResource(
216                            PortletConfig portletConfig, ResourceRequest resourceRequest,
217                            ResourceResponse resourceResponse)
218                    throws Exception {
219            }
220    
221            public void setPreference(
222                    PortletRequest portletRequest, String name, String value) {
223    
224                    setPreference(portletRequest, name, new String[] {value});
225            }
226    
227            public void setPreference(
228                    PortletRequest portletRequest, String name, String[] values) {
229    
230                    Map<String, String[]> portletPreferencesMap =
231                            (Map<String, String[]>)portletRequest.getAttribute(
232                                    WebKeys.PORTLET_PREFERENCES_MAP);
233    
234                    if (portletPreferencesMap == null) {
235                            portletPreferencesMap = new HashMap<String, String[]>();
236    
237                            portletRequest.setAttribute(
238                                    WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
239                    }
240    
241                    portletPreferencesMap.put(name, values);
242            }
243    
244            protected PortletConfig getSelPortletConfig(PortletRequest portletRequest) {
245                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
246                            WebKeys.THEME_DISPLAY);
247    
248                    String portletResource = ParamUtil.getString(
249                            portletRequest, "portletResource");
250    
251                    Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
252                            themeDisplay.getCompanyId(), portletResource);
253    
254                    ServletContext servletContext =
255                            (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
256    
257                    PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
258                            selPortlet, servletContext);
259    
260                    return selPortletConfig;
261            }
262    
263            protected Settings getSettings(ActionRequest actionRequest)
264                    throws PortalException {
265    
266                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
267                            WebKeys.THEME_DISPLAY);
268    
269                    String serviceName = ParamUtil.getString(actionRequest, "serviceName");
270    
271                    String settingsScope = ParamUtil.getString(
272                            actionRequest, "settingsScope");
273    
274                    if (settingsScope.equals("company")) {
275                            return SettingsFactoryUtil.getCompanyServiceSettings(
276                                    themeDisplay.getCompanyId(), serviceName);
277                    }
278                    else if (settingsScope.equals("group")) {
279                            return SettingsFactoryUtil.getGroupServiceSettings(
280                                    themeDisplay.getSiteGroupId(), serviceName);
281                    }
282                    else if (settingsScope.equals("portletInstance")) {
283                            String portletResource = ParamUtil.getString(
284                                    actionRequest, "portletResource");
285    
286                            return SettingsFactoryUtil.getPortletInstanceSettings(
287                                    themeDisplay.getLayout(), portletResource);
288                    }
289    
290                    throw new IllegalArgumentException(
291                            "Invalid settings scope " + settingsScope);
292            }
293    
294            protected String getSettingsId(ActionRequest actionRequest) {
295                    String settingsId = ParamUtil.getString(actionRequest, "serviceName");
296    
297                    String settingsScope = ParamUtil.getString(
298                            actionRequest, "settingsScope");
299    
300                    if (settingsScope.equals("portletInstance")) {
301                            settingsId = ParamUtil.getString(actionRequest, "portletResource");
302                    }
303    
304                    return settingsId;
305            }
306    
307            @SuppressWarnings("unused")
308            protected void postProcess(
309                            long companyId, PortletRequest portletRequest, Settings settings)
310                    throws PortalException {
311            }
312    
313            protected void setParameterNamePrefix(String parameterNamePrefix) {
314                    _parameterNamePrefix = parameterNamePrefix;
315            }
316    
317            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
318                    String settingsId = getSettingsId(actionRequest);
319    
320                    SettingsFactory settingsFactory =
321                            SettingsFactoryUtil.getSettingsFactory();
322    
323                    List<String> multiValuedKeys = settingsFactory.getMultiValuedKeys(
324                            settingsId);
325    
326                    for (String multiValuedKey : multiValuedKeys) {
327                            String multiValuedValue = getParameter(
328                                    actionRequest, multiValuedKey);
329    
330                            if (multiValuedValue != null) {
331                                    setPreference(
332                                            actionRequest, multiValuedKey,
333                                            StringUtil.split(multiValuedValue));
334                            }
335                    }
336            }
337    
338            protected void validateEmail(
339                    ActionRequest actionRequest, String emailParam) {
340    
341                    boolean emailEnabled = GetterUtil.getBoolean(
342                            getParameter(actionRequest, emailParam + "Enabled"));
343                    String emailSubject = null;
344                    String emailBody = null;
345    
346                    String languageId = LocaleUtil.toLanguageId(
347                            LocaleUtil.getSiteDefault());
348    
349                    emailSubject = getLocalizedParameter(
350                            actionRequest, emailParam + "Subject", languageId);
351                    emailBody = getLocalizedParameter(
352                            actionRequest, emailParam + "Body", languageId);
353    
354                    if (emailEnabled) {
355                            if (Validator.isNull(emailSubject)) {
356                                    SessionErrors.add(actionRequest, emailParam + "Subject");
357                            }
358                            else if (Validator.isNull(emailBody)) {
359                                    SessionErrors.add(actionRequest, emailParam + "Body");
360                            }
361                    }
362            }
363    
364            protected void validateEmailFrom(ActionRequest actionRequest) {
365                    String emailFromName = getParameter(actionRequest, "emailFromName");
366                    String emailFromAddress = getParameter(
367                            actionRequest, "emailFromAddress");
368    
369                    if (Validator.isNull(emailFromName)) {
370                            SessionErrors.add(actionRequest, "emailFromName");
371                    }
372                    else if (!Validator.isEmailAddress(emailFromAddress) &&
373                                     !Validator.isVariableTerm(emailFromAddress)) {
374    
375                            SessionErrors.add(actionRequest, "emailFromAddress");
376                    }
377            }
378    
379            private String _parameterNamePrefix;
380    
381    }