1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ArrayUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.workflow.WorkflowConstants;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portal.util.PropsValues;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortalPreferences;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  import com.liferay.portlet.messageboards.NoSuchMessageException;
30  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
31  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
32  
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="ViewMessageAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class ViewMessageAction extends PortletAction {
47  
48      public ActionForward render(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              RenderRequest renderRequest, RenderResponse renderResponse)
51          throws Exception {
52  
53          try {
54              long messageId = ParamUtil.getLong(renderRequest, "messageId");
55  
56              PortalPreferences preferences =
57                  PortletPreferencesFactoryUtil.getPortalPreferences(
58                      renderRequest);
59  
60              String threadView = ParamUtil.getString(
61                  renderRequest, "threadView");
62  
63              if (Validator.isNotNull(threadView)) {
64                  preferences.setValue(
65                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
66              }
67              else {
68                  threadView = preferences.getValue(
69                      PortletKeys.MESSAGE_BOARDS, "thread-view",
70                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
71              }
72  
73              if (!ArrayUtil.contains(
74                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
75  
76                  threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
77  
78                  preferences.setValue(
79                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
80              }
81  
82              boolean includePrevAndNext =
83                  PropsValues.
84                      MESSAGE_BOARDS_THREAD_PREVIOUS_AND_NEXT_NAVIGATION_ENABLED;
85  
86              MBMessageDisplay messageDisplay =
87                  MBMessageServiceUtil.getMessageDisplay(
88                      messageId, WorkflowConstants.STATUS_ANY, threadView,
89                      includePrevAndNext);
90  
91              renderRequest.setAttribute(
92                  WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
93  
94              return mapping.findForward("portlet.message_boards.view_message");
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchMessageException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(renderRequest, e.getClass().getName());
101 
102                 return mapping.findForward("portlet.message_boards.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108     }
109 
110 }