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