001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.portlet.LiferayPortletMode;
026: import com.liferay.portal.kernel.security.permission.ActionKeys;
027: import com.liferay.portal.kernel.security.permission.PermissionChecker;
028: import com.liferay.portal.kernel.util.InstancePool;
029: import com.liferay.portal.kernel.util.JavaConstants;
030: import com.liferay.portal.kernel.util.ParamUtil;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.Layout;
033: import com.liferay.portal.model.LayoutTypePortlet;
034: import com.liferay.portal.model.Portlet;
035: import com.liferay.portal.model.PortletPreferencesIds;
036: import com.liferay.portal.security.auth.PrincipalException;
037: import com.liferay.portal.security.permission.PermissionThreadLocal;
038: import com.liferay.portal.service.PortletLocalServiceUtil;
039: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
040: import com.liferay.portal.service.UserLocalServiceUtil;
041: import com.liferay.portal.service.permission.LayoutPermissionUtil;
042: import com.liferay.portal.servlet.PortletContextPool;
043: import com.liferay.portal.servlet.PortletContextWrapper;
044: import com.liferay.portal.theme.ThemeDisplay;
045: import com.liferay.portal.util.PortalUtil;
046: import com.liferay.portal.util.PortletKeys;
047: import com.liferay.portal.util.WebKeys;
048:
049: import javax.portlet.ActionRequest;
050: import javax.portlet.PortletPreferences;
051: import javax.portlet.PreferencesValidator;
052: import javax.portlet.RenderRequest;
053:
054: import javax.servlet.http.HttpServletRequest;
055: import javax.servlet.http.HttpSession;
056:
057: /**
058: * <a href="PortletPreferencesFactoryImpl.java.html"><b><i>View Source</i></b>
059: * </a>
060: *
061: * @author Brian Wing Shun Chan
062: *
063: */
064: public class PortletPreferencesFactoryImpl implements
065: PortletPreferencesFactory {
066:
067: public PortalPreferences getPortalPreferences(HttpServletRequest req)
068: throws PortalException, SystemException {
069:
070: ThemeDisplay themeDisplay = (ThemeDisplay) req
071: .getAttribute(WebKeys.THEME_DISPLAY);
072:
073: long ownerId = themeDisplay.getUserId();
074: int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
075: long plid = PortletKeys.PREFS_PLID_SHARED;
076: String portletId = PortletKeys.LIFERAY_PORTAL;
077:
078: PortalPreferences portalPrefs = null;
079:
080: if (themeDisplay.isSignedIn()) {
081: PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl) PortletPreferencesLocalServiceUtil
082: .getPreferences(themeDisplay.getCompanyId(),
083: ownerId, ownerType, plid, portletId);
084:
085: portalPrefs = new PortalPreferencesImpl(prefsImpl,
086: themeDisplay.isSignedIn());
087: } else {
088: HttpSession ses = req.getSession();
089:
090: portalPrefs = (PortalPreferences) ses
091: .getAttribute(WebKeys.PORTAL_PREFERENCES);
092:
093: if (portalPrefs == null) {
094: PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl) PortletPreferencesLocalServiceUtil
095: .getPreferences(themeDisplay.getCompanyId(),
096: ownerId, ownerType, plid, portletId);
097:
098: prefsImpl = (PortletPreferencesImpl) prefsImpl.clone();
099:
100: portalPrefs = new PortalPreferencesImpl(prefsImpl,
101: themeDisplay.isSignedIn());
102:
103: ses.setAttribute(WebKeys.PORTAL_PREFERENCES,
104: portalPrefs);
105: }
106: }
107:
108: return portalPrefs;
109: }
110:
111: public PortalPreferences getPortalPreferences(ActionRequest req)
112: throws PortalException, SystemException {
113:
114: HttpServletRequest httpReq = PortalUtil
115: .getHttpServletRequest(req);
116:
117: return getPortalPreferences(httpReq);
118: }
119:
120: public PortalPreferences getPortalPreferences(RenderRequest req)
121: throws PortalException, SystemException {
122:
123: HttpServletRequest httpReq = PortalUtil
124: .getHttpServletRequest(req);
125:
126: return getPortalPreferences(httpReq);
127: }
128:
129: public PortletPreferences getPortletPreferences(
130: HttpServletRequest req, String portletId)
131: throws PortalException, SystemException {
132:
133: PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
134: req, portletId);
135:
136: return PortletPreferencesLocalServiceUtil
137: .getPreferences(portletPreferencesIds);
138: }
139:
140: public PortletPreferencesIds getPortletPreferencesIds(
141: HttpServletRequest req, String portletId)
142: throws PortalException, SystemException {
143:
144: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
145:
146: return getPortletPreferencesIds(req, layout, portletId);
147: }
148:
149: public PortletPreferencesIds getPortletPreferencesIds(
150: HttpServletRequest req, Layout selLayout, String portletId)
151: throws PortalException, SystemException {
152:
153: // Below is a list of the possible combinations, where we specify the
154: // the owner id, the layout id, portlet id, and the function.
155:
156: // liferay.com.1, SHARED, PORTAL, preference is scoped per user across
157: // the entire portal
158:
159: // COMPANY.liferay.com, SHARED, 56_INSTANCE_abcd, preference is scoped
160: // per portlet and company and is shared across all layouts
161:
162: // GROUP.10, SHARED, 56_INSTANCE_abcd, preference is scoped per portlet
163: // and group and is shared across all layouts
164:
165: // USER.liferay.com.1, SHARED, 56_INSTANCE_abcd, preference is scoped
166: // per portlet and user and is shared across all layouts
167:
168: // PUB.10, 3, 56_INSTANCE_abcd, preference is scoped per portlet, group,
169: // and layout
170:
171: // PUB.10.USER.liferay.com.1, 3, 56_INSTANCE_abcd, preference is scoped
172: // per portlet, user, and layout
173:
174: ThemeDisplay themeDisplay = (ThemeDisplay) req
175: .getAttribute(WebKeys.THEME_DISPLAY);
176:
177: Layout layout = themeDisplay.getLayout();
178: LayoutTypePortlet layoutTypePortlet = themeDisplay
179: .getLayoutTypePortlet();
180: PermissionChecker permissionChecker = PermissionThreadLocal
181: .getPermissionChecker();
182:
183: Portlet portlet = PortletLocalServiceUtil.getPortletById(
184: themeDisplay.getCompanyId(), portletId);
185:
186: long ownerId = 0;
187: int ownerType = 0;
188: long plid = 0;
189:
190: boolean modeEditGuest = false;
191:
192: String portletMode = ParamUtil.getString(req, "p_p_mode");
193:
194: if (portletMode
195: .equals(LiferayPortletMode.EDIT_GUEST.toString())
196: || layoutTypePortlet
197: .hasModeEditGuestPortletId(portletId)) {
198:
199: modeEditGuest = true;
200: }
201:
202: if (modeEditGuest) {
203: boolean hasUpdateLayoutPermission = LayoutPermissionUtil
204: .contains(permissionChecker, layout,
205: ActionKeys.UPDATE);
206:
207: if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
208: } else {
209:
210: // Only users with the correct permissions can update guest
211: // preferences
212:
213: throw new PrincipalException();
214: }
215: }
216:
217: if (portlet.isPreferencesCompanyWide()) {
218: ownerId = themeDisplay.getCompanyId();
219: ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
220: plid = PortletKeys.PREFS_PLID_SHARED;
221: } else {
222: if (portlet.isPreferencesUniquePerLayout()) {
223: ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
224: ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
225: plid = selLayout.getPlid();
226:
227: if (portlet.isPreferencesOwnedByGroup()) {
228: } else {
229: long userId = PortalUtil.getUserId(req);
230:
231: if ((userId <= 0) || modeEditGuest) {
232: userId = UserLocalServiceUtil
233: .getDefaultUserId(themeDisplay
234: .getCompanyId());
235: }
236:
237: ownerId = userId;
238: ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
239: }
240: } else {
241: plid = PortletKeys.PREFS_PLID_SHARED;
242:
243: if (portlet.isPreferencesOwnedByGroup()) {
244: ownerId = selLayout.getGroupId();
245: ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
246: } else {
247: long userId = PortalUtil.getUserId(req);
248:
249: if ((userId <= 0) || modeEditGuest) {
250: userId = UserLocalServiceUtil
251: .getDefaultUserId(themeDisplay
252: .getCompanyId());
253: }
254:
255: ownerId = userId;
256: ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
257: }
258: }
259: }
260:
261: return new PortletPreferencesIds(themeDisplay.getCompanyId(),
262: ownerId, ownerType, plid, portletId);
263: }
264:
265: public PortletPreferences getPortletSetup(Layout layout,
266: String portletId) throws PortalException, SystemException {
267:
268: long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
269: int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
270:
271: return PortletPreferencesLocalServiceUtil.getPreferences(layout
272: .getCompanyId(), ownerId, ownerType, layout.getPlid(),
273: portletId);
274: }
275:
276: public PortletPreferences getPortletSetup(HttpServletRequest req,
277: String portletId, boolean uniquePerLayout,
278: boolean uniquePerGroup) throws PortalException,
279: SystemException {
280:
281: return getPortletSetup(req, portletId, uniquePerLayout,
282: uniquePerGroup, null);
283: }
284:
285: public PortletPreferences getPortletSetup(HttpServletRequest req,
286: String portletId, boolean uniquePerLayout,
287: boolean uniquePerGroup, String defaultPreferences)
288: throws PortalException, SystemException {
289:
290: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
291:
292: return getPortletSetup(layout, portletId, uniquePerLayout,
293: uniquePerGroup, defaultPreferences);
294: }
295:
296: public PortletPreferences getPortletSetup(Layout layout,
297: String portletId, boolean uniquePerLayout,
298: boolean uniquePerGroup, String defaultPreferences)
299: throws PortalException, SystemException {
300:
301: long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
302: int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
303: long plid = layout.getPlid();
304:
305: if (!uniquePerLayout) {
306: plid = PortletKeys.PREFS_PLID_SHARED;
307:
308: if (uniquePerGroup) {
309: ownerId = layout.getGroupId();
310: ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
311: } else {
312: ownerId = layout.getCompanyId();
313: ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
314: }
315: }
316:
317: return PortletPreferencesLocalServiceUtil.getPreferences(layout
318: .getCompanyId(), ownerId, ownerType, plid, portletId,
319: defaultPreferences);
320: }
321:
322: public PortletPreferences getPortletSetup(ActionRequest req,
323: String portletId, boolean uniquePerLayout,
324: boolean uniquePerGroup) throws PortalException,
325: SystemException {
326:
327: HttpServletRequest httpReq = PortalUtil
328: .getHttpServletRequest(req);
329:
330: return getPortletSetup(httpReq, portletId, uniquePerLayout,
331: uniquePerGroup);
332: }
333:
334: public PortletPreferences getPortletSetup(RenderRequest req,
335: String portletId, boolean uniquePerLayout,
336: boolean uniquePerGroup) throws PortalException,
337: SystemException {
338:
339: HttpServletRequest httpReq = PortalUtil
340: .getHttpServletRequest(req);
341:
342: return getPortletSetup(httpReq, portletId, uniquePerLayout,
343: uniquePerGroup);
344: }
345:
346: public PortletPreferences getPreferences(HttpServletRequest req) {
347: RenderRequest renderRequest = (RenderRequest) req
348: .getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
349:
350: PortletPreferences prefs = null;
351:
352: if (renderRequest != null) {
353: PortletPreferencesWrapper prefsWrapper = (PortletPreferencesWrapper) renderRequest
354: .getPreferences();
355:
356: prefs = prefsWrapper.getPreferencesImpl();
357: }
358:
359: return prefs;
360: }
361:
362: public PreferencesValidator getPreferencesValidator(Portlet portlet) {
363: if (portlet.isWARFile()) {
364: PortletContextWrapper pcw = PortletContextPool.get(portlet
365: .getRootPortletId());
366:
367: return pcw.getPreferencesValidatorInstance();
368: } else {
369: PreferencesValidator prefsValidator = null;
370:
371: if (Validator.isNotNull(portlet.getPreferencesValidator())) {
372: prefsValidator = (PreferencesValidator) InstancePool
373: .get(portlet.getPreferencesValidator());
374: }
375:
376: return prefsValidator;
377: }
378: }
379:
380: }
|