1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.LayoutTypePortlet;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.model.PublicRenderParameter;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.PortletPreferencesFactoryUtil;
27  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
28  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierComparator;
29  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierConfigurationComparator;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Set;
36  import java.util.TreeSet;
37  
38  import javax.portlet.PortletPreferences;
39  import javax.portlet.RenderRequest;
40  
41  /**
42   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   */
46  public class ActionUtil {
47  
48      public static void getLayoutPublicRenderParameters(
49              RenderRequest renderRequest)
50          throws Exception {
51  
52          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
53              WebKeys.THEME_DISPLAY);
54  
55          Set<String> identifiers = new HashSet<String>();
56  
57          Set<PublicRenderParameter> publicRenderParameters =
58              new TreeSet<PublicRenderParameter>(
59                  new PublicRenderParameterIdentifierComparator());
60  
61          LayoutTypePortlet layoutTypePortlet =
62              themeDisplay.getLayoutTypePortlet();
63  
64          List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
65  
66          for (Portlet portlet : portlets) {
67              for (PublicRenderParameter publicRenderParameter:
68                      portlet.getPublicRenderParameters()) {
69  
70                  if (!identifiers.contains(
71                          publicRenderParameter.getIdentifier())) {
72  
73                      identifiers.add(publicRenderParameter.getIdentifier());
74  
75                      publicRenderParameters.add(publicRenderParameter);
76                  }
77              }
78          }
79  
80          renderRequest.setAttribute(
81              WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
82      }
83  
84      public static void getPublicRenderParameterConfigurationList(
85              RenderRequest renderRequest, Portlet portlet)
86          throws Exception {
87  
88          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
89              WebKeys.THEME_DISPLAY);
90  
91          Layout layout = themeDisplay.getLayout();
92  
93          PortletPreferences preferences =
94              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
95                  layout, portlet.getPortletId());
96  
97          List<PublicRenderParameterConfiguration>
98              publicRenderParameterConfigurations =
99                  new ArrayList<PublicRenderParameterConfiguration>();
100 
101         for (PublicRenderParameter publicRenderParameter:
102                 portlet.getPublicRenderParameters()) {
103 
104             String mappingKey =
105                 PublicRenderParameterConfiguration.getMappingKey(
106                     publicRenderParameter);
107             String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
108                 publicRenderParameter);
109 
110             String mappingValue = null;
111             boolean ignoreValue = false;
112 
113             if (SessionErrors.isEmpty(renderRequest)) {
114                 mappingValue = preferences.getValue(mappingKey, null);
115                 ignoreValue = GetterUtil.getBoolean(
116                     preferences.getValue(ignoreKey, null));
117             }
118             else {
119                 mappingValue = ParamUtil.getString(renderRequest, mappingKey);
120                 ignoreValue = GetterUtil.getBoolean(
121                     ParamUtil.getString(renderRequest, ignoreKey));
122             }
123 
124             publicRenderParameterConfigurations.add(
125                 new PublicRenderParameterConfiguration(
126                     publicRenderParameter, mappingValue, ignoreValue));
127         }
128 
129         Collections.sort(
130             publicRenderParameterConfigurations,
131             new PublicRenderParameterIdentifierConfigurationComparator());
132 
133         renderRequest.setAttribute(
134             WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
135             publicRenderParameterConfigurations);
136     }
137 
138 }