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.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.struts.PortletAction;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.messageboards.NoSuchMessageException;
030    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
031    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
032    
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class ViewMessageAction extends PortletAction {
045    
046            @Override
047            public ActionForward render(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, RenderRequest renderRequest,
050                            RenderResponse renderResponse)
051                    throws Exception {
052    
053                    try {
054                            long messageId = ParamUtil.getLong(renderRequest, "messageId");
055    
056                            PortalPreferences preferences =
057                                    PortletPreferencesFactoryUtil.getPortalPreferences(
058                                            renderRequest);
059    
060                            String threadView = ParamUtil.getString(
061                                    renderRequest, "threadView");
062    
063                            if (Validator.isNotNull(threadView)) {
064                                    preferences.setValue(
065                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
066                            }
067                            else {
068                                    threadView = preferences.getValue(
069                                            PortletKeys.MESSAGE_BOARDS, "thread-view",
070                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
071                            }
072    
073                            if (!ArrayUtil.contains(
074                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
075    
076                                    threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
077    
078                                    preferences.setValue(
079                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
080                            }
081    
082                            boolean includePrevAndNext =
083                                    PropsValues.
084                                            MESSAGE_BOARDS_THREAD_PREVIOUS_AND_NEXT_NAVIGATION_ENABLED;
085    
086                            MBMessageDisplay messageDisplay =
087                                    MBMessageServiceUtil.getMessageDisplay(
088                                            messageId, WorkflowConstants.STATUS_ANY, threadView,
089                                            includePrevAndNext);
090    
091                            renderRequest.setAttribute(
092                                    WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
093    
094                            return actionMapping.findForward(
095                                    "portlet.message_boards.view_message");
096                    }
097                    catch (Exception e) {
098                            if (e instanceof NoSuchMessageException ||
099                                    e instanceof PrincipalException) {
100    
101                                    SessionErrors.add(renderRequest, e.getClass());
102    
103                                    return actionMapping.findForward(
104                                            "portlet.message_boards.error");
105                            }
106                            else {
107                                    throw e;
108                            }
109                    }
110            }
111    
112    }