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