001    /**
002     * Copyright (c) 2000-present 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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.PortalPreferences;
030    import com.liferay.portlet.PortletPreferencesFactoryUtil;
031    import com.liferay.portlet.messageboards.NoSuchMessageException;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
034    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
035    
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class ViewMessageAction extends PortletAction {
048    
049            @Override
050            public ActionForward render(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, RenderRequest renderRequest,
053                            RenderResponse renderResponse)
054                    throws Exception {
055    
056                    try {
057                            long messageId = ParamUtil.getLong(renderRequest, "messageId");
058    
059                            ThemeDisplay themeDisplay =
060                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
061    
062                            PermissionChecker permissionChecker =
063                                    themeDisplay.getPermissionChecker();
064    
065                            int status = WorkflowConstants.STATUS_APPROVED;
066    
067                            if (permissionChecker.isContentReviewer(
068                                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId())) {
069    
070                                    status = WorkflowConstants.STATUS_ANY;
071                            }
072    
073                            PortalPreferences preferences =
074                                    PortletPreferencesFactoryUtil.getPortalPreferences(
075                                            renderRequest);
076    
077                            String threadView = ParamUtil.getString(
078                                    renderRequest, "threadView");
079    
080                            if (Validator.isNotNull(threadView)) {
081                                    preferences.setValue(
082                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
083                            }
084                            else {
085                                    threadView = preferences.getValue(
086                                            PortletKeys.MESSAGE_BOARDS, "thread-view",
087                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
088                            }
089    
090                            if (!ArrayUtil.contains(
091                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
092    
093                                    threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
094    
095                                    preferences.setValue(
096                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
097                            }
098    
099                            boolean includePrevAndNext =
100                                    PropsValues.
101                                            MESSAGE_BOARDS_THREAD_PREVIOUS_AND_NEXT_NAVIGATION_ENABLED;
102    
103                            MBMessageDisplay messageDisplay =
104                                    MBMessageServiceUtil.getMessageDisplay(
105                                            messageId, status, threadView, includePrevAndNext);
106    
107                            if (messageDisplay != null) {
108                                    MBMessage message = messageDisplay.getMessage();
109    
110                                    if ((message != null) && message.isInTrash()) {
111                                            throw new NoSuchMessageException(
112                                                    "{messageId=" + messageId + "}");
113                                    }
114                            }
115    
116                            renderRequest.setAttribute(
117                                    WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
118    
119                            return actionMapping.findForward(
120                                    "portlet.message_boards.view_message");
121                    }
122                    catch (Exception e) {
123                            if (e instanceof NoSuchMessageException ||
124                                    e instanceof PrincipalException) {
125    
126                                    SessionErrors.add(renderRequest, e.getClass());
127    
128                                    return actionMapping.findForward(
129                                            "portlet.message_boards.error");
130                            }
131                            else {
132                                    throw e;
133                            }
134                    }
135            }
136    
137    }