001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 int getStatus() {
100                    return _entry.getStatus();
101            }
102    
103            @Override
104            public String getSummary(Locale locale) {
105                    String summary = _entry.getDescription();
106    
107                    if (Validator.isNull(summary)) {
108                            summary = StringUtil.shorten(
109                                    HtmlUtil.stripHtml(_entry.getContent()), 200);
110                    }
111    
112                    return summary;
113            }
114    
115            @Override
116            public String getThumbnailPath(PortletRequest portletRequest)
117                    throws Exception {
118    
119                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
120                            WebKeys.THEME_DISPLAY);
121    
122                    return themeDisplay.getPathThemeImages() +
123                            "/file_system/large/blog.png";
124            }
125    
126            @Override
127            public String getTitle(Locale locale) {
128                    return _entry.getTitle();
129            }
130    
131            @Override
132            public String getType() {
133                    return BlogsEntryAssetRendererFactory.TYPE;
134            }
135    
136            @Override
137            public PortletURL getURLEdit(
138                            LiferayPortletRequest liferayPortletRequest,
139                            LiferayPortletResponse liferayPortletResponse)
140                    throws Exception {
141    
142                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
143                            getControlPanelPlid(liferayPortletRequest), PortletKeys.BLOGS,
144                            PortletRequest.RENDER_PHASE);
145    
146                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
147                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
148    
149                    return portletURL;
150            }
151    
152            @Override
153            public String getUrlTitle() {
154                    return _entry.getUrlTitle();
155            }
156    
157            @Override
158            public PortletURL getURLView(
159                            LiferayPortletResponse liferayPortletResponse,
160                            WindowState windowState)
161                    throws Exception {
162    
163                    AssetRendererFactory assetRendererFactory = getAssetRendererFactory();
164    
165                    PortletURL portletURL = assetRendererFactory.getURLView(
166                            liferayPortletResponse, windowState);
167    
168                    portletURL.setParameter("struts_action", "/blogs/view_entry");
169                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
170                    portletURL.setWindowState(windowState);
171    
172                    return portletURL;
173            }
174    
175            @Override
176            public String getURLViewInContext(
177                    LiferayPortletRequest liferayPortletRequest,
178                    LiferayPortletResponse liferayPortletResponse,
179                    String noSuchEntryRedirect) {
180    
181                    return getURLViewInContext(
182                            liferayPortletRequest, noSuchEntryRedirect, "/blogs/find_entry",
183                            "entryId", _entry.getEntryId());
184            }
185    
186            @Override
187            public long getUserId() {
188                    return _entry.getUserId();
189            }
190    
191            @Override
192            public String getUserName() {
193                    return _entry.getUserName();
194            }
195    
196            @Override
197            public String getUuid() {
198                    return _entry.getUuid();
199            }
200    
201            public boolean hasDeletePermission(PermissionChecker permissionChecker) {
202                    return BlogsEntryPermission.contains(
203                            permissionChecker, _entry, ActionKeys.DELETE);
204            }
205    
206            @Override
207            public boolean hasEditPermission(PermissionChecker permissionChecker) {
208                    return BlogsEntryPermission.contains(
209                            permissionChecker, _entry, ActionKeys.UPDATE);
210            }
211    
212            @Override
213            public boolean hasViewPermission(PermissionChecker permissionChecker) {
214                    return BlogsEntryPermission.contains(
215                            permissionChecker, _entry, ActionKeys.VIEW);
216            }
217    
218            @Override
219            public boolean isPrintable() {
220                    return true;
221            }
222    
223            @Override
224            public String render(
225                            RenderRequest renderRequest, RenderResponse renderResponse,
226                            String template)
227                    throws Exception {
228    
229                    if (template.equals(TEMPLATE_ABSTRACT) ||
230                            template.equals(TEMPLATE_FULL_CONTENT)) {
231    
232                            renderRequest.setAttribute(WebKeys.BLOGS_ENTRY, _entry);
233    
234                            return "/html/portlet/blogs/asset/" + template + ".jsp";
235                    }
236                    else {
237                            return null;
238                    }
239            }
240    
241            private BlogsEntry _entry;
242    
243    }