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.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            @Override
053            public long getClassPK() {
054                    return _message.getMessageId();
055            }
056    
057            @Override
058            public long getGroupId() {
059                    return _message.getGroupId();
060            }
061    
062            @Override
063            public String getSearchSummary(Locale locale) {
064                    if (_message.isFormatBBCode()) {
065                            return HtmlUtil.extractText(
066                                    BBCodeTranslatorUtil.getHTML(_message.getBody()));
067                    }
068    
069                    return getSummary(locale);
070            }
071    
072            @Override
073            public String getSummary(Locale locale) {
074                    return HtmlUtil.extractText(_message.getBody());
075            }
076    
077            @Override
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.setParameter(
110                            "struts_action", "/message_boards/view_message");
111                    portletURL.setParameter(
112                            "messageId", String.valueOf(_message.getMessageId()));
113                    portletURL.setWindowState(windowState);
114    
115                    return portletURL;
116            }
117    
118            @Override
119            public String getURLViewInContext(
120                    LiferayPortletRequest liferayPortletRequest,
121                    LiferayPortletResponse liferayPortletResponse,
122                    String noSuchEntryRedirect) {
123    
124                    return getURLViewInContext(
125                            liferayPortletRequest, noSuchEntryRedirect,
126                            "/message_boards/find_message", "messageId",
127                            _message.getMessageId());
128            }
129    
130            @Override
131            public long getUserId() {
132                    return _message.getUserId();
133            }
134    
135            @Override
136            public String getUserName() {
137                    return _message.getUserName();
138            }
139    
140            @Override
141            public String getUuid() {
142                    return _message.getUuid();
143            }
144    
145            @Override
146            public boolean hasEditPermission(PermissionChecker permissionChecker)
147                    throws PortalException, SystemException {
148    
149                    if (_message.isDiscussion()) {
150                            return MBDiscussionPermission.contains(
151                                    permissionChecker, _message.getCompanyId(),
152                                    _message.getGroupId(), _message.getClassName(),
153                                    _message.getClassPK(), _message.getMessageId(),
154                                    _message.getUserId(), ActionKeys.UPDATE);
155                    }
156                    else {
157                            return MBMessagePermission.contains(
158                                    permissionChecker, _message, ActionKeys.UPDATE);
159                    }
160            }
161    
162            @Override
163            public boolean hasViewPermission(PermissionChecker permissionChecker)
164                    throws PortalException, SystemException {
165    
166                    if (_message.isDiscussion()) {
167                            return MBDiscussionPermission.contains(
168                                    permissionChecker, _message.getCompanyId(),
169                                    _message.getGroupId(), _message.getClassName(),
170                                    _message.getClassPK(), _message.getMessageId(),
171                                    _message.getUserId(), ActionKeys.VIEW);
172                    }
173                    else {
174                            return MBMessagePermission.contains(
175                                    permissionChecker, _message, ActionKeys.VIEW);
176                    }
177            }
178    
179            @Override
180            public boolean isPrintable() {
181                    return true;
182            }
183    
184            @Override
185            public String render(
186                            RenderRequest renderRequest, RenderResponse renderResponse,
187                            String template)
188                    throws Exception {
189    
190                    if (template.equals(TEMPLATE_ABSTRACT) ||
191                            template.equals(TEMPLATE_FULL_CONTENT)) {
192    
193                            renderRequest.setAttribute(
194                                    WebKeys.MESSAGE_BOARDS_MESSAGE, _message);
195    
196                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
197                    }
198                    else {
199                            return null;
200                    }
201            }
202    
203            @Override
204            protected String getIconPath(ThemeDisplay themeDisplay) {
205                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
206            }
207    
208            private MBMessage _message;
209    
210    }