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