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    import javax.servlet.http.HttpServletRequest;
039    
040    /**
041     * @author Julio Camarero
042     * @author Juan Fern??ndez
043     * @author Raymond Aug??
044     * @author Sergio Gonz??lez
045     */
046    public class BookmarksEntryAssetRendererFactory
047            extends BaseAssetRendererFactory {
048    
049            public static final String CLASS_NAME =BookmarksEntry.class.getName();
050    
051            public static final String TYPE = "bookmark";
052    
053            @Override
054            public AssetRenderer getAssetRenderer(long classPK, int type)
055                    throws PortalException, SystemException {
056    
057                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
058    
059                    return new BookmarksEntryAssetRenderer(entry);
060            }
061    
062            @Override
063            public String getClassName() {
064                    return CLASS_NAME;
065            }
066    
067            @Override
068            public String getType() {
069                    return TYPE;
070            }
071    
072            @Override
073            public PortletURL getURLAdd(
074                            LiferayPortletRequest liferayPortletRequest,
075                            LiferayPortletResponse liferayPortletResponse)
076                    throws PortalException, SystemException {
077    
078                    HttpServletRequest request =
079                            liferayPortletRequest.getHttpServletRequest();
080    
081                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
082                            WebKeys.THEME_DISPLAY);
083    
084                    if (!BookmarksPermission.contains(
085                                    themeDisplay.getPermissionChecker(),
086                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ENTRY)) {
087    
088                            return null;
089                    }
090    
091                    PortletURL portletURL = PortletURLFactoryUtil.create(
092                            request, PortletKeys.BOOKMARKS, getControlPanelPlid(themeDisplay),
093                            PortletRequest.RENDER_PHASE);
094    
095                    portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
096                    portletURL.setParameter(
097                            "folderId",
098                            String.valueOf(
099                                    AssetPublisherUtil.getRecentFolderId(
100                                            liferayPortletRequest, CLASS_NAME)));
101    
102                    return portletURL;
103            }
104    
105            @Override
106            public boolean hasPermission(
107                            PermissionChecker permissionChecker, long classPK, String actionId)
108                    throws Exception {
109    
110                    return BookmarksEntryPermission.contains(
111                            permissionChecker, classPK, actionId);
112            }
113    
114            @Override
115            public boolean isLinkable() {
116                    return _LINKABLE;
117            }
118    
119            @Override
120            protected String getIconPath(ThemeDisplay themeDisplay) {
121                    return themeDisplay.getPathThemeImages() + "/ratings/star_hover.png";
122            }
123    
124            private static final boolean _LINKABLE = true;
125    
126    }