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.StatusConstants;
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              MBMessageDisplay messageDisplay =
83                  MBMessageServiceUtil.getMessageDisplay(
84                      messageId, StatusConstants.APPROVED, threadView);
85  
86              renderRequest.setAttribute(
87                  WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
88  
89              return mapping.findForward("portlet.message_boards.view_message");
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchMessageException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(renderRequest, e.getClass().getName());
96  
97                  return mapping.findForward("portlet.message_boards.error");
98              }
99              else {
100                 throw e;
101             }
102         }
103     }
104 
105 }