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.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 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     */
045    public class MBMessageAssetRenderer extends BaseAssetRenderer {
046    
047            public MBMessageAssetRenderer(MBMessage message) {
048                    _message = message;
049            }
050    
051            public long getClassPK() {
052                    return _message.getMessageId();
053            }
054    
055            public long getGroupId() {
056                    return _message.getGroupId();
057            }
058    
059            public String getSummary(Locale locale) {
060                    return HtmlUtil.stripHtml(_message.getBody());
061            }
062    
063            public String getTitle(Locale locale) {
064                    return _message.getSubject();
065            }
066    
067            @Override
068            public PortletURL getURLEdit(
069                            LiferayPortletRequest liferayPortletRequest,
070                            LiferayPortletResponse liferayPortletResponse)
071                    throws Exception {
072    
073                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
074                            getControlPanelPlid(liferayPortletRequest),
075                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
076    
077                    portletURL.setParameter(
078                            "struts_action", "/message_boards/edit_message");
079                    portletURL.setParameter(
080                            "messageId", String.valueOf(_message.getMessageId()));
081    
082                    return portletURL;
083            }
084    
085            @Override
086            public PortletURL getURLView(
087                            LiferayPortletResponse liferayPortletResponse,
088                            WindowState windowState)
089                    throws Exception {
090    
091                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
092                            PortletKeys.MESSAGE_BOARDS, PortletRequest.RENDER_PHASE);
093    
094                    portletURL.setWindowState(windowState);
095    
096                    portletURL.setParameter(
097                            "struts_action", "/message_boards/view_message");
098                    portletURL.setParameter(
099                            "messageId", String.valueOf(_message.getMessageId()));
100    
101                    return portletURL;
102            }
103    
104            @Override
105            public String getURLViewInContext(
106                    LiferayPortletRequest liferayPortletRequest,
107                    LiferayPortletResponse liferayPortletResponse,
108                    String noSuchEntryRedirect) {
109    
110                    return getURLViewInContext(
111                            liferayPortletRequest, noSuchEntryRedirect,
112                            "/message_boards/find_message", "messageId",
113                            _message.getMessageId());
114            }
115    
116            public long getUserId() {
117                    return _message.getUserId();
118            }
119    
120            public String getUuid() {
121                    return _message.getUuid();
122            }
123    
124            @Override
125            public boolean hasEditPermission(PermissionChecker permissionChecker)
126                    throws PortalException, SystemException {
127    
128                    if (_message.isDiscussion()) {
129                            return MBDiscussionPermission.contains(
130                                    permissionChecker, _message.getCompanyId(),
131                                    _message.getGroupId(), _message.getClassName(),
132                                    _message.getClassPK(), _message.getMessageId(),
133                                    _message.getUserId(), ActionKeys.UPDATE);
134                    }
135                    else {
136                            return MBMessagePermission.contains(
137                                    permissionChecker, _message, ActionKeys.UPDATE);
138                    }
139            }
140    
141            @Override
142            public boolean hasViewPermission(PermissionChecker permissionChecker)
143                    throws PortalException, SystemException {
144    
145                    if (_message.isDiscussion()) {
146                            return MBDiscussionPermission.contains(
147                                    permissionChecker, _message.getCompanyId(),
148                                    _message.getGroupId(), _message.getClassName(),
149                                    _message.getClassPK(), _message.getMessageId(),
150                                    _message.getUserId(), ActionKeys.VIEW);
151                    }
152                    else {
153                            return MBMessagePermission.contains(
154                                    permissionChecker, _message, ActionKeys.VIEW);
155                    }
156            }
157    
158            @Override
159            public boolean isPrintable() {
160                    return true;
161            }
162    
163            public String render(
164                            RenderRequest renderRequest, RenderResponse renderResponse,
165                            String template)
166                    throws Exception {
167    
168                    if (template.equals(TEMPLATE_ABSTRACT) ||
169                            template.equals(TEMPLATE_FULL_CONTENT)) {
170    
171                            renderRequest.setAttribute(
172                                    WebKeys.MESSAGE_BOARDS_MESSAGE, _message);
173    
174                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
175                    }
176                    else {
177                            return null;
178                    }
179            }
180    
181            @Override
182            protected String getIconPath(ThemeDisplay themeDisplay) {
183                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
184            }
185    
186            private MBMessage _message;
187    
188    }