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.kernel.portlet.LiferayPortletURL;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.WebKeys;
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    import javax.portlet.WindowState;
038    import javax.portlet.WindowStateException;
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 TYPE = "bookmark";
050    
051            @Override
052            public AssetRenderer getAssetRenderer(long classPK, int type)
053                    throws PortalException, SystemException {
054    
055                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
056    
057                    BookmarksEntryAssetRenderer bookmarksEntryAssetRenderer =
058                            new BookmarksEntryAssetRenderer(entry);
059    
060                    bookmarksEntryAssetRenderer.setAssetRendererType(type);
061    
062                    return bookmarksEntryAssetRenderer;
063            }
064    
065            @Override
066            public String getClassName() {
067                    return BookmarksEntry.class.getName();
068            }
069    
070            @Override
071            public String getType() {
072                    return TYPE;
073            }
074    
075            @Override
076            public PortletURL getURLAdd(
077                    LiferayPortletRequest liferayPortletRequest,
078                    LiferayPortletResponse liferayPortletResponse) {
079    
080                    ThemeDisplay themeDisplay =
081                            (ThemeDisplay)liferayPortletRequest.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 = liferayPortletResponse.createRenderURL(
092                            PortletKeys.BOOKMARKS);
093    
094                    portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
095                    portletURL.setParameter(
096                            "folderId",
097                            String.valueOf(
098                                    AssetPublisherUtil.getRecentFolderId(
099                                            liferayPortletRequest, getClassName())));
100    
101                    return portletURL;
102            }
103    
104            @Override
105            public PortletURL getURLView(
106                    LiferayPortletResponse liferayPortletResponse,
107                    WindowState windowState) {
108    
109                    LiferayPortletURL liferayPortletURL =
110                            liferayPortletResponse.createLiferayPortletURL(
111                                    PortletKeys.BOOKMARKS, PortletRequest.RENDER_PHASE);
112    
113                    try {
114                            liferayPortletURL.setWindowState(windowState);
115                    }
116                    catch (WindowStateException wse) {
117                    }
118    
119                    return liferayPortletURL;
120            }
121    
122            @Override
123            public boolean hasPermission(
124                            PermissionChecker permissionChecker, long classPK, String actionId)
125                    throws Exception {
126    
127                    return BookmarksEntryPermission.contains(
128                            permissionChecker, classPK, actionId);
129            }
130    
131            @Override
132            public boolean isLinkable() {
133                    return _LINKABLE;
134            }
135    
136            @Override
137            protected String getIconPath(ThemeDisplay themeDisplay) {
138                    return themeDisplay.getPathThemeImages() + "/ratings/star_hover.png";
139            }
140    
141            private static final boolean _LINKABLE = true;
142    
143    }