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