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