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