1
19
20 package com.liferay.portlet;
21
22 import com.liferay.portal.PortalException;
23 import com.liferay.portal.SystemException;
24 import com.liferay.portal.kernel.portlet.LiferayPortletMode;
25 import com.liferay.portal.kernel.portlet.PortletBag;
26 import com.liferay.portal.kernel.portlet.PortletBagPool;
27 import com.liferay.portal.kernel.util.InstancePool;
28 import com.liferay.portal.kernel.util.JavaConstants;
29 import com.liferay.portal.kernel.util.ParamUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.Layout;
32 import com.liferay.portal.model.LayoutConstants;
33 import com.liferay.portal.model.LayoutTypePortlet;
34 import com.liferay.portal.model.Portlet;
35 import com.liferay.portal.model.PortletApp;
36 import com.liferay.portal.model.PortletConstants;
37 import com.liferay.portal.model.PortletPreferencesIds;
38 import com.liferay.portal.security.auth.PrincipalException;
39 import com.liferay.portal.security.permission.ActionKeys;
40 import com.liferay.portal.security.permission.PermissionChecker;
41 import com.liferay.portal.security.permission.PermissionThreadLocal;
42 import com.liferay.portal.service.PortletLocalServiceUtil;
43 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
44 import com.liferay.portal.service.UserLocalServiceUtil;
45 import com.liferay.portal.service.permission.LayoutPermissionUtil;
46 import com.liferay.portal.theme.ThemeDisplay;
47 import com.liferay.portal.util.PortalUtil;
48 import com.liferay.portal.util.PortletKeys;
49 import com.liferay.portal.util.WebKeys;
50
51 import javax.portlet.PortletPreferences;
52 import javax.portlet.PortletRequest;
53 import javax.portlet.PreferencesValidator;
54
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpSession;
57
58
65 public class PortletPreferencesFactoryImpl
66 implements PortletPreferencesFactory {
67
68 public PortletPreferences getLayoutPortletSetup(
69 Layout layout, String portletId)
70 throws SystemException {
71
72 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
73 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
74
75 return PortletPreferencesLocalServiceUtil.getPreferences(
76 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
77 portletId);
78 }
79
80 public PortalPreferences getPortalPreferences(HttpServletRequest request)
81 throws SystemException {
82
83 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84 WebKeys.THEME_DISPLAY);
85
86 long ownerId = themeDisplay.getUserId();
87 int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
88 long plid = PortletKeys.PREFS_PLID_SHARED;
89 String portletId = PortletKeys.LIFERAY_PORTAL;
90
91 PortalPreferences portalPrefs = null;
92
93 if (themeDisplay.isSignedIn()) {
94 PortletPreferencesImpl preferencesImpl = (PortletPreferencesImpl)
95 PortletPreferencesLocalServiceUtil.getPreferences(
96 themeDisplay.getCompanyId(), ownerId, ownerType, plid,
97 portletId);
98
99 portalPrefs = new PortalPreferencesImpl(
100 preferencesImpl, themeDisplay.isSignedIn());
101 }
102 else {
103 HttpSession session = request.getSession();
104
105 portalPrefs = (PortalPreferences)session.getAttribute(
106 WebKeys.PORTAL_PREFERENCES);
107
108 if (portalPrefs == null) {
109 PortletPreferencesImpl preferencesImpl =
110 (PortletPreferencesImpl)
111 PortletPreferencesLocalServiceUtil.getPreferences(
112 themeDisplay.getCompanyId(), ownerId, ownerType,
113 plid, portletId);
114
115 preferencesImpl =
116 (PortletPreferencesImpl)preferencesImpl.clone();
117
118 portalPrefs = new PortalPreferencesImpl(
119 preferencesImpl, themeDisplay.isSignedIn());
120
121 session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPrefs);
122 }
123 }
124
125 return portalPrefs;
126 }
127
128 public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
129 throws SystemException {
130
131 HttpServletRequest request = PortalUtil.getHttpServletRequest(
132 portletRequest);
133
134 return getPortalPreferences(request);
135 }
136
137 public PortletPreferences getPortletPreferences(
138 HttpServletRequest request, String portletId)
139 throws PortalException, SystemException {
140
141 PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
142 request, portletId);
143
144 return PortletPreferencesLocalServiceUtil.getPreferences(
145 portletPreferencesIds);
146 }
147
148 public PortletPreferencesIds getPortletPreferencesIds(
149 HttpServletRequest request, String portletId)
150 throws PortalException, SystemException {
151
152 Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
153
154 return getPortletPreferencesIds(request, layout, portletId);
155 }
156
157 public PortletPreferencesIds getPortletPreferencesIds(
158 HttpServletRequest request, Layout selLayout, String portletId)
159 throws PortalException, SystemException {
160
161
164
167
170
173
176
179
182 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
183 WebKeys.THEME_DISPLAY);
184
185 Layout layout = themeDisplay.getLayout();
186 LayoutTypePortlet layoutTypePortlet =
187 themeDisplay.getLayoutTypePortlet();
188 PermissionChecker permissionChecker =
189 PermissionThreadLocal.getPermissionChecker();
190
191 Portlet portlet = PortletLocalServiceUtil.getPortletById(
192 themeDisplay.getCompanyId(), portletId);
193
194 long ownerId = 0;
195 int ownerType = 0;
196 long plid = 0;
197
198 boolean modeEditGuest = false;
199
200 String portletMode = ParamUtil.getString(request, "p_p_mode");
201
202 if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
203 ((layoutTypePortlet != null) &&
204 (layoutTypePortlet.hasModeEditGuestPortletId(portletId)))) {
205
206 modeEditGuest = true;
207 }
208
209 if (modeEditGuest) {
210 boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
211 permissionChecker, layout, ActionKeys.UPDATE);
212
213 if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
214 }
215 else {
216
217
220 throw new PrincipalException();
221 }
222 }
223
224 if (portlet.isPreferencesCompanyWide()) {
225 ownerId = themeDisplay.getCompanyId();
226 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
227 plid = PortletKeys.PREFS_PLID_SHARED;
228 portletId = PortletConstants.getRootPortletId(portletId);
229 }
230 else {
231 if (portlet.isPreferencesUniquePerLayout()) {
232 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
233 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
234 plid = selLayout.getPlid();
235
236 if (portlet.isPreferencesOwnedByGroup()) {
237 }
238 else {
239 long userId = PortalUtil.getUserId(request);
240
241 if ((userId <= 0) || modeEditGuest) {
242 userId = UserLocalServiceUtil.getDefaultUserId(
243 themeDisplay.getCompanyId());
244 }
245
246 ownerId = userId;
247 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
248 }
249 }
250 else {
251 plid = PortletKeys.PREFS_PLID_SHARED;
252
253 if (portlet.isPreferencesOwnedByGroup()) {
254 ownerId = selLayout.getGroupId();
255 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
256 portletId = PortletConstants.getRootPortletId(portletId);
257 }
258 else {
259 long userId = PortalUtil.getUserId(request);
260
261 if ((userId <= 0) || modeEditGuest) {
262 userId = UserLocalServiceUtil.getDefaultUserId(
263 themeDisplay.getCompanyId());
264 }
265
266 ownerId = userId;
267 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
268 }
269 }
270 }
271
272 return new PortletPreferencesIds(
273 themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
274 }
275
276 public PortletPreferences getPortletSetup(
277 Layout layout, String portletId, String defaultPreferences)
278 throws SystemException {
279
280 return getPortletSetup(
281 LayoutConstants.DEFAULT_PLID, layout, portletId,
282 defaultPreferences);
283 }
284
285 public PortletPreferences getPortletSetup(
286 HttpServletRequest request, String portletId)
287 throws SystemException {
288
289 return getPortletSetup(request, portletId, null);
290 }
291
292 public PortletPreferences getPortletSetup(
293 HttpServletRequest request, String portletId,
294 String defaultPreferences)
295 throws SystemException {
296
297 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
298 WebKeys.THEME_DISPLAY);
299
300 return getPortletSetup(
301 themeDisplay.getScopeGroupId(), themeDisplay.getLayout(), portletId,
302 defaultPreferences);
303 }
304
305 public PortletPreferences getPortletSetup(PortletRequest portletRequest)
306 throws SystemException {
307
308 HttpServletRequest request = PortalUtil.getHttpServletRequest(
309 portletRequest);
310 String portletId = PortalUtil.getPortletId(portletRequest);
311
312 return getPortletSetup(request, portletId);
313 }
314
315 public PortletPreferences getPortletSetup(
316 PortletRequest portletRequest, String portletId)
317 throws SystemException {
318
319 HttpServletRequest request = PortalUtil.getHttpServletRequest(
320 portletRequest);
321
322 return getPortletSetup(request, portletId);
323 }
324
325 public PortletPreferences getPreferences(HttpServletRequest request) {
326 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
327 JavaConstants.JAVAX_PORTLET_REQUEST);
328
329 PortletPreferences preferences = null;
330
331 if (portletRequest != null) {
332 PortletPreferencesWrapper preferencesWrapper =
333 (PortletPreferencesWrapper)portletRequest.getPreferences();
334
335 preferences = preferencesWrapper.getPreferencesImpl();
336 }
337
338 return preferences;
339 }
340
341 public PreferencesValidator getPreferencesValidator(Portlet portlet) {
342 PortletApp portletApp = portlet.getPortletApp();
343
344 if (portletApp.isWARFile()) {
345 PortletBag portletBag = PortletBagPool.get(
346 portlet.getRootPortletId());
347
348 return portletBag.getPreferencesValidatorInstance();
349 }
350 else {
351 PreferencesValidator preferencesValidator = null;
352
353 if (Validator.isNotNull(portlet.getPreferencesValidator())) {
354 preferencesValidator =
355 (PreferencesValidator)InstancePool.get(
356 portlet.getPreferencesValidator());
357 }
358
359 return preferencesValidator;
360 }
361 }
362
363 protected PortletPreferences getPortletSetup(
364 long scopeGroupId, Layout layout, String portletId,
365 String defaultPreferences)
366 throws SystemException {
367
368 Portlet portlet = PortletLocalServiceUtil.getPortletById(
369 layout.getCompanyId(), portletId);
370
371 boolean uniquePerLayout = false;
372 boolean uniquePerGroup = false;
373
374 if (portlet.isPreferencesCompanyWide()) {
375 portletId = PortletConstants.getRootPortletId(portletId);
376 }
377 else {
378 if (portlet.isPreferencesUniquePerLayout()) {
379 uniquePerLayout = true;
380
381 if (portlet.isPreferencesOwnedByGroup()) {
382 uniquePerGroup = true;
383 }
384 }
385 else {
386 if (portlet.isPreferencesOwnedByGroup()) {
387 uniquePerGroup = true;
388 portletId = PortletConstants.getRootPortletId(portletId);
389 }
390 }
391 }
392
393 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
394 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
395 long plid = layout.getPlid();
396
397 if (!uniquePerLayout) {
398 plid = PortletKeys.PREFS_PLID_SHARED;
399
400 if (uniquePerGroup) {
401 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
402 ownerId = scopeGroupId;
403 }
404 else {
405 ownerId = layout.getGroupId();
406 }
407
408 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
409 }
410 else {
411 ownerId = layout.getCompanyId();
412 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
413 }
414 }
415
416 return PortletPreferencesLocalServiceUtil.getPreferences(
417 layout.getCompanyId(), ownerId, ownerType, plid, portletId,
418 defaultPreferences);
419 }
420
421 }