001    /**
002     * Copyright (c) 2000-2012 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.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 getUserName() {
129                    return _entry.getUserName();
130            }
131    
132            public String getUuid() {
133                    return _entry.getUuid();
134            }
135    
136            @Override
137            public boolean hasEditPermission(PermissionChecker permissionChecker) {
138                    return BlogsEntryPermission.contains(
139                            permissionChecker, _entry, ActionKeys.UPDATE);
140            }
141    
142            @Override
143            public boolean hasViewPermission(PermissionChecker permissionChecker) {
144                    return BlogsEntryPermission.contains(
145                            permissionChecker, _entry, ActionKeys.VIEW);
146            }
147    
148            @Override
149            public boolean isPrintable() {
150                    return true;
151            }
152    
153            public String render(
154                            RenderRequest renderRequest, RenderResponse renderResponse,
155                            String template)
156                    throws Exception {
157    
158                    if (template.equals(TEMPLATE_ABSTRACT) ||
159                            template.equals(TEMPLATE_FULL_CONTENT)) {
160    
161                            renderRequest.setAttribute(WebKeys.BLOGS_ENTRY, _entry);
162    
163                            return "/html/portlet/blogs/asset/" + template + ".jsp";
164                    }
165                    else {
166                            return null;
167                    }
168            }
169    
170            @Override
171            protected String getIconPath(ThemeDisplay themeDisplay) {
172                    return themeDisplay.getPathThemeImages() + "/blogs/blogs.png";
173            }
174    
175            private BlogsEntry _entry;
176    
177    }