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.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 mapping, ActionForm form, PortletConfig portletConfig,
049                            RenderRequest renderRequest, RenderResponse renderResponse)
050                    throws Exception {
051    
052                    try {
053                            long messageId = ParamUtil.getLong(renderRequest, "messageId");
054    
055                            PortalPreferences preferences =
056                                    PortletPreferencesFactoryUtil.getPortalPreferences(
057                                            renderRequest);
058    
059                            String threadView = ParamUtil.getString(
060                                    renderRequest, "threadView");
061    
062                            if (Validator.isNotNull(threadView)) {
063                                    preferences.setValue(
064                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
065                            }
066                            else {
067                                    threadView = preferences.getValue(
068                                            PortletKeys.MESSAGE_BOARDS, "thread-view",
069                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
070                            }
071    
072                            if (!ArrayUtil.contains(
073                                            PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
074    
075                                    threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
076    
077                                    preferences.setValue(
078                                            PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
079                            }
080    
081                            boolean includePrevAndNext =
082                                    PropsValues.
083                                            MESSAGE_BOARDS_THREAD_PREVIOUS_AND_NEXT_NAVIGATION_ENABLED;
084    
085                            MBMessageDisplay messageDisplay =
086                                    MBMessageServiceUtil.getMessageDisplay(
087                                            messageId, WorkflowConstants.STATUS_ANY, threadView,
088                                            includePrevAndNext);
089    
090                            renderRequest.setAttribute(
091                                    WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
092    
093                            return mapping.findForward("portlet.message_boards.view_message");
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchMessageException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(renderRequest, e.getClass().getName());
100    
101                                    return mapping.findForward("portlet.message_boards.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107            }
108    
109    }