001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutConstants;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortletURLImpl;
026    import com.liferay.portlet.wiki.model.WikiNode;
027    import com.liferay.portlet.wiki.model.WikiPageResource;
028    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
029    import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
030    
031    import javax.portlet.PortletMode;
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    import javax.portlet.WindowState;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.Action;
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Samuel Kong
046     */
047    public class FindPageAction extends Action {
048    
049            public ActionForward execute(
050                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
051                            HttpServletResponse response)
052                    throws Exception {
053    
054                    try {
055                            long plid = ParamUtil.getLong(request, "p_l_id");
056                            long pageResourcePrimKey = ParamUtil.getLong(
057                                    request, "pageResourcePrimKey");
058    
059                            plid = getPlid(plid, pageResourcePrimKey);
060    
061                            WikiPageResource pageResource =
062                                    WikiPageResourceLocalServiceUtil.getPageResource(
063                                            pageResourcePrimKey);
064    
065                            WikiNode node = WikiNodeLocalServiceUtil.getNode(
066                                    pageResource.getNodeId());
067    
068                            PortletURL portletURL = new PortletURLImpl(
069                                    request, PortletKeys.WIKI, plid, PortletRequest.RENDER_PHASE);
070    
071                            portletURL.setWindowState(WindowState.NORMAL);
072                            portletURL.setPortletMode(PortletMode.VIEW);
073    
074                            portletURL.setParameter("struts_action", "/wiki/view");
075                            portletURL.setParameter("nodeName", node.getName());
076                            portletURL.setParameter("title", pageResource.getTitle());
077    
078                            response.sendRedirect(portletURL.toString());
079    
080                            return null;
081                    }
082                    catch (Exception e) {
083                            PortalUtil.sendError(e, request, response);
084    
085                            return null;
086                    }
087            }
088    
089            protected long getPlid(long plid, long pageResourcePrimKey)
090                    throws Exception {
091    
092                    if (plid != LayoutConstants.DEFAULT_PLID) {
093                            try {
094                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
095    
096                                    LayoutTypePortlet layoutTypePortlet =
097                                            (LayoutTypePortlet)layout.getLayoutType();
098    
099                                    if (layoutTypePortlet.hasPortletId(PortletKeys.WIKI)) {
100                                            return plid;
101                                    }
102                            }
103                            catch (NoSuchLayoutException nsle) {
104                            }
105                    }
106    
107                    WikiPageResource pageResource =
108                            WikiPageResourceLocalServiceUtil.getPageResource(
109                                    pageResourcePrimKey);
110    
111                    WikiNode node = WikiNodeLocalServiceUtil.getNode(
112                            pageResource.getNodeId());
113    
114                    plid = PortalUtil.getPlidFromPortletId(
115                            node.getGroupId(), PortletKeys.WIKI);
116    
117                    if (plid != LayoutConstants.DEFAULT_PLID) {
118                            return plid;
119                    }
120                    else {
121                            throw new NoSuchLayoutException(
122                                    "No page was found with the Wiki portlet.");
123                    }
124            }
125    
126    }