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