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            @Override
049            public AssetRenderer getAssetRenderer(long classPK, int type)
050                    throws PortalException, SystemException {
051    
052                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
053    
054                    BlogsEntryAssetRenderer blogsEntryAssetRenderer =
055                            new BlogsEntryAssetRenderer(entry);
056    
057                    blogsEntryAssetRenderer.setAssetRendererType(type);
058    
059                    return blogsEntryAssetRenderer;
060            }
061    
062            @Override
063            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
064                    throws PortalException, SystemException {
065    
066                    BlogsEntry entry = BlogsEntryServiceUtil.getEntry(groupId, urlTitle);
067    
068                    return new BlogsEntryAssetRenderer(entry);
069            }
070    
071            @Override
072            public String getClassName() {
073                    return BlogsEntry.class.getName();
074            }
075    
076            @Override
077            public String getType() {
078                    return TYPE;
079            }
080    
081            @Override
082            public PortletURL getURLAdd(
083                            LiferayPortletRequest liferayPortletRequest,
084                            LiferayPortletResponse liferayPortletResponse)
085                    throws PortalException, SystemException {
086    
087                    ThemeDisplay themeDisplay =
088                            (ThemeDisplay)liferayPortletRequest.getAttribute(
089                                    WebKeys.THEME_DISPLAY);
090    
091                    if (!BlogsPermission.contains(
092                                    themeDisplay.getPermissionChecker(),
093                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ENTRY)) {
094    
095                            return null;
096                    }
097    
098                    PortletURL portletURL = PortletURLFactoryUtil.create(
099                            liferayPortletRequest, PortletKeys.BLOGS,
100                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
101    
102                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
103    
104                    return portletURL;
105            }
106    
107            @Override
108            public boolean hasPermission(
109                            PermissionChecker permissionChecker, long classPK, String actionId)
110                    throws Exception {
111    
112                    return BlogsEntryPermission.contains(
113                            permissionChecker, classPK, actionId);
114            }
115    
116            @Override
117            public boolean isLinkable() {
118                    return _LINKABLE;
119            }
120    
121            @Override
122            protected String getIconPath(ThemeDisplay themeDisplay) {
123                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
124            }
125    
126            private static final boolean _LINKABLE = true;
127    
128    }