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.parsers.bbcode.BBCodeTranslatorUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.model.BaseAssetRenderer;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
031    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
032    
033    import java.util.Locale;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    import javax.portlet.WindowState;
040    
041    /**
042     * @author Julio Camarero
043     * @author Juan Fernández
044     * @author Sergio González
045     */
046    public class MBMessageAssetRenderer extends BaseAssetRenderer {
047    
048            public MBMessageAssetRenderer(MBMessage message) {
049                    _message = message;
050            }
051    
052            public String getAssetRendererFactoryClassName() {
053                    return MBCategoryAssetRendererFactory.CLASS_NAME;
054            }
055    
056            public long getClassPK() {
057                    return _message.getMessageId();
058            }
059    
060            public long getGroupId() {
061                    return _message.getGroupId();
062            }
063    
064            @Override
065            public String getSearchSummary(Locale locale) {
066                    if (_message.isFormatBBCode()) {
067                            return HtmlUtil.extractText(
068                                    BBCodeTranslatorUtil.getHTML(_message.getBody()));
069                    }
070    
071                    return getSummary(locale);
072            }
073    
074            public String getSummary(Locale locale) {
075                    return HtmlUtil.stripHtml(_message.getBody());
076            }
077    
078            public String getTitle(Locale locale) {
079                    return _message.getSubject();
080            }
081    
082            @Override
083            public PortletURL getURLEdit(
084                            LiferayPortletRequest liferayPortletRequest,
085                            LiferayPortletResponse liferayPortletResponse)
086                    throws Exception {
087    
088                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
089                            getControlPanelPlid(liferayPortletRequest),
090                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
091    
092                    portletURL.setParameter(
093                            "struts_action", "/message_boards/edit_message");
094                    portletURL.setParameter(
095                            "messageId", String.valueOf(_message.getMessageId()));
096    
097                    return portletURL;
098            }
099    
100            @Override
101            public PortletURL getURLView(
102                            LiferayPortletResponse liferayPortletResponse,
103                            WindowState windowState)
104                    throws Exception {
105    
106                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
107                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
108    
109                    portletURL.setWindowState(windowState);
110    
111                    portletURL.setParameter(
112                            "struts_action", "/message_boards/view_message");
113                    portletURL.setParameter(
114                            "messageId", String.valueOf(_message.getMessageId()));
115    
116                    return portletURL;
117            }
118    
119            @Override
120            public String getURLViewInContext(
121                    LiferayPortletRequest liferayPortletRequest,
122                    LiferayPortletResponse liferayPortletResponse,
123                    String noSuchEntryRedirect) {
124    
125                    return getURLViewInContext(
126                            liferayPortletRequest, noSuchEntryRedirect,
127                            "/message_boards/find_message", "messageId",
128                            _message.getMessageId());
129            }
130    
131            public long getUserId() {
132                    return _message.getUserId();
133            }
134    
135            public String getUserName() {
136                    return _message.getUserName();
137            }
138    
139            public String getUuid() {
140                    return _message.getUuid();
141            }
142    
143            @Override
144            public boolean hasEditPermission(PermissionChecker permissionChecker)
145                    throws PortalException, SystemException {
146    
147                    if (_message.isDiscussion()) {
148                            return MBDiscussionPermission.contains(
149                                    permissionChecker, _message.getCompanyId(),
150                                    _message.getGroupId(), _message.getClassName(),
151                                    _message.getClassPK(), _message.getMessageId(),
152                                    _message.getUserId(), ActionKeys.UPDATE);
153                    }
154                    else {
155                            return MBMessagePermission.contains(
156                                    permissionChecker, _message, ActionKeys.UPDATE);
157                    }
158            }
159    
160            @Override
161            public boolean hasViewPermission(PermissionChecker permissionChecker)
162                    throws PortalException, SystemException {
163    
164                    if (_message.isDiscussion()) {
165                            return MBDiscussionPermission.contains(
166                                    permissionChecker, _message.getCompanyId(),
167                                    _message.getGroupId(), _message.getClassName(),
168                                    _message.getClassPK(), _message.getMessageId(),
169                                    _message.getUserId(), ActionKeys.VIEW);
170                    }
171                    else {
172                            return MBMessagePermission.contains(
173                                    permissionChecker, _message, ActionKeys.VIEW);
174                    }
175            }
176    
177            @Override
178            public boolean isPrintable() {
179                    return true;
180            }
181    
182            public String render(
183                            RenderRequest renderRequest, RenderResponse renderResponse,
184                            String template)
185                    throws Exception {
186    
187                    if (template.equals(TEMPLATE_ABSTRACT) ||
188                            template.equals(TEMPLATE_FULL_CONTENT)) {
189    
190                            renderRequest.setAttribute(
191                                    WebKeys.MESSAGE_BOARDS_MESSAGE, _message);
192    
193                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
194                    }
195                    else {
196                            return null;
197                    }
198            }
199    
200            @Override
201            protected String getIconPath(ThemeDisplay themeDisplay) {
202                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
203            }
204    
205            private MBMessage _message;
206    
207    }