001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 getUserName() {
121                    return _message.getUserName();
122            }
123    
124            public String getUuid() {
125                    return _message.getUuid();
126            }
127    
128            @Override
129            public boolean hasEditPermission(PermissionChecker permissionChecker)
130                    throws PortalException, SystemException {
131    
132                    if (_message.isDiscussion()) {
133                            return MBDiscussionPermission.contains(
134                                    permissionChecker, _message.getCompanyId(),
135                                    _message.getGroupId(), _message.getClassName(),
136                                    _message.getClassPK(), _message.getMessageId(),
137                                    _message.getUserId(), ActionKeys.UPDATE);
138                    }
139                    else {
140                            return MBMessagePermission.contains(
141                                    permissionChecker, _message, ActionKeys.UPDATE);
142                    }
143            }
144    
145            @Override
146            public boolean hasViewPermission(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.VIEW);
155                    }
156                    else {
157                            return MBMessagePermission.contains(
158                                    permissionChecker, _message, ActionKeys.VIEW);
159                    }
160            }
161    
162            @Override
163            public boolean isPrintable() {
164                    return true;
165            }
166    
167            public String render(
168                            RenderRequest renderRequest, RenderResponse renderResponse,
169                            String template)
170                    throws Exception {
171    
172                    if (template.equals(TEMPLATE_ABSTRACT) ||
173                            template.equals(TEMPLATE_FULL_CONTENT)) {
174    
175                            renderRequest.setAttribute(
176                                    WebKeys.MESSAGE_BOARDS_MESSAGE, _message);
177    
178                            return "/html/portlet/message_boards/asset/" + template + ".jsp";
179                    }
180                    else {
181                            return null;
182                    }
183            }
184    
185            @Override
186            protected String getIconPath(ThemeDisplay themeDisplay) {
187                    return themeDisplay.getPathThemeImages() + "/common/conversation.png";
188            }
189    
190            private MBMessage _message;
191    
192    }