001    /**
002     * Copyright (c) 2000-2013 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.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 PortletPreferences getStrictLayoutPortletSetup(
086                            PortletRequest portletRequest, Portlet portlet)
087                    throws SystemException {
088    
089                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
090                            WebKeys.THEME_DISPLAY);
091    
092                    Layout layout = themeDisplay.getLayout();
093    
094                    return PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
095                            layout, portlet.getPortletId());
096            }
097    
098            public static void getLayoutPublicRenderParameters(
099                            PortletRequest portletRequest)
100                    throws Exception {
101    
102                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
103                            WebKeys.THEME_DISPLAY);
104    
105                    Set<String> identifiers = new HashSet<String>();
106    
107                    Set<PublicRenderParameter> publicRenderParameters =
108                            new TreeSet<PublicRenderParameter>(
109                                    new PublicRenderParameterIdentifierComparator());
110    
111                    LayoutTypePortlet layoutTypePortlet =
112                            themeDisplay.getLayoutTypePortlet();
113    
114                    for (Portlet portlet : layoutTypePortlet.getAllPortlets()) {
115                            for (PublicRenderParameter publicRenderParameter :
116                                            portlet.getPublicRenderParameters()) {
117    
118                                    if (!identifiers.contains(
119                                                    publicRenderParameter.getIdentifier())) {
120    
121                                            identifiers.add(publicRenderParameter.getIdentifier());
122    
123                                            publicRenderParameters.add(publicRenderParameter);
124                                    }
125                            }
126                    }
127    
128                    portletRequest.setAttribute(
129                            WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
130            }
131    
132            public static void getPublicRenderParameterConfigurationList(
133                            PortletRequest portletRequest, Portlet portlet)
134                    throws Exception {
135    
136                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
137                            WebKeys.THEME_DISPLAY);
138    
139                    Layout layout = themeDisplay.getLayout();
140    
141                    PortletPreferences preferences =
142                            PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
143                                    layout, portlet.getPortletId());
144    
145                    List<PublicRenderParameterConfiguration>
146                            publicRenderParameterConfigurations =
147                                    new ArrayList<PublicRenderParameterConfiguration>();
148    
149                    for (PublicRenderParameter publicRenderParameter :
150                                    portlet.getPublicRenderParameters()) {
151    
152                            String mappingKey =
153                                    PublicRenderParameterConfiguration.getMappingKey(
154                                            publicRenderParameter);
155                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
156                                    publicRenderParameter);
157    
158                            String mappingValue = null;
159                            boolean ignoreValue = false;
160    
161                            if (SessionErrors.isEmpty(portletRequest)) {
162                                    mappingValue = preferences.getValue(mappingKey, null);
163                                    ignoreValue = GetterUtil.getBoolean(
164                                            preferences.getValue(ignoreKey, null));
165                            }
166                            else {
167                                    mappingValue = ParamUtil.getString(portletRequest, mappingKey);
168                                    ignoreValue = GetterUtil.getBoolean(
169                                            ParamUtil.getString(portletRequest, ignoreKey));
170                            }
171    
172                            publicRenderParameterConfigurations.add(
173                                    new PublicRenderParameterConfiguration(
174                                            publicRenderParameter, mappingValue, ignoreValue));
175                    }
176    
177                    Collections.sort(
178                            publicRenderParameterConfigurations,
179                            new PublicRenderParameterIdentifierConfigurationComparator());
180    
181                    portletRequest.setAttribute(
182                            WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
183                            publicRenderParameterConfigurations);
184            }
185    
186            public static ActionRequest getWrappedActionRequest(
187                            ActionRequest actionRequest, PortletPreferences portletPreferences)
188                    throws PortalException, SystemException {
189    
190                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
191                            actionRequest);
192    
193                    portletPreferences = getPortletPreferences(
194                            request, actionRequest.getPreferences(), portletPreferences);
195    
196                    return new ConfigurationActionRequest(
197                            actionRequest, portletPreferences);
198            }
199    
200            public static RenderRequest getWrappedRenderRequest(
201                            RenderRequest renderRequest, PortletPreferences portletPreferences)
202                    throws PortalException, SystemException {
203    
204                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
205                            renderRequest);
206    
207                    portletPreferences = getPortletPreferences(
208                            request, renderRequest.getPreferences(), portletPreferences);
209    
210                    renderRequest = new ConfigurationRenderRequest(
211                            renderRequest, portletPreferences);
212    
213                    request.setAttribute(
214                            JavaConstants.JAVAX_PORTLET_REQUEST, renderRequest);
215    
216                    return renderRequest;
217            }
218    
219            public static ResourceRequest getWrappedResourceRequest(
220                            ResourceRequest resourceRequest,
221                            PortletPreferences portletPreferences)
222                    throws PortalException, SystemException {
223    
224                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
225                            resourceRequest);
226    
227                    portletPreferences = getPortletPreferences(
228                            request, resourceRequest.getPreferences(), portletPreferences);
229    
230                    return new ConfigurationResourceRequest(
231                            resourceRequest, portletPreferences);
232            }
233    
234            protected static Portlet getPortlet(PortletRequest portletRequest)
235                    throws Exception {
236    
237                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
238                            WebKeys.THEME_DISPLAY);
239    
240                    PermissionChecker permissionChecker =
241                            themeDisplay.getPermissionChecker();
242    
243                    String portletId = ParamUtil.getString(
244                            portletRequest, "portletResource");
245    
246                    Layout layout = PortletConfigurationLayoutUtil.getLayout(themeDisplay);
247    
248                    if (!PortletPermissionUtil.contains(
249                                    permissionChecker, themeDisplay.getScopeGroupId(), layout,
250                                    portletId, ActionKeys.CONFIGURATION)) {
251    
252                            throw new PrincipalException();
253                    }
254    
255                    return PortletLocalServiceUtil.getPortletById(
256                            themeDisplay.getCompanyId(), portletId);
257            }
258    
259            protected static PortletPreferences getPortletPreferences(
260                            HttpServletRequest request,
261                            PortletPreferences portletConfigPortletPreferences,
262                            PortletPreferences portletPreferences)
263                    throws PortalException, SystemException {
264    
265                    String portletResource = ParamUtil.getString(
266                            request, "portletResource");
267    
268                    if (Validator.isNull(portletResource)) {
269                            return portletConfigPortletPreferences;
270                    }
271    
272                    if (portletPreferences != null) {
273                            return portletPreferences;
274                    }
275    
276                    return PortletPreferencesFactoryUtil.getPortletPreferences(
277                            request, portletResource);
278            }
279    
280            protected static PortletPreferences getPortletSetup(
281                            HttpServletRequest request,
282                            PortletPreferences portletConfigPortletSetup,
283                            PortletPreferences portletSetup)
284                    throws PortalException, SystemException {
285    
286                    String portletResource = ParamUtil.getString(
287                            request, "portletResource");
288    
289                    if (Validator.isNull(portletResource)) {
290                            return portletConfigPortletSetup;
291                    }
292    
293                    if (portletSetup != null) {
294                            return portletSetup;
295                    }
296    
297                    return PortletPreferencesFactoryUtil.getPortletSetup(
298                            request, portletResource);
299            }
300    
301            protected static String getTitle(
302                            Portlet portlet, RenderRequest renderRequest)
303                    throws Exception {
304    
305                    ServletContext servletContext =
306                            (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
307    
308                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
309                            WebKeys.THEME_DISPLAY);
310    
311                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
312                            renderRequest);
313    
314                    PortletPreferences portletSetup = getStrictLayoutPortletSetup(
315                            renderRequest, portlet);
316    
317                    portletSetup = getPortletSetup(
318                            request, renderRequest.getPreferences(), portletSetup);
319    
320                    String title = PortletConfigurationUtil.getPortletTitle(
321                            portletSetup, themeDisplay.getLanguageId());
322    
323                    if (Validator.isNull(title)) {
324                            title = PortalUtil.getPortletTitle(
325                                    portlet, servletContext, themeDisplay.getLocale());
326                    }
327    
328                    return title;
329            }
330    
331    }