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.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.kernel.trash.TrashRenderer;
020    import com.liferay.portal.kernel.util.HtmlUtil;
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.PropsValues;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.asset.model.AssetRendererFactory;
028    import com.liferay.portlet.asset.model.BaseAssetRenderer;
029    import com.liferay.portlet.blogs.model.BlogsEntry;
030    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
031    
032    import java.util.Locale;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    import javax.portlet.WindowState;
039    
040    /**
041     * @author Jorge Ferrer
042     * @author Juan Fernández
043     * @author Sergio González
044     * @author Zsolt Berentey
045     */
046    public class BlogsEntryAssetRenderer
047            extends BaseAssetRenderer implements TrashRenderer {
048    
049            public BlogsEntryAssetRenderer(BlogsEntry entry) {
050                    _entry = entry;
051            }
052    
053            public String getAssetRendererFactoryClassName() {
054                    return BlogsEntryAssetRendererFactory.CLASS_NAME;
055            }
056    
057            public String getClassName() {
058                    return BlogsEntry.class.getName();
059            }
060    
061            public long getClassPK() {
062                    return _entry.getEntryId();
063            }
064    
065            @Override
066            public String getDiscussionPath() {
067                    if (PropsValues.BLOGS_ENTRY_COMMENTS_ENABLED) {
068                            return "edit_entry_discussion";
069                    }
070                    else {
071                            return null;
072                    }
073            }
074    
075            public long getGroupId() {
076                    return _entry.getGroupId();
077            }
078    
079            @Override
080            public String getIconPath(ThemeDisplay themeDisplay) {
081                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
082            }
083    
084            public String getPortletId() {
085                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
086    
087                    return assetRendererFactory.getPortletId();
088            }
089    
090            public String getSummary(Locale locale) {
091                    return HtmlUtil.stripHtml(_entry.getDescription());
092            }
093    
094            public String getTitle(Locale locale) {
095                    return _entry.getTitle();
096            }
097    
098            public String getType() {
099                    return BlogsEntryAssetRendererFactory.TYPE;
100            }
101    
102            @Override
103            public PortletURL getURLEdit(
104                            LiferayPortletRequest liferayPortletRequest,
105                            LiferayPortletResponse liferayPortletResponse)
106                    throws Exception {
107    
108                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
109                            getControlPanelPlid(liferayPortletRequest), PortletKeys.BLOGS,
110                            PortletRequest.RENDER_PHASE);
111    
112                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
113                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
114    
115                    return portletURL;
116            }
117    
118            @Override
119            public String getUrlTitle() {
120                    return _entry.getUrlTitle();
121            }
122    
123            @Override
124            public PortletURL getURLView(
125                            LiferayPortletResponse liferayPortletResponse,
126                            WindowState windowState)
127                    throws Exception {
128    
129                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
130                            PortletKeys.BLOGS, PortletRequest.RENDER_PHASE);
131    
132                    portletURL.setWindowState(windowState);
133    
134                    portletURL.setParameter("struts_action", "/blogs/view_entry");
135                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
136    
137                    return portletURL;
138            }
139    
140            @Override
141            public String getURLViewInContext(
142                    LiferayPortletRequest liferayPortletRequest,
143                    LiferayPortletResponse liferayPortletResponse,
144                    String noSuchEntryRedirect) {
145    
146                    return getURLViewInContext(
147                            liferayPortletRequest, noSuchEntryRedirect, "/blogs/find_entry",
148                            "entryId", _entry.getEntryId());
149            }
150    
151            public long getUserId() {
152                    return _entry.getUserId();
153            }
154    
155            public String getUserName() {
156                    return _entry.getUserName();
157            }
158    
159            public String getUuid() {
160                    return _entry.getUuid();
161            }
162    
163            public boolean hasDeletePermission(PermissionChecker permissionChecker) {
164                    return BlogsEntryPermission.contains(
165                            permissionChecker, _entry, ActionKeys.DELETE);
166            }
167    
168            @Override
169            public boolean hasEditPermission(PermissionChecker permissionChecker) {
170                    return BlogsEntryPermission.contains(
171                            permissionChecker, _entry, ActionKeys.UPDATE);
172            }
173    
174            @Override
175            public boolean hasViewPermission(PermissionChecker permissionChecker) {
176                    return BlogsEntryPermission.contains(
177                            permissionChecker, _entry, ActionKeys.VIEW);
178            }
179    
180            @Override
181            public boolean isPrintable() {
182                    return true;
183            }
184    
185            public String render(
186                            RenderRequest renderRequest, RenderResponse renderResponse,
187                            String template)
188                    throws Exception {
189    
190                    if (template.equals(TEMPLATE_ABSTRACT) ||
191                            template.equals(TEMPLATE_FULL_CONTENT)) {
192    
193                            renderRequest.setAttribute(WebKeys.BLOGS_ENTRY, _entry);
194    
195                            return "/html/portlet/blogs/asset/" + template + ".jsp";
196                    }
197                    else {
198                            return null;
199                    }
200            }
201    
202            private BlogsEntry _entry;
203    
204    }