001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.service.PortletLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portlet.PortletConfigFactoryUtil;
031    import com.liferay.portlet.PortletPreferencesFactoryUtil;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.PortletPreferences;
040    import javax.portlet.PortletRequest;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    import javax.portlet.ResourceRequest;
044    import javax.portlet.ResourceResponse;
045    
046    import javax.servlet.ServletContext;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Julio Camarero
051     */
052    public class DefaultConfigurationAction
053            implements ConfigurationAction, ResourceServingConfigurationAction {
054    
055            public final static String PREFERENCES_PREFIX = "preferences--";
056    
057            public String getLocalizedParameter(
058                    PortletRequest portletRequest, String name) {
059    
060                    String languageId = ParamUtil.getString(portletRequest, "languageId");
061    
062                    return getParameter(
063                            portletRequest,
064                            name.concat(StringPool.UNDERLINE).concat(languageId));
065            }
066    
067            public String getParameter(PortletRequest portletRequest, String name) {
068                    name = PREFERENCES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
069    
070                    return ParamUtil.getString(portletRequest, name);
071            }
072    
073            public void processAction(
074                            PortletConfig portletConfig, ActionRequest actionRequest,
075                            ActionResponse actionResponse)
076                    throws Exception {
077    
078                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
079    
080                    if (!cmd.equals(Constants.UPDATE)) {
081                            return;
082                    }
083    
084                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
085                            actionRequest, PREFERENCES_PREFIX);
086    
087                    String portletResource = ParamUtil.getString(
088                            actionRequest, "portletResource");
089    
090                    PortletPreferences portletPreferences =
091                            PortletPreferencesFactoryUtil.getPortletSetup(
092                                    actionRequest, portletResource);
093    
094                    for (Map.Entry<String, String> entry : properties.entrySet()) {
095                            String name = entry.getKey();
096                            String value = entry.getValue();
097    
098                            portletPreferences.setValue(name, value);
099                    }
100    
101                    Map<String, String[]> portletPreferencesMap =
102                            (Map<String, String[]>)actionRequest.getAttribute(
103                                    WebKeys.PORTLET_PREFERENCES_MAP);
104    
105                    if (portletPreferencesMap != null) {
106                            for (Map.Entry<String, String[]> entry :
107                                            portletPreferencesMap.entrySet()) {
108    
109                                    String name = entry.getKey();
110                                    String[] values = entry.getValue();
111    
112                                    portletPreferences.setValues(name, values);
113                            }
114                    }
115    
116                    if (SessionErrors.isEmpty(actionRequest)) {
117                            portletPreferences.store();
118    
119                            SessionMessages.add(
120                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
121    
122                            SessionMessages.add(
123                                    actionRequest, portletConfig.getPortletName() + ".doRefresh",
124                                    portletResource);
125                    }
126            }
127    
128            public String render(
129                            PortletConfig portletConfig, RenderRequest renderRequest,
130                            RenderResponse renderResponse)
131                    throws Exception {
132    
133                    PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
134    
135                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
136    
137                    if (Validator.isNotNull(configJSP)) {
138                            return configJSP;
139                    }
140    
141                    return "/configuration.jsp";
142            }
143    
144            public void serveResource(
145                            PortletConfig portletConfig, ResourceRequest resourceRequest,
146                            ResourceResponse resourceResponse)
147                    throws Exception {
148            }
149    
150            public void setPreference(
151                    PortletRequest portletRequest, String name, String value) {
152    
153                    setPreference(portletRequest, name, new String[] {value});
154            }
155    
156            public void setPreference(
157                    PortletRequest portletRequest, String name, String[] values) {
158    
159                    Map<String, String[]> portletPreferencesMap =
160                            (Map<String, String[]>)portletRequest.getAttribute(
161                                    WebKeys.PORTLET_PREFERENCES_MAP);
162    
163                    if (portletPreferencesMap == null) {
164                            portletPreferencesMap = new HashMap<String, String[]>();
165    
166                            portletRequest.setAttribute(
167                                    WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
168                    }
169    
170                    portletPreferencesMap.put(name, values);
171            }
172    
173            protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
174                    throws SystemException {
175    
176                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
177                            WebKeys.THEME_DISPLAY);
178    
179                    String portletResource = ParamUtil.getString(
180                            portletRequest, "portletResource");
181    
182                    Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
183                            themeDisplay.getCompanyId(), portletResource);
184    
185                    ServletContext servletContext =
186                            (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
187    
188                    PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
189                            selPortlet, servletContext);
190    
191                    return selPortletConfig;
192            }
193    
194    }