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.NoSuchLayoutException;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.model.Layout;
20  import com.liferay.portal.model.LayoutConstants;
21  import com.liferay.portal.model.LayoutTypePortlet;
22  import com.liferay.portal.service.LayoutLocalServiceUtil;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.PortletURLImpl;
26  import com.liferay.portlet.messageboards.model.MBCategory;
27  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletURL;
32  import javax.portlet.WindowState;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.apache.struts.action.Action;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="FindCategoryAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class FindCategoryAction extends Action {
48  
49      public ActionForward execute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              long plid = ParamUtil.getLong(request, "p_l_id");
56              long categoryId = ParamUtil.getLong(request, "mbCategoryId");
57  
58              plid = getPlid(plid, categoryId);
59  
60              PortletURL portletURL = new PortletURLImpl(
61                  request, PortletKeys.MESSAGE_BOARDS, plid,
62                  PortletRequest.RENDER_PHASE);
63  
64              portletURL.setWindowState(WindowState.NORMAL);
65              portletURL.setPortletMode(PortletMode.VIEW);
66  
67              portletURL.setParameter(
68                  "struts_action", "/message_boards/view");
69              portletURL.setParameter("mbCategoryId", String.valueOf(categoryId));
70  
71              response.sendRedirect(portletURL.toString());
72  
73              return null;
74          }
75          catch (Exception e) {
76              PortalUtil.sendError(e, request, response);
77  
78              return null;
79          }
80      }
81  
82      protected long getPlid(long plid, long categoryId) throws Exception {
83          if (plid != LayoutConstants.DEFAULT_PLID) {
84              try {
85                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
86  
87                  LayoutTypePortlet layoutTypePortlet =
88                      (LayoutTypePortlet)layout.getLayoutType();
89  
90                  if (layoutTypePortlet.hasPortletId(
91                          PortletKeys.MESSAGE_BOARDS)) {
92  
93                      return plid;
94                  }
95              }
96              catch (NoSuchLayoutException nsle) {
97              }
98          }
99  
100         MBCategory category = MBCategoryLocalServiceUtil.getCategory(
101             categoryId);
102 
103         plid = PortalUtil.getPlidFromPortletId(
104             category.getGroupId(), PortletKeys.MESSAGE_BOARDS);
105 
106         if (plid != LayoutConstants.DEFAULT_PLID) {
107             return plid;
108         }
109         else {
110             throw new NoSuchLayoutException(
111                 "No page was found with the Message Boards portlet.");
112         }
113     }
114 
115 }