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.MBMessage;
031    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
032    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
033    
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ViewMessageAction extends PortletAction {
046    
047            @Override
048            public ActionForward render(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            RenderRequest renderRequest, 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                            if (messageDisplay != null) {
092                                    MBMessage message = messageDisplay.getMessage();
093    
094                                    if ((message != null) &&
095                                            (message.isInTrash() || message.isInTrashThread())) {
096    
097                                            throw new NoSuchMessageException();
098                                    }
099                            }
100    
101                            renderRequest.setAttribute(
102                                    WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
103    
104                            return mapping.findForward("portlet.message_boards.view_message");
105                    }
106                    catch (Exception e) {
107                            if (e instanceof NoSuchMessageException ||
108                                    e instanceof PrincipalException) {
109    
110                                    SessionErrors.add(renderRequest, e.getClass());
111    
112                                    return mapping.findForward("portlet.message_boards.error");
113                            }
114                            else {
115                                    throw e;
116                            }
117                    }
118            }
119    
120    }