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