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 long getClassPK() {
058                    return _entry.getEntryId();
059            }
060    
061            @Override
062            public String getDiscussionPath() {
063                    if (PropsValues.BLOGS_ENTRY_COMMENTS_ENABLED) {
064                            return "edit_entry_discussion";
065                    }
066                    else {
067                            return null;
068                    }
069            }
070    
071            public long getGroupId() {
072                    return _entry.getGroupId();
073            }
074    
075            @Override
076            public String getIconPath(ThemeDisplay themeDisplay) {
077                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
078            }
079    
080            public String getPortletId() {
081                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
082    
083                    return assetRendererFactory.getPortletId();
084            }
085    
086            public String getSummary(Locale locale) {
087                    return HtmlUtil.stripHtml(_entry.getDescription());
088            }
089    
090            public String getTitle(Locale locale) {
091                    return _entry.getTitle();
092            }
093    
094            public String getType() {
095                    return BlogsEntryAssetRendererFactory.TYPE;
096            }
097    
098            @Override
099            public PortletURL getURLEdit(
100                            LiferayPortletRequest liferayPortletRequest,
101                            LiferayPortletResponse liferayPortletResponse)
102                    throws Exception {
103    
104                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
105                            getControlPanelPlid(liferayPortletRequest), PortletKeys.BLOGS,
106                            PortletRequest.RENDER_PHASE);
107    
108                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
109                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
110    
111                    return portletURL;
112            }
113    
114            @Override
115            public String getUrlTitle() {
116                    return _entry.getUrlTitle();
117            }
118    
119            @Override
120            public PortletURL getURLView(
121                            LiferayPortletResponse liferayPortletResponse,
122                            WindowState windowState)
123                    throws Exception {
124    
125                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
126                            PortletKeys.BLOGS, PortletRequest.RENDER_PHASE);
127    
128                    portletURL.setWindowState(windowState);
129    
130                    portletURL.setParameter("struts_action", "/blogs/view_entry");
131                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
132    
133                    return portletURL;
134            }
135    
136            @Override
137            public String getURLViewInContext(
138                    LiferayPortletRequest liferayPortletRequest,
139                    LiferayPortletResponse liferayPortletResponse,
140                    String noSuchEntryRedirect) {
141    
142                    return getURLViewInContext(
143                            liferayPortletRequest, noSuchEntryRedirect, "/blogs/find_entry",
144                            "entryId", _entry.getEntryId());
145            }
146    
147            public long getUserId() {
148                    return _entry.getUserId();
149            }
150    
151            public String getUserName() {
152                    return _entry.getUserName();
153            }
154    
155            public String getUuid() {
156                    return _entry.getUuid();
157            }
158    
159            public boolean hasDeletePermission(PermissionChecker permissionChecker) {
160                    return BlogsEntryPermission.contains(
161                            permissionChecker, _entry, ActionKeys.DELETE);
162            }
163    
164            @Override
165            public boolean hasEditPermission(PermissionChecker permissionChecker) {
166                    return BlogsEntryPermission.contains(
167                            permissionChecker, _entry, ActionKeys.UPDATE);
168            }
169    
170            @Override
171            public boolean hasViewPermission(PermissionChecker permissionChecker) {
172                    return BlogsEntryPermission.contains(
173                            permissionChecker, _entry, ActionKeys.VIEW);
174            }
175    
176            @Override
177            public boolean isPrintable() {
178                    return true;
179            }
180    
181            public String render(
182                            RenderRequest renderRequest, RenderResponse renderResponse,
183                            String template)
184                    throws Exception {
185    
186                    if (template.equals(TEMPLATE_ABSTRACT) ||
187                            template.equals(TEMPLATE_FULL_CONTENT)) {
188    
189                            renderRequest.setAttribute(WebKeys.BLOGS_ENTRY, _entry);
190    
191                            return "/html/portlet/blogs/asset/" + template + ".jsp";
192                    }
193                    else {
194                            return null;
195                    }
196            }
197    
198            private BlogsEntry _entry;
199    
200    }