001    /**
002     * Copyright (c) 2000-present 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.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
019    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
020    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.asset.model.AssetRenderer;
026    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
027    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
028    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
029    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
030    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
031    import com.liferay.portlet.bookmarks.service.permission.BookmarksPermission;
032    
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletURL;
035    import javax.portlet.WindowState;
036    import javax.portlet.WindowStateException;
037    
038    /**
039     * @author Julio Camarero
040     * @author Juan Fern??ndez
041     * @author Raymond Aug??
042     * @author Sergio Gonz??lez
043     */
044    public class BookmarksEntryAssetRendererFactory
045            extends BaseAssetRendererFactory {
046    
047            public static final String TYPE = "bookmark";
048    
049            public BookmarksEntryAssetRendererFactory() {
050                    setLinkable(true);
051            }
052    
053            @Override
054            public AssetRenderer getAssetRenderer(long classPK, int type)
055                    throws PortalException {
056    
057                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
058    
059                    BookmarksEntryAssetRenderer bookmarksEntryAssetRenderer =
060                            new BookmarksEntryAssetRenderer(entry);
061    
062                    bookmarksEntryAssetRenderer.setAssetRendererType(type);
063    
064                    return bookmarksEntryAssetRenderer;
065            }
066    
067            @Override
068            public String getClassName() {
069                    return BookmarksEntry.class.getName();
070            }
071    
072            @Override
073            public String getIconCssClass() {
074                    return "icon-bookmark-empty";
075            }
076    
077            @Override
078            public String getType() {
079                    return TYPE;
080            }
081    
082            @Override
083            public PortletURL getURLAdd(
084                    LiferayPortletRequest liferayPortletRequest,
085                    LiferayPortletResponse liferayPortletResponse) {
086    
087                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
088                            PortletKeys.BOOKMARKS);
089    
090                    portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
091                    portletURL.setParameter(
092                            "folderId",
093                            String.valueOf(
094                                    AssetPublisherUtil.getRecentFolderId(
095                                            liferayPortletRequest, getClassName())));
096    
097                    return portletURL;
098            }
099    
100            @Override
101            public PortletURL getURLView(
102                    LiferayPortletResponse liferayPortletResponse,
103                    WindowState windowState) {
104    
105                    LiferayPortletURL liferayPortletURL =
106                            liferayPortletResponse.createLiferayPortletURL(
107                                    PortletKeys.BOOKMARKS, PortletRequest.RENDER_PHASE);
108    
109                    try {
110                            liferayPortletURL.setWindowState(windowState);
111                    }
112                    catch (WindowStateException wse) {
113                    }
114    
115                    return liferayPortletURL;
116            }
117    
118            @Override
119            public boolean hasAddPermission(
120                            PermissionChecker permissionChecker, long groupId, long classTypeId)
121                    throws Exception {
122    
123                    return BookmarksPermission.contains(
124                            permissionChecker, groupId, ActionKeys.ADD_ENTRY);
125            }
126    
127            @Override
128            public boolean hasPermission(
129                            PermissionChecker permissionChecker, long classPK, String actionId)
130                    throws Exception {
131    
132                    return BookmarksEntryPermission.contains(
133                            permissionChecker, classPK, actionId);
134            }
135    
136            @Override
137            protected String getIconPath(ThemeDisplay themeDisplay) {
138                    return themeDisplay.getPathThemeImages() + "/ratings/star_hover.png";
139            }
140    
141    }