001    /**
002     * Copyright (c) 2000-2010 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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.model.PublicRenderParameter;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
028    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierComparator;
029    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierConfigurationComparator;
030    
031    import java.util.ArrayList;
032    import java.util.Collections;
033    import java.util.HashSet;
034    import java.util.List;
035    import java.util.Set;
036    import java.util.TreeSet;
037    
038    import javax.portlet.PortletPreferences;
039    import javax.portlet.PortletRequest;
040    
041    /**
042     * @author Jorge Ferrer
043     */
044    public class ActionUtil {
045    
046            public static void getLayoutPublicRenderParameters(
047                            PortletRequest portletRequest)
048                    throws Exception {
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    Set<String> identifiers = new HashSet<String>();
054    
055                    Set<PublicRenderParameter> publicRenderParameters =
056                            new TreeSet<PublicRenderParameter>(
057                                    new PublicRenderParameterIdentifierComparator());
058    
059                    LayoutTypePortlet layoutTypePortlet =
060                            themeDisplay.getLayoutTypePortlet();
061    
062                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
063    
064                    for (Portlet portlet : portlets) {
065                            for (PublicRenderParameter publicRenderParameter:
066                                            portlet.getPublicRenderParameters()) {
067    
068                                    if (!identifiers.contains(
069                                                    publicRenderParameter.getIdentifier())) {
070    
071                                            identifiers.add(publicRenderParameter.getIdentifier());
072    
073                                            publicRenderParameters.add(publicRenderParameter);
074                                    }
075                            }
076                    }
077    
078                    portletRequest.setAttribute(
079                            WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
080            }
081    
082            public static void getPublicRenderParameterConfigurationList(
083                            PortletRequest portletRequest, Portlet portlet)
084                    throws Exception {
085    
086                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
087                            WebKeys.THEME_DISPLAY);
088    
089                    Layout layout = themeDisplay.getLayout();
090    
091                    PortletPreferences preferences =
092                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
093                                    layout, portlet.getPortletId());
094    
095                    List<PublicRenderParameterConfiguration>
096                            publicRenderParameterConfigurations =
097                                    new ArrayList<PublicRenderParameterConfiguration>();
098    
099                    for (PublicRenderParameter publicRenderParameter:
100                                    portlet.getPublicRenderParameters()) {
101    
102                            String mappingKey =
103                                    PublicRenderParameterConfiguration.getMappingKey(
104                                            publicRenderParameter);
105                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
106                                    publicRenderParameter);
107    
108                            String mappingValue = null;
109                            boolean ignoreValue = false;
110    
111                            if (SessionErrors.isEmpty(portletRequest)) {
112                                    mappingValue = preferences.getValue(mappingKey, null);
113                                    ignoreValue = GetterUtil.getBoolean(
114                                            preferences.getValue(ignoreKey, null));
115                            }
116                            else {
117                                    mappingValue = ParamUtil.getString(portletRequest, mappingKey);
118                                    ignoreValue = GetterUtil.getBoolean(
119                                            ParamUtil.getString(portletRequest, ignoreKey));
120                            }
121    
122                            publicRenderParameterConfigurations.add(
123                                    new PublicRenderParameterConfiguration(
124                                            publicRenderParameter, mappingValue, ignoreValue));
125                    }
126    
127                    Collections.sort(
128                            publicRenderParameterConfigurations,
129                            new PublicRenderParameterIdentifierConfigurationComparator());
130    
131                    portletRequest.setAttribute(
132                            WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
133                            publicRenderParameterConfigurations);
134            }
135    
136    }