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