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