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.portlet.portletconfiguration.action;
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.util.GetterUtil;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.model.PublicRenderParameter;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.security.permission.PermissionChecker;
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.portal.util.WebKeys;
036    import com.liferay.portlet.PortletPreferencesFactoryUtil;
037    import com.liferay.portlet.portletconfiguration.util.ConfigurationActionRequest;
038    import com.liferay.portlet.portletconfiguration.util.ConfigurationRenderRequest;
039    import com.liferay.portlet.portletconfiguration.util.ConfigurationResourceRequest;
040    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
041    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
042    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierComparator;
043    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierConfigurationComparator;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.HashSet;
048    import java.util.List;
049    import java.util.Set;
050    import java.util.TreeSet;
051    
052    import javax.portlet.ActionRequest;
053    import javax.portlet.PortletPreferences;
054    import javax.portlet.PortletRequest;
055    import javax.portlet.RenderRequest;
056    import javax.portlet.ResourceRequest;
057    
058    import javax.servlet.ServletContext;
059    import javax.servlet.http.HttpServletRequest;
060    
061    /**
062     * @author Jorge Ferrer
063     */
064    public class ActionUtil {
065    
066            public static PortletPreferences getLayoutPortletSetup(
067                            PortletRequest portletRequest, Portlet portlet)
068                    throws SystemException {
069    
070                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
071                            WebKeys.THEME_DISPLAY);
072    
073                    Layout layout = themeDisplay.getLayout();
074    
075                    return PortletPreferencesFactoryUtil.getLayoutPortletSetup(
076                            layout, portlet.getPortletId());
077            }
078    
079            public static void getLayoutPublicRenderParameters(
080                            PortletRequest portletRequest)
081                    throws Exception {
082    
083                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
084                            WebKeys.THEME_DISPLAY);
085    
086                    Set<String> identifiers = new HashSet<String>();
087    
088                    Set<PublicRenderParameter> publicRenderParameters =
089                            new TreeSet<PublicRenderParameter>(
090                                    new PublicRenderParameterIdentifierComparator());
091    
092                    LayoutTypePortlet layoutTypePortlet =
093                            themeDisplay.getLayoutTypePortlet();
094    
095                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
096    
097                    for (Portlet portlet : portlets) {
098                            for (PublicRenderParameter publicRenderParameter :
099                                            portlet.getPublicRenderParameters()) {
100    
101                                    if (!identifiers.contains(
102                                                    publicRenderParameter.getIdentifier())) {
103    
104                                            identifiers.add(publicRenderParameter.getIdentifier());
105    
106                                            publicRenderParameters.add(publicRenderParameter);
107                                    }
108                            }
109                    }
110    
111                    portletRequest.setAttribute(
112                            WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
113            }
114    
115            public static void getPublicRenderParameterConfigurationList(
116                            PortletRequest portletRequest, Portlet portlet)
117                    throws Exception {
118    
119                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
120                            WebKeys.THEME_DISPLAY);
121    
122                    Layout layout = themeDisplay.getLayout();
123    
124                    PortletPreferences preferences =
125                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
126                                    layout, portlet.getPortletId());
127    
128                    List<PublicRenderParameterConfiguration>
129                            publicRenderParameterConfigurations =
130                                    new ArrayList<PublicRenderParameterConfiguration>();
131    
132                    for (PublicRenderParameter publicRenderParameter :
133                                    portlet.getPublicRenderParameters()) {
134    
135                            String mappingKey =
136                                    PublicRenderParameterConfiguration.getMappingKey(
137                                            publicRenderParameter);
138                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
139                                    publicRenderParameter);
140    
141                            String mappingValue = null;
142                            boolean ignoreValue = false;
143    
144                            if (SessionErrors.isEmpty(portletRequest)) {
145                                    mappingValue = preferences.getValue(mappingKey, null);
146                                    ignoreValue = GetterUtil.getBoolean(
147                                            preferences.getValue(ignoreKey, null));
148                            }
149                            else {
150                                    mappingValue = ParamUtil.getString(portletRequest, mappingKey);
151                                    ignoreValue = GetterUtil.getBoolean(
152                                            ParamUtil.getString(portletRequest, ignoreKey));
153                            }
154    
155                            publicRenderParameterConfigurations.add(
156                                    new PublicRenderParameterConfiguration(
157                                            publicRenderParameter, mappingValue, ignoreValue));
158                    }
159    
160                    Collections.sort(
161                            publicRenderParameterConfigurations,
162                            new PublicRenderParameterIdentifierConfigurationComparator());
163    
164                    portletRequest.setAttribute(
165                            WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
166                            publicRenderParameterConfigurations);
167            }
168    
169            public static ActionRequest getWrappedActionRequest(
170                            ActionRequest actionRequest, PortletPreferences portletPreferences)
171                    throws PortalException, SystemException {
172    
173                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
174                            actionRequest);
175    
176                    portletPreferences = getPortletPreferences(
177                            request, actionRequest.getPreferences(), portletPreferences);
178    
179                    return new ConfigurationActionRequest(
180                            actionRequest, portletPreferences);
181            }
182    
183            public static RenderRequest getWrappedRenderRequest(
184                            RenderRequest renderRequest, PortletPreferences portletPreferences)
185                    throws PortalException, SystemException {
186    
187                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
188                            renderRequest);
189    
190                    portletPreferences = getPortletPreferences(
191                            request, renderRequest.getPreferences(), portletPreferences);
192    
193                    renderRequest = new ConfigurationRenderRequest(
194                            renderRequest, portletPreferences);
195    
196                    request.setAttribute(
197                            JavaConstants.JAVAX_PORTLET_REQUEST, renderRequest);
198    
199                    return renderRequest;
200            }
201    
202            public static ResourceRequest getWrappedResourceRequest(
203                            ResourceRequest resourceRequest,
204                            PortletPreferences portletPreferences)
205                    throws PortalException, SystemException {
206    
207                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
208                            resourceRequest);
209    
210                    portletPreferences = getPortletPreferences(
211                            request, resourceRequest.getPreferences(), portletPreferences);
212    
213                    return new ConfigurationResourceRequest(
214                            resourceRequest, portletPreferences);
215            }
216    
217            protected static Portlet getPortlet(PortletRequest portletRequest)
218                    throws Exception {
219    
220                    long companyId = PortalUtil.getCompanyId(portletRequest);
221    
222                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
223                            WebKeys.THEME_DISPLAY);
224    
225                    PermissionChecker permissionChecker =
226                            themeDisplay.getPermissionChecker();
227    
228                    String portletId = ParamUtil.getString(
229                            portletRequest, "portletResource");
230    
231                    if (!PortletPermissionUtil.contains(
232                                    permissionChecker, themeDisplay.getLayout(), portletId,
233                                    ActionKeys.CONFIGURATION)) {
234    
235                            throw new PrincipalException();
236                    }
237    
238                    return PortletLocalServiceUtil.getPortletById(companyId, portletId);
239            }
240    
241            protected static PortletPreferences getPortletPreferences(
242                            HttpServletRequest request,
243                            PortletPreferences portletConfigPortletPreferences,
244                            PortletPreferences portletPreferences)
245                    throws PortalException, SystemException {
246    
247                    String portletResource = ParamUtil.getString(
248                            request, "portletResource");
249    
250                    if (Validator.isNull(portletResource)) {
251                            return portletConfigPortletPreferences;
252                    }
253    
254                    if (portletPreferences != null) {
255                            return portletPreferences;
256                    }
257    
258                    return PortletPreferencesFactoryUtil.getPortletSetup(
259                            request, portletResource);
260            }
261    
262            protected static String getTitle(
263                            Portlet portlet, RenderRequest renderRequest)
264                    throws Exception {
265    
266                    ServletContext servletContext =
267                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
268    
269                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
270                            WebKeys.THEME_DISPLAY);
271    
272                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
273                            renderRequest);
274    
275                    PortletPreferences portletPreferences = getLayoutPortletSetup(
276                            renderRequest, portlet);
277    
278                    portletPreferences = getPortletPreferences(
279                            request, renderRequest.getPreferences(), portletPreferences);
280    
281                    String title = PortletConfigurationUtil.getPortletTitle(
282                            portletPreferences, themeDisplay.getLanguageId());
283    
284                    if (Validator.isNull(title)) {
285                            title = PortalUtil.getPortletTitle(
286                                    portlet, servletContext, themeDisplay.getLocale());
287                    }
288    
289                    return title;
290            }
291    
292    }