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            }
123    
124            public String render(
125                            PortletConfig portletConfig, RenderRequest renderRequest,
126                            RenderResponse renderResponse)
127                    throws Exception {
128    
129                    PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
130    
131                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
132    
133                    if (Validator.isNotNull(configJSP)) {
134                            return configJSP;
135                    }
136    
137                    return "/configuration.jsp";
138            }
139    
140            public void serveResource(
141                            PortletConfig portletConfig, ResourceRequest resourceRequest,
142                            ResourceResponse resourceResponse)
143                    throws Exception {
144            }
145    
146            public void setPreference(
147                    PortletRequest portletRequest, String name, String value) {
148    
149                    setPreference(portletRequest, name, new String[] {value});
150            }
151    
152            public void setPreference(
153                    PortletRequest portletRequest, String name, String[] values) {
154    
155                    Map<String, String[]> portletPreferencesMap =
156                            (Map<String, String[]>)portletRequest.getAttribute(
157                                    WebKeys.PORTLET_PREFERENCES_MAP);
158    
159                    if (portletPreferencesMap == null) {
160                            portletPreferencesMap = new HashMap<String, String[]>();
161    
162                            portletRequest.setAttribute(
163                                    WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
164                    }
165    
166                    portletPreferencesMap.put(name, values);
167            }
168    
169            protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
170                    throws SystemException {
171    
172                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
173                            WebKeys.THEME_DISPLAY);
174    
175                    String portletResource = ParamUtil.getString(
176                            portletRequest, "portletResource");
177    
178                    Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
179                            themeDisplay.getCompanyId(), portletResource);
180    
181                    ServletContext servletContext =
182                            (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
183    
184                    PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
185                            selPortlet, servletContext);
186    
187                    return selPortletConfig;
188            }
189    
190    }