001    /**
002     * Copyright (c) 2000-2013 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            @Override
052            public long getClassPK() {
053                    return _category.getCategoryId();
054            }
055    
056            @Override
057            public long getGroupId() {
058                    return _category.getGroupId();
059            }
060    
061            @Override
062            public String getSummary(Locale locale) {
063                    return HtmlUtil.stripHtml(_category.getDescription());
064            }
065    
066            @Override
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.setParameter("struts_action", "/message_boards/view");
099                    portletURL.setParameter(
100                            "mbCategoryId", String.valueOf(_category.getCategoryId()));
101                    portletURL.setWindowState(windowState);
102    
103                    return portletURL;
104            }
105    
106            @Override
107            public String getURLViewInContext(
108                    LiferayPortletRequest liferayPortletRequest,
109                    LiferayPortletResponse liferayPortletResponse,
110                    String noSuchEntryRedirect) {
111    
112                    return getURLViewInContext(
113                            liferayPortletRequest, noSuchEntryRedirect,
114                            "/message_boards/find_category", "mbCategoryId",
115                            _category.getCategoryId());
116            }
117    
118            @Override
119            public long getUserId() {
120                    return _category.getUserId();
121            }
122    
123            @Override
124            public String getUserName() {
125                    return _category.getUserName();
126            }
127    
128            @Override
129            public String getUuid() {
130                    return _category.getUuid();
131            }
132    
133            @Override
134            public boolean hasEditPermission(PermissionChecker permissionChecker)
135                    throws PortalException, SystemException {
136    
137                    return MBCategoryPermission.contains(
138                            permissionChecker, _category, ActionKeys.UPDATE);
139            }
140    
141            @Override
142            public boolean hasViewPermission(PermissionChecker permissionChecker)
143                    throws PortalException, SystemException {
144    
145                    return MBCategoryPermission.contains(
146                            permissionChecker, _category, ActionKeys.VIEW);
147    
148            }
149    
150            @Override
151            public String render(
152                            RenderRequest renderRequest, RenderResponse renderResponse,
153                            String template)
154                    throws Exception {
155    
156                    if (template.equals(TEMPLATE_ABSTRACT) ||
157                            template.equals(TEMPLATE_FULL_CONTENT)) {
158    
159                            renderRequest.setAttribute(
160                                    WebKeys.MESSAGE_BOARDS_CATEGORY, _category);
161    
162                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
163                    }
164                    else {
165                            return null;
166                    }
167            }
168    
169            @Override
170            protected String getIconPath(ThemeDisplay themeDisplay) {
171                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
172            }
173    
174            private MBCategory _category;
175    
176    }