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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.wiki.NoSuchNodeException;
34  import com.liferay.portlet.wiki.NoSuchPageException;
35  import com.liferay.portlet.wiki.model.WikiNode;
36  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
37  import com.liferay.util.servlet.SessionErrors;
38  
39  import java.util.List;
40  
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
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="ViewPageAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Jorge Ferrer
54   *
55   */
56  public class ViewPageAction extends PortletAction {
57  
58      public ActionForward render(
59              ActionMapping mapping, ActionForm form, PortletConfig config,
60              RenderRequest req, RenderResponse res)
61          throws Exception {
62  
63          try {
64              ActionUtil.getNode(req);
65  
66              checkNode(req);
67  
68              ActionUtil.getPage(req);
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchNodeException ||
72                  e instanceof NoSuchPageException ||
73                  e instanceof PrincipalException) {
74  
75                  SessionErrors.add(req, e.getClass().getName());
76  
77                  return mapping.findForward("portlet.wiki.error");
78              }
79              else {
80                  throw e;
81              }
82          }
83  
84          return mapping.findForward(getForward(req, "portlet.wiki.view_page"));
85      }
86  
87      private void checkNode(RenderRequest req)
88          throws PortalException, SystemException {
89  
90          WikiNode node = (WikiNode)req.getAttribute(WebKeys.WIKI_NODE);
91  
92          if (node == null) {
93              ThemeDisplay themeDisplay =
94                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
95  
96              List nodes = WikiNodeLocalServiceUtil.getNodes(
97                  themeDisplay.getLayout().getGroupId(), 0, 1);
98  
99              if (nodes.size() == 0) {
100                 String nodeName = PropsUtil.get(
101                     PropsUtil.WIKI_INITIAL_NODE_NAME);
102 
103                 node = WikiNodeLocalServiceUtil.addNode(
104                     themeDisplay.getUserId(), themeDisplay.getPlid(), nodeName,
105                     StringPool.BLANK, true, true);
106             }
107             else {
108                 node = (WikiNode)nodes.get(0);
109             }
110 
111             req.setAttribute(WebKeys.WIKI_NODE, node);
112         }
113     }
114 
115 }