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 long getClassPK() {
052                    return _category.getCategoryId();
053            }
054    
055            public long getGroupId() {
056                    return _category.getGroupId();
057            }
058    
059            public String getSummary(Locale locale) {
060                    return HtmlUtil.stripHtml(_category.getDescription());
061            }
062    
063            public String getTitle(Locale locale) {
064                    return _category.getName();
065            }
066    
067            @Override
068            public PortletURL getURLEdit(
069                            LiferayPortletRequest liferayPortletRequest,
070                            LiferayPortletResponse liferayPortletResponse)
071                    throws Exception {
072    
073                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
074                            getControlPanelPlid(liferayPortletRequest),
075                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
076    
077                    portletURL.setParameter(
078                            "struts_action", "/message_boards/edit_category");
079                    portletURL.setParameter(
080                            "mbCategoryId", String.valueOf(_category.getCategoryId()));
081    
082                    return portletURL;
083            }
084    
085            @Override
086            public PortletURL getURLView(
087                            LiferayPortletResponse liferayPortletResponse,
088                            WindowState windowState)
089                    throws Exception {
090    
091                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
092                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
093    
094                    portletURL.setWindowState(windowState);
095    
096                    portletURL.setParameter("struts_action", "/message_boards/view");
097                    portletURL.setParameter(
098                            "mbCategoryId", String.valueOf(_category.getCategoryId()));
099    
100                    return portletURL;
101            }
102    
103            @Override
104            public String getURLViewInContext(
105                    LiferayPortletRequest liferayPortletRequest,
106                    LiferayPortletResponse liferayPortletResponse,
107                    String noSuchEntryRedirect) {
108    
109                    return getURLViewInContext(
110                            liferayPortletRequest, noSuchEntryRedirect,
111                            "/message_boards/find_category", "mbCategoryId",
112                            _category.getCategoryId());
113            }
114    
115            public long getUserId() {
116                    return _category.getUserId();
117            }
118    
119            public String getUuid() {
120                    return _category.getUuid();
121            }
122    
123            @Override
124            public boolean hasEditPermission(PermissionChecker permissionChecker)
125                    throws PortalException, SystemException {
126    
127                    return MBCategoryPermission.contains(
128                            permissionChecker, _category, ActionKeys.UPDATE);
129            }
130    
131            @Override
132            public boolean hasViewPermission(PermissionChecker permissionChecker)
133                    throws PortalException, SystemException {
134    
135                    return MBCategoryPermission.contains(
136                            permissionChecker, _category, ActionKeys.VIEW);
137    
138            }
139    
140            public String render(
141                            RenderRequest renderRequest, RenderResponse renderResponse,
142                            String template)
143                    throws Exception {
144    
145                    if (template.equals(TEMPLATE_ABSTRACT) ||
146                            template.equals(TEMPLATE_FULL_CONTENT)) {
147    
148                            renderRequest.setAttribute(
149                                    WebKeys.MESSAGE_BOARDS_CATEGORY, _category);
150    
151                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
152                    }
153                    else {
154                            return null;
155                    }
156            }
157    
158            @Override
159            protected String getIconPath(ThemeDisplay themeDisplay) {
160                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
161            }
162    
163            private MBCategory _category;
164    
165    }