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