1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class FindPageAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          try {
65              long plid = ParamUtil.getLong(request, "p_l_id");
66              long pageResourcePrimKey = ParamUtil.getLong(
67                  request, "pageResourcePrimKey");
68  
69              plid = getPlid(plid, pageResourcePrimKey);
70  
71              WikiPageResource pageResource =
72                  WikiPageResourceLocalServiceUtil.getPageResource(
73                      pageResourcePrimKey);
74  
75              WikiNode node = WikiNodeLocalServiceUtil.getNode(
76                  pageResource.getNodeId());
77  
78              PortletURL portletURL = new PortletURLImpl(
79                  request, PortletKeys.WIKI, plid, PortletRequest.RENDER_PHASE);
80  
81              portletURL.setWindowState(WindowState.NORMAL);
82              portletURL.setPortletMode(PortletMode.VIEW);
83  
84              portletURL.setParameter("struts_action", "/wiki/view");
85              portletURL.setParameter("nodeName", node.getName());
86              portletURL.setParameter("title", pageResource.getTitle());
87  
88              response.sendRedirect(portletURL.toString());
89  
90              return null;
91          }
92          catch (Exception e) {
93              PortalUtil.sendError(e, request, response);
94  
95              return null;
96          }
97      }
98  
99      protected long getPlid(long plid, long pageResourcePrimKey)
100         throws Exception {
101 
102         if (plid != LayoutConstants.DEFAULT_PLID) {
103             try {
104                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
105 
106                 LayoutTypePortlet layoutTypePortlet =
107                     (LayoutTypePortlet)layout.getLayoutType();
108 
109                 if (layoutTypePortlet.hasPortletId(PortletKeys.WIKI)) {
110                     return plid;
111                 }
112             }
113             catch (NoSuchLayoutException nsle) {
114             }
115         }
116 
117         WikiPageResource pageResource =
118             WikiPageResourceLocalServiceUtil.getPageResource(
119                 pageResourcePrimKey);
120 
121         WikiNode node = WikiNodeLocalServiceUtil.getNode(
122             pageResource.getNodeId());
123 
124         plid = PortalUtil.getPlidFromPortletId(
125             node.getGroupId(), PortletKeys.WIKI);
126 
127         if (plid != LayoutConstants.DEFAULT_PLID) {
128             return plid;
129         }
130         else {
131             throw new NoSuchLayoutException(
132                 "No page was found with the Wiki portlet.");
133         }
134     }
135 
136 }