1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.action;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutConstants;
29  import com.liferay.portal.model.LayoutTypePortlet;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.portlet.messageboards.model.MBMessage;
35  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletRequest;
39  import javax.portlet.PortletURL;
40  import javax.portlet.WindowState;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="FindMessageAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class FindMessageAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              long plid = ParamUtil.getLong(request, "p_l_id");
65              long messageId = ParamUtil.getLong(request, "messageId");
66  
67              plid = getPlid(plid, messageId);
68  
69              PortletURL portletURL = new PortletURLImpl(
70                  request, PortletKeys.MESSAGE_BOARDS, plid,
71                  PortletRequest.RENDER_PHASE);
72  
73              portletURL.setWindowState(WindowState.NORMAL);
74              portletURL.setPortletMode(PortletMode.VIEW);
75  
76              portletURL.setParameter(
77                  "struts_action", "/message_boards/view_message");
78              portletURL.setParameter("messageId", String.valueOf(messageId));
79  
80              response.sendRedirect(portletURL.toString());
81  
82              return null;
83          }
84          catch (Exception e) {
85              PortalUtil.sendError(e, request, response);
86  
87              return null;
88          }
89      }
90  
91      protected long getPlid(long plid, long messageId) throws Exception {
92          if (plid != LayoutConstants.DEFAULT_PLID) {
93              try {
94                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
95  
96                  LayoutTypePortlet layoutTypePortlet =
97                      (LayoutTypePortlet)layout.getLayoutType();
98  
99                  if (layoutTypePortlet.hasPortletId(
100                         PortletKeys.MESSAGE_BOARDS)) {
101 
102                     return plid;
103                 }
104             }
105             catch (NoSuchLayoutException nsle) {
106             }
107         }
108 
109         MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
110 
111         plid = PortalUtil.getPlidFromPortletId(
112             message.getCategory().getGroupId(), false,
113             PortletKeys.MESSAGE_BOARDS);
114 
115         if (plid != LayoutConstants.DEFAULT_PLID) {
116             return plid;
117         }
118 
119         plid = PortalUtil.getPlidFromPortletId(
120             message.getCategory().getGroupId(), true,
121             PortletKeys.MESSAGE_BOARDS);
122 
123         if (plid != LayoutConstants.DEFAULT_PLID) {
124             return plid;
125         }
126         else {
127             throw new NoSuchLayoutException(
128                 "No page was found with the Message Boards portlet.");
129         }
130     }
131 
132 }