1   /**
2    * Copyright (c) 2000-2007 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.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portlet.tags.TagsEntryException;
32  import com.liferay.portlet.wiki.NoSuchNodeException;
33  import com.liferay.portlet.wiki.NoSuchPageException;
34  import com.liferay.portlet.wiki.PageContentException;
35  import com.liferay.portlet.wiki.PageTitleException;
36  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
37  import com.liferay.util.servlet.SessionErrors;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
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="EditPageAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class EditPageAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig config,
59              ActionRequest req, ActionResponse res)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(req, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updatePage(req);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deletePage(req);
70              }
71              else if (cmd.equals(Constants.REVERT)) {
72                  revertPage(req);
73              }
74  
75              if (Validator.isNotNull(cmd)) {
76                  sendRedirect(req, res);
77              }
78          }
79          catch (Exception e) {
80              if (e instanceof NoSuchNodeException ||
81                  e instanceof NoSuchPageException ||
82                  e instanceof PrincipalException) {
83  
84                  SessionErrors.add(req, e.getClass().getName());
85  
86                  setForward(req, "portlet.wiki.error");
87              }
88              else if (e instanceof PageContentException ||
89                       e instanceof PageTitleException) {
90  
91                  SessionErrors.add(req, e.getClass().getName());
92              }
93              else if (e instanceof TagsEntryException) {
94                  SessionErrors.add(req, e.getClass().getName(), e);
95              }
96              else {
97                  throw e;
98              }
99          }
100     }
101 
102     public ActionForward render(
103             ActionMapping mapping, ActionForm form, PortletConfig config,
104             RenderRequest req, RenderResponse res)
105         throws Exception {
106 
107         try {
108             ActionUtil.getNode(req);
109             ActionUtil.getPage(req);
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchNodeException ||
113                 e instanceof NoSuchPageException ||
114                 e instanceof PrincipalException) {
115 
116                 SessionErrors.add(req, e.getClass().getName());
117 
118                 return mapping.findForward("portlet.wiki.error");
119             }
120             else {
121                 throw e;
122             }
123         }
124 
125         return mapping.findForward(getForward(req, "portlet.wiki.edit_page"));
126     }
127 
128     protected void deletePage(ActionRequest req) throws Exception {
129         long nodeId = ParamUtil.getLong(req, "nodeId");
130         String title = ParamUtil.getString(req, "title");
131 
132         WikiPageServiceUtil.deletePage(nodeId, title);
133     }
134 
135     protected void revertPage(ActionRequest req) throws Exception {
136         long nodeId = ParamUtil.getLong(req, "nodeId");
137         String title = ParamUtil.getString(req, "title");
138         double version = ParamUtil.getDouble(req, "version");
139 
140         WikiPageServiceUtil.revertPage(nodeId, title, version);
141     }
142 
143     protected void updatePage(ActionRequest req) throws Exception {
144         long nodeId = ParamUtil.getLong(req, "nodeId");
145         String title = ParamUtil.getString(req, "title");
146 
147         String content = ParamUtil.getString(req, "content");
148         String format = ParamUtil.getString(req, "format");
149 
150         String[] tagsEntries = StringUtil.split(
151             ParamUtil.getString(req, "tagsEntries"));
152 
153         WikiPageServiceUtil.updatePage(
154             nodeId, title, content, format, tagsEntries);
155     }
156 
157 }