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