1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.ArrayUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portal.util.PropsValues;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.PortalPreferences;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  import com.liferay.portlet.messageboards.NoSuchMessageException;
34  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
35  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
36  
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="ViewMessageAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class ViewMessageAction extends PortletAction {
52  
53      public ActionForward render(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              RenderRequest renderRequest, RenderResponse renderResponse)
56          throws Exception {
57  
58          try {
59              long messageId = ParamUtil.getLong(renderRequest, "messageId");
60  
61              PortalPreferences preferences =
62                  PortletPreferencesFactoryUtil.getPortalPreferences(
63                      renderRequest);
64  
65              String threadView = ParamUtil.getString(
66                  renderRequest, "threadView");
67  
68              if (Validator.isNotNull(threadView)) {
69                  preferences.setValue(
70                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
71              }
72              else {
73                  threadView = preferences.getValue(
74                      PortletKeys.MESSAGE_BOARDS, "thread-view",
75                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT);
76              }
77  
78              if (!ArrayUtil.contains(
79                      PropsValues.MESSAGE_BOARDS_THREAD_VIEWS, threadView)) {
80  
81                  threadView = PropsValues.MESSAGE_BOARDS_THREAD_VIEWS_DEFAULT;
82  
83                  preferences.setValue(
84                      PortletKeys.MESSAGE_BOARDS, "thread-view", threadView);
85              }
86  
87              MBMessageDisplay messageDisplay =
88                  MBMessageServiceUtil.getMessageDisplay(messageId, threadView);
89  
90              renderRequest.setAttribute(
91                  WebKeys.MESSAGE_BOARDS_MESSAGE, messageDisplay);
92  
93              return mapping.findForward("portlet.message_boards.view_message");
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchMessageException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.message_boards.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107     }
108 
109 }