1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portal.util.PropsValues;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.PortalPreferences;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  import com.liferay.portlet.messageboards.NoSuchMessageException;
29  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
30  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
31  
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="ViewMessageAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class ViewMessageAction extends PortletAction {
46  
47      public ActionForward render(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              RenderRequest renderRequest, RenderResponse renderResponse)
50          throws Exception {
51  
52          try {
53              long messageId = ParamUtil.getLong(renderRequest, "messageId");
54  
55              PortalPreferences preferences =
56                  PortletPreferencesFactoryUtil.getPortalPreferences(
57                      renderRequest);
58  
59              String threadView = ParamUtil.getString(
60                  renderRequest, "threadView");
61  
62              if (Validator.isNotNull(threadView)) {
63                  preferences.setValue(
64                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
65              }
66              else {
67                  threadView = preferences.getValue(
68                      PortletKeys.MESSAGE_BOARDS, "thread-view",
69                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
70              }
71  
72              if (!ArrayUtil.contains(
73                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
74  
75                  threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
76  
77                  preferences.setValue(
78                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
79              }
80  
81              MBMessageDisplay messageDisplay =
82                  MBMessageServiceUtil.getMessageDisplay(messageId, threadView);
83  
84              renderRequest.setAttribute(
85                  WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
86  
87              return mapping.findForward("portlet.message_boards.view_message");
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchMessageException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(renderRequest, e.getClass().getName());
94  
95                  return mapping.findForward("portlet.message_boards.error");
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103 }