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.kernel.util.Validator;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutTypePortlet;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.model.PublicRenderParameter;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
29  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierComparator;
30  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterIdentifierConfigurationComparator;
31  
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Set;
39  import java.util.TreeSet;
40  
41  import javax.portlet.PortletPreferences;
42  import javax.portlet.RenderRequest;
43  
44  /**
45   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   */
49  public class ActionUtil {
50  
51      public static void getLayoutPublicRenderParameters(
52              RenderRequest renderRequest)
53          throws Exception {
54  
55          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
56              WebKeys.THEME_DISPLAY);
57  
58          Set<String> identifiers = new HashSet<String>();
59  
60          Set<PublicRenderParameter> publicRenderParameters =
61              new TreeSet<PublicRenderParameter>(
62                  new PublicRenderParameterIdentifierComparator());
63  
64          LayoutTypePortlet layoutTypePortlet =
65              themeDisplay.getLayoutTypePortlet();
66  
67          List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
68  
69          for (Portlet portlet : portlets) {
70              for (PublicRenderParameter publicRenderParameter:
71                      portlet.getPublicRenderParameters()) {
72  
73                  if (!identifiers.contains(
74                          publicRenderParameter.getIdentifier())) {
75  
76                      identifiers.add(publicRenderParameter.getIdentifier());
77  
78                      publicRenderParameters.add(publicRenderParameter);
79                  }
80              }
81          }
82  
83          renderRequest.setAttribute(
84              WebKeys.PUBLIC_RENDER_PARAMETERS, publicRenderParameters);
85      }
86  
87      public static void getPublicRenderParameterConfigurationList(
88              RenderRequest renderRequest, Portlet portlet)
89          throws Exception {
90  
91          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
92              WebKeys.THEME_DISPLAY);
93  
94          Layout layout = themeDisplay.getLayout();
95  
96          PortletPreferences preferences =
97              PortletPreferencesFactoryUtil.getLayoutPortletSetup(
98                  layout, portlet.getPortletId());
99  
100         Map<String, String> mappings = new HashMap<String, String>();
101 
102         if (SessionErrors.isEmpty(renderRequest)) {
103             Set<PublicRenderParameter> publicRenderParameters =
104                 (Set<PublicRenderParameter>)renderRequest.getAttribute(
105                     WebKeys.PUBLIC_RENDER_PARAMETERS);
106 
107             for (PublicRenderParameter publicRenderParameter :
108                     publicRenderParameters) {
109 
110                 String mapping = preferences.getValue(
111                     _MAPPING_PREFIX + publicRenderParameter.getIdentifier(),
112                     null);
113 
114                 if (Validator.isNotNull(mapping)) {
115                     mappings.put(
116                         mapping, publicRenderParameter.getIdentifier());
117                 }
118             }
119         }
120         else {
121             for (PublicRenderParameter publicRenderParameter :
122                     portlet.getPublicRenderParameters()) {
123 
124                 String mapping = ParamUtil.getString(
125                     renderRequest,
126                     _MAPPING_PREFIX + publicRenderParameter.getIdentifier());
127 
128                 mappings.put(publicRenderParameter.getIdentifier(), mapping);
129             }
130         }
131 
132         List<PublicRenderParameterConfiguration>
133             publicRenderParameterConfigurations =
134                 new ArrayList<PublicRenderParameterConfiguration>();
135 
136         for (PublicRenderParameter publicRenderParameter:
137                 portlet.getPublicRenderParameters()) {
138 
139             boolean ignore = false;
140 
141             String ignoreKey =
142                 _IGNORE_PREFIX + publicRenderParameter.getIdentifier();
143 
144             if (SessionErrors.isEmpty(renderRequest)) {
145                 ignore = GetterUtil.getBoolean(
146                     preferences.getValue(ignoreKey, null));
147             }
148             else {
149                 ignore = GetterUtil.getBoolean(
150                     ParamUtil.getString(renderRequest, ignoreKey));
151             }
152 
153             publicRenderParameterConfigurations.add(
154                 new PublicRenderParameterConfiguration(
155                     publicRenderParameter,
156                     mappings.get(publicRenderParameter.getIdentifier()),
157                     ignore));
158         }
159 
160         Collections.sort(
161             publicRenderParameterConfigurations,
162             new PublicRenderParameterIdentifierConfigurationComparator());
163 
164         renderRequest.setAttribute(
165             WebKeys.PUBLIC_RENDER_PARAMETER_CONFIGURATIONS,
166             publicRenderParameterConfigurations);
167     }
168 
169     private static final String _IGNORE_PREFIX =
170         PublicRenderParameterConfiguration.IGNORE_PREFIX;
171 
172     private static final String _MAPPING_PREFIX =
173         PublicRenderParameterConfiguration.MAPPING_PREFIX;
174 
175 }