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