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.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
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.portal.util.WebKeys;
026    import com.liferay.portlet.PortletURLFactoryUtil;
027    import com.liferay.portlet.asset.model.AssetRenderer;
028    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
029    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
030    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
031    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
032    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
033    import com.liferay.portlet.bookmarks.service.permission.BookmarksPermission;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
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 AssetRenderer getAssetRenderer(long classPK, int type)
050                    throws PortalException, SystemException {
051    
052                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
053    
054                    BookmarksEntryAssetRenderer bookmarksEntryAssetRenderer =
055                            new BookmarksEntryAssetRenderer(entry);
056    
057                    bookmarksEntryAssetRenderer.setAssetRendererType(type);
058    
059                    return bookmarksEntryAssetRenderer;
060            }
061    
062            public String getClassName() {
063                    return BookmarksEntry.class.getName();
064            }
065    
066            public String getType() {
067                    return TYPE;
068            }
069    
070            @Override
071            public PortletURL getURLAdd(
072                            LiferayPortletRequest liferayPortletRequest,
073                            LiferayPortletResponse liferayPortletResponse)
074                    throws PortalException, SystemException {
075    
076                    ThemeDisplay themeDisplay =
077                            (ThemeDisplay)liferayPortletRequest.getAttribute(
078                                    WebKeys.THEME_DISPLAY);
079    
080                    if (!BookmarksPermission.contains(
081                                    themeDisplay.getPermissionChecker(),
082                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ENTRY)) {
083    
084                            return null;
085                    }
086    
087                    PortletURL portletURL = PortletURLFactoryUtil.create(
088                            liferayPortletRequest, PortletKeys.BOOKMARKS,
089                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
090    
091                    portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
092                    portletURL.setParameter(
093                            "folderId",
094                            String.valueOf(
095                                    AssetPublisherUtil.getRecentFolderId(
096                                            liferayPortletRequest, getClassName())));
097    
098                    return portletURL;
099            }
100    
101            @Override
102            public boolean hasPermission(
103                            PermissionChecker permissionChecker, long classPK, String actionId)
104                    throws Exception {
105    
106                    return BookmarksEntryPermission.contains(
107                            permissionChecker, classPK, actionId);
108            }
109    
110            @Override
111            public boolean isLinkable() {
112                    return _LINKABLE;
113            }
114    
115            @Override
116            protected String getIconPath(ThemeDisplay themeDisplay) {
117                    return themeDisplay.getPathThemeImages() + "/ratings/star_hover.png";
118            }
119    
120            private static final boolean _LINKABLE = true;
121    
122    }