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.util.HtmlUtil;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.BaseAssetRenderer;
027    import com.liferay.portlet.blogs.model.BlogsEntry;
028    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
029    
030    import java.util.Locale;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    import javax.portlet.WindowState;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Juan Fernández
041     * @author Sergio González
042     */
043    public class BlogsEntryAssetRenderer extends BaseAssetRenderer {
044    
045            public BlogsEntryAssetRenderer(BlogsEntry entry) {
046                    _entry = entry;
047            }
048    
049            public long getClassPK() {
050                    return _entry.getEntryId();
051            }
052    
053            @Override
054            public String getDiscussionPath() {
055                    if (PropsValues.BLOGS_ENTRY_COMMENTS_ENABLED) {
056                            return "edit_entry_discussion";
057                    }
058                    else {
059                            return null;
060                    }
061            }
062    
063            public long getGroupId() {
064                    return _entry.getGroupId();
065            }
066    
067            public String getSummary(Locale locale) {
068                    return HtmlUtil.stripHtml(_entry.getDescription());
069            }
070    
071            public String getTitle(Locale locale) {
072                    return _entry.getTitle();
073            }
074    
075            @Override
076            public PortletURL getURLEdit(
077                            LiferayPortletRequest liferayPortletRequest,
078                            LiferayPortletResponse liferayPortletResponse)
079                    throws Exception {
080    
081                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
082                            getControlPanelPlid(liferayPortletRequest), PortletKeys.BLOGS,
083                            PortletRequest.RENDER_PHASE);
084    
085                    portletURL.setParameter("struts_action", "/blogs/edit_entry");
086                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
087    
088                    return portletURL;
089            }
090    
091            @Override
092            public String getUrlTitle() {
093                    return _entry.getUrlTitle();
094            }
095    
096            @Override
097            public PortletURL getURLView(
098                            LiferayPortletResponse liferayPortletResponse,
099                            WindowState windowState)
100                    throws Exception {
101    
102                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
103                            PortletKeys.BLOGS, PortletRequest.RENDER_PHASE);
104    
105                    portletURL.setWindowState(windowState);
106    
107                    portletURL.setParameter("struts_action", "/blogs/view_entry");
108                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
109    
110                    return portletURL;
111            }
112    
113            @Override
114            public String getURLViewInContext(
115                    LiferayPortletRequest liferayPortletRequest,
116                    LiferayPortletResponse liferayPortletResponse,
117                    String noSuchEntryRedirect) {
118    
119                    return getURLViewInContext(
120                            liferayPortletRequest, noSuchEntryRedirect, "/blogs/find_entry",
121                            "entryId", _entry.getEntryId());
122            }
123    
124            public long getUserId() {
125                    return _entry.getUserId();
126            }
127    
128            public String getUuid() {
129                    return _entry.getUuid();
130            }
131    
132            @Override
133            public boolean hasEditPermission(PermissionChecker permissionChecker) {
134                    return BlogsEntryPermission.contains(
135                            permissionChecker, _entry, ActionKeys.UPDATE);
136            }
137    
138            @Override
139            public boolean hasViewPermission(PermissionChecker permissionChecker) {
140                    return BlogsEntryPermission.contains(
141                            permissionChecker, _entry, ActionKeys.VIEW);
142            }
143    
144            @Override
145            public boolean isPrintable() {
146                    return true;
147            }
148    
149            public String render(
150                            RenderRequest renderRequest, RenderResponse renderResponse,
151                            String template)
152                    throws Exception {
153    
154                    if (template.equals(TEMPLATE_ABSTRACT) ||
155                            template.equals(TEMPLATE_FULL_CONTENT)) {
156    
157                            renderRequest.setAttribute(WebKeys.BLOGS_ENTRY, _entry);
158    
159                            return "/html/portlet/blogs/asset/" + template + ".jsp";
160                    }
161                    else {
162                            return null;
163                    }
164            }
165    
166            @Override
167            protected String getIconPath(ThemeDisplay themeDisplay) {
168                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
169            }
170    
171            private BlogsEntry _entry;
172    
173    }