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.blogs.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.blogs.model.BlogsEntry;
030    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
031    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
032    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
033    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Juan Fernández
041     * @author Raymond Augé
042     * @author Sergio González
043     */
044    public class BlogsEntryAssetRendererFactory extends BaseAssetRendererFactory {
045    
046            public static final String TYPE = "blog";
047    
048            public AssetRenderer getAssetRenderer(long classPK, int type)
049                    throws PortalException, SystemException {
050    
051                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
052    
053                    BlogsEntryAssetRenderer blogsEntryAssetRenderer =
054                            new BlogsEntryAssetRenderer(entry);
055    
056                    blogsEntryAssetRenderer.setAssetRendererType(type);
057    
058                    return blogsEntryAssetRenderer;
059            }
060    
061            @Override
062            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
063                    throws PortalException, SystemException {
064    
065                    BlogsEntry entry = BlogsEntryServiceUtil.getEntry(groupId, urlTitle);
066    
067                    return new BlogsEntryAssetRenderer(entry);
068            }
069    
070            public String getClassName() {
071                    return BlogsEntry.class.getName();
072            }
073    
074            public String getType() {
075                    return TYPE;
076            }
077    
078            @Override
079            public PortletURL getURLAdd(
080                            LiferayPortletRequest liferayPortletRequest,
081                            LiferayPortletResponse liferayPortletResponse)
082                    throws PortalException, SystemException {
083    
084                    ThemeDisplay themeDisplay =
085                            (ThemeDisplay)liferayPortletRequest.getAttribute(
086                                    WebKeys.THEME_DISPLAY);
087    
088                    if (!BlogsPermission.contains(
089                                    themeDisplay.getPermissionChecker(),
090                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ENTRY)) {
091    
092                            return null;
093                    }
094    
095                    PortletURL portletURL = PortletURLFactoryUtil.create(
096                            liferayPortletRequest, PortletKeys.BLOGS,
097                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
098    
099                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
100    
101                    return portletURL;
102            }
103    
104            @Override
105            public boolean hasPermission(
106                            PermissionChecker permissionChecker, long classPK, String actionId)
107                    throws Exception {
108    
109                    return BlogsEntryPermission.contains(
110                            permissionChecker, classPK, actionId);
111            }
112    
113            @Override
114            public boolean isLinkable() {
115                    return _LINKABLE;
116            }
117    
118            @Override
119            protected String getIconPath(ThemeDisplay themeDisplay) {
120                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
121            }
122    
123            private static final boolean _LINKABLE = true;
124    
125    }