001    /**
002     * Copyright (c) 2000-2010 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.MBMessage;
029    import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
030    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
031    
032    import javax.portlet.PortletURL;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    /**
037     * @author Julio Camarero
038     * @author Juan Fernández
039     */
040    public class MBMessageAssetRenderer extends BaseAssetRenderer {
041    
042            public MBMessageAssetRenderer(MBMessage message) {
043                    _message = message;
044            }
045    
046            public long getClassPK() {
047                    return _message.getMessageId();
048            }
049    
050            public long getGroupId() {
051                    return _message.getGroupId();
052            }
053    
054            public String getSummary() {
055                    return HtmlUtil.stripHtml(_message.getBody());
056            }
057    
058            public String getTitle() {
059                    return _message.getSubject();
060            }
061    
062            public PortletURL getURLEdit(
063                    LiferayPortletRequest liferayPortletRequest,
064                    LiferayPortletResponse liferayPortletResponse) {
065    
066                    PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
067                            PortletKeys.MESSAGE_BOARDS);
068    
069                    editPortletURL.setParameter(
070                            "struts_action", "/message_boards/edit_message");
071                    editPortletURL.setParameter(
072                            "messageId", String.valueOf(_message.getMessageId()));
073    
074                    return editPortletURL;
075            }
076            public String getURLViewInContext(
077                    LiferayPortletRequest liferayPortletRequest,
078                    LiferayPortletResponse liferayPortletResponse,
079                    String noSuchEntryRedirect) {
080    
081                    ThemeDisplay themeDisplay =
082                            (ThemeDisplay)liferayPortletRequest.getAttribute(
083                                    WebKeys.THEME_DISPLAY);
084    
085                    return themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
086                            "/message_boards/find_message?messageId=" + _message.getMessageId();
087            }
088    
089            public long getUserId() {
090                    return _message.getUserId();
091            }
092    
093            public String getUuid() {
094                    return _message.getUuid();
095            }
096    
097            public boolean hasEditPermission(PermissionChecker permissionChecker)
098                    throws PortalException, SystemException {
099    
100                    if (_message.isDiscussion()) {
101                            return MBDiscussionPermission.contains(
102                                    permissionChecker, _message.getCompanyId(),
103                                    _message.getGroupId(), _message.getClassName(),
104                                    _message.getClassPK(), _message.getMessageId(),
105                                    ActionKeys.UPDATE);
106                    }
107                    else {
108                            return MBMessagePermission.contains(
109                                    permissionChecker, _message, ActionKeys.UPDATE);
110                    }
111            }
112    
113            public boolean hasViewPermission(PermissionChecker permissionChecker)
114                    throws PortalException, SystemException {
115    
116                    if (_message.isDiscussion()) {
117                            return MBDiscussionPermission.contains(
118                                    permissionChecker, _message.getCompanyId(),
119                                    _message.getGroupId(), _message.getClassName(),
120                                    _message.getClassPK(), _message.getMessageId(),
121                                    ActionKeys.VIEW);
122                    }
123                    else {
124                            return MBMessagePermission.contains(
125                                    permissionChecker, _message, ActionKeys.VIEW);
126                    }
127            }
128    
129            public boolean isPrintable() {
130                    return true;
131            }
132    
133            public String render(
134                            RenderRequest renderRequest, RenderResponse renderResponse,
135                            String template)
136                    throws Exception {
137    
138                    if (template.equals(TEMPLATE_ABSTRACT) ||
139                            template.equals(TEMPLATE_FULL_CONTENT)) {
140    
141                            renderRequest.setAttribute(
142                                    WebKeys.MESSAGE_BOARDS_MESSAGE, _message);
143    
144                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
145                    }
146                    else {
147                            return null;
148                    }
149            }
150    
151            protected String getIconPath(ThemeDisplay themeDisplay) {
152                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
153            }
154    
155            private MBMessage _message;
156    
157    }