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