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