001    /**
002     * Copyright (c) 2000-2013 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.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    
034    import java.util.HashMap;
035    import java.util.Map;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletPreferences;
041    import javax.portlet.PortletRequest;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    import javax.portlet.ResourceRequest;
045    import javax.portlet.ResourceResponse;
046    import javax.portlet.ValidatorException;
047    
048    import javax.servlet.ServletContext;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Julio Camarero
053     */
054    public class DefaultConfigurationAction
055            implements ConfigurationAction, ResourceServingConfigurationAction {
056    
057            public static final String PREFERENCES_PREFIX = "preferences--";
058    
059            public String getLocalizedParameter(
060                    PortletRequest portletRequest, String name) {
061    
062                    String languageId = ParamUtil.getString(portletRequest, "languageId");
063    
064                    return getParameter(
065                            portletRequest,
066                            name.concat(StringPool.UNDERLINE).concat(languageId));
067            }
068    
069            public String getParameter(PortletRequest portletRequest, String name) {
070                    name = PREFERENCES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
071    
072                    return ParamUtil.getString(portletRequest, name);
073            }
074    
075            @Override
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 = actionRequest.getPreferences();
101    
102                    for (Map.Entry<String, String> entry : properties.entrySet()) {
103                            String name = entry.getKey();
104                            String value = entry.getValue();
105    
106                            portletPreferences.setValue(name, value);
107                    }
108    
109                    Map<String, String[]> portletPreferencesMap =
110                            (Map<String, String[]>)actionRequest.getAttribute(
111                                    WebKeys.PORTLET_PREFERENCES_MAP);
112    
113                    if (portletPreferencesMap != null) {
114                            for (Map.Entry<String, String[]> entry :
115                                            portletPreferencesMap.entrySet()) {
116    
117                                    String name = entry.getKey();
118                                    String[] values = entry.getValue();
119    
120                                    portletPreferences.setValues(name, values);
121                            }
122                    }
123    
124                    if (SessionErrors.isEmpty(actionRequest)) {
125                            try {
126                                    portletPreferences.store();
127                            }
128                            catch (ValidatorException ve) {
129                                    SessionErrors.add(
130                                            actionRequest, ValidatorException.class.getName(), ve);
131    
132                                    return;
133                            }
134    
135                            LiferayPortletConfig liferayPortletConfig =
136                                    (LiferayPortletConfig)portletConfig;
137    
138                            SessionMessages.add(
139                                    actionRequest,
140                                    liferayPortletConfig.getPortletId() +
141                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
142                                    portletResource);
143    
144                            SessionMessages.add(
145                                    actionRequest,
146                                    liferayPortletConfig.getPortletId() +
147                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
148                    }
149            }
150    
151            @Override
152            public String render(
153                            PortletConfig portletConfig, RenderRequest renderRequest,
154                            RenderResponse renderResponse)
155                    throws Exception {
156    
157                    PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
158    
159                    String configTemplate = selPortletConfig.getInitParameter(
160                            "config-template");
161    
162                    if (Validator.isNotNull(configTemplate)) {
163                            return configTemplate;
164                    }
165    
166                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
167    
168                    if (Validator.isNotNull(configJSP)) {
169                            return configJSP;
170                    }
171    
172                    return "/configuration.jsp";
173            }
174    
175            @Override
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    }