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