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