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