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