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.wiki.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.wiki.model.WikiNode;
27  import com.liferay.portlet.wiki.model.WikiPageResource;
28  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
29  import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
30  
31  import javax.portlet.PortletMode;
32  import javax.portlet.PortletRequest;
33  import javax.portlet.PortletURL;
34  import javax.portlet.WindowState;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.struts.action.Action;
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="FindPageAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Samuel Kong
48   */
49  public class FindPageAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          try {
57              long plid = ParamUtil.getLong(request, "p_l_id");
58              long pageResourcePrimKey = ParamUtil.getLong(
59                  request, "pageResourcePrimKey");
60  
61              plid = getPlid(plid, pageResourcePrimKey);
62  
63              WikiPageResource pageResource =
64                  WikiPageResourceLocalServiceUtil.getPageResource(
65                      pageResourcePrimKey);
66  
67              WikiNode node = WikiNodeLocalServiceUtil.getNode(
68                  pageResource.getNodeId());
69  
70              PortletURL portletURL = new PortletURLImpl(
71                  request, PortletKeys.WIKI, plid, PortletRequest.RENDER_PHASE);
72  
73              portletURL.setWindowState(WindowState.NORMAL);
74              portletURL.setPortletMode(PortletMode.VIEW);
75  
76              portletURL.setParameter("struts_action", "/wiki/view");
77              portletURL.setParameter("nodeName", node.getName());
78              portletURL.setParameter("title", pageResource.getTitle());
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 pageResourcePrimKey)
92          throws Exception {
93  
94          if (plid != LayoutConstants.DEFAULT_PLID) {
95              try {
96                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
97  
98                  LayoutTypePortlet layoutTypePortlet =
99                      (LayoutTypePortlet)layout.getLayoutType();
100 
101                 if (layoutTypePortlet.hasPortletId(PortletKeys.WIKI)) {
102                     return plid;
103                 }
104             }
105             catch (NoSuchLayoutException nsle) {
106             }
107         }
108 
109         WikiPageResource pageResource =
110             WikiPageResourceLocalServiceUtil.getPageResource(
111                 pageResourcePrimKey);
112 
113         WikiNode node = WikiNodeLocalServiceUtil.getNode(
114             pageResource.getNodeId());
115 
116         plid = PortalUtil.getPlidFromPortletId(
117             node.getGroupId(), PortletKeys.WIKI);
118 
119         if (plid != LayoutConstants.DEFAULT_PLID) {
120             return plid;
121         }
122         else {
123             throw new NoSuchLayoutException(
124                 "No page was found with the Wiki portlet.");
125         }
126     }
127 
128 }