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            public String getClassName() {
052                    return MBCategory.class.getName();
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.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            public long getUserId() {
119                    return _category.getUserId();
120            }
121    
122            public String getUserName() {
123                    return _category.getUserName();
124            }
125    
126            public String getUuid() {
127                    return _category.getUuid();
128            }
129    
130            @Override
131            public boolean hasEditPermission(PermissionChecker permissionChecker)
132                    throws PortalException, SystemException {
133    
134                    return MBCategoryPermission.contains(
135                            permissionChecker, _category, ActionKeys.UPDATE);
136            }
137    
138            @Override
139            public boolean hasViewPermission(PermissionChecker permissionChecker)
140                    throws PortalException, SystemException {
141    
142                    return MBCategoryPermission.contains(
143                            permissionChecker, _category, ActionKeys.VIEW);
144    
145            }
146    
147            public String render(
148                            RenderRequest renderRequest, RenderResponse renderResponse,
149                            String template)
150                    throws Exception {
151    
152                    if (template.equals(TEMPLATE_ABSTRACT) ||
153                            template.equals(TEMPLATE_FULL_CONTENT)) {
154    
155                            renderRequest.setAttribute(
156                                    WebKeys.MESSAGE_BOARDS_CATEGORY, _category);
157    
158                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
159                    }
160                    else {
161                            return null;
162                    }
163            }
164    
165            @Override
166            protected String getIconPath(ThemeDisplay themeDisplay) {
167                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
168            }
169    
170            private MBCategory _category;
171    
172    }