001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.security.permission.ActionKeys;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.bookmarks.NoSuchEntryException;
023    import com.liferay.portlet.bookmarks.NoSuchFolderException;
024    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
026    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
027    import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
028    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
029    import com.liferay.portlet.bookmarks.service.permission.BookmarksPermission;
030    
031    import javax.portlet.PortletRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ActionUtil {
039    
040            public static void getEntry(HttpServletRequest request) throws Exception {
041                    long entryId = ParamUtil.getLong(request, "entryId");
042    
043                    BookmarksEntry entry = null;
044    
045                    if (entryId > 0) {
046                            entry = BookmarksEntryServiceUtil.getEntry(entryId);
047    
048                            if (entry.isInTrash()) {
049                                    throw new NoSuchEntryException("{entryId=" + entryId + "}");
050                            }
051                    }
052    
053                    request.setAttribute(WebKeys.BOOKMARKS_ENTRY, entry);
054            }
055    
056            public static void getEntry(PortletRequest portletRequest)
057                    throws Exception {
058    
059                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
060                            portletRequest);
061    
062                    getEntry(request);
063            }
064    
065            public static void getFolder(HttpServletRequest request) throws Exception {
066                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
067                            WebKeys.THEME_DISPLAY);
068    
069                    long folderId = ParamUtil.getLong(request, "folderId");
070    
071                    BookmarksFolder folder = null;
072    
073                    if ((folderId > 0) &&
074                            (folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
075    
076                            folder = BookmarksFolderServiceUtil.getFolder(folderId);
077    
078                            if (folder.isInTrash()) {
079                                    throw new NoSuchFolderException("{folderId=" + folderId + "}");
080                            }
081                    }
082                    else {
083                            BookmarksPermission.check(
084                                    themeDisplay.getPermissionChecker(),
085                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
086                    }
087    
088                    request.setAttribute(WebKeys.BOOKMARKS_FOLDER, folder);
089            }
090    
091            public static void getFolder(PortletRequest portletRequest)
092                    throws Exception {
093    
094                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
095                            portletRequest);
096    
097                    getFolder(request);
098            }
099    
100    }