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