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