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.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.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.ActionRequestImpl;
35  import com.liferay.portlet.PortletURLImpl;
36  import com.liferay.portlet.tags.TagsEntryException;
37  import com.liferay.portlet.wiki.NoSuchNodeException;
38  import com.liferay.portlet.wiki.NoSuchPageException;
39  import com.liferay.portlet.wiki.PageContentException;
40  import com.liferay.portlet.wiki.PageTitleException;
41  import com.liferay.portlet.wiki.PageVersionException;
42  import com.liferay.portlet.wiki.model.WikiNode;
43  import com.liferay.portlet.wiki.model.WikiPage;
44  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
45  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
46  import com.liferay.util.servlet.SessionErrors;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.PortletPreferences;
52  import javax.portlet.PortletRequest;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  import javax.portlet.WindowState;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="EditPageAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   * @author Jorge Ferrer
66   *
67   */
68  public class EditPageAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig config,
72              ActionRequest req, ActionResponse res)
73          throws Exception {
74  
75          String cmd = ParamUtil.getString(req, Constants.CMD);
76  
77          WikiPage page = null;
78  
79          try {
80              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
81                  page = updatePage(req);
82              }
83              else if (cmd.equals(Constants.DELETE)) {
84                  deletePage(req);
85              }
86              else if (cmd.equals(Constants.REVERT)) {
87                  revertPage(req);
88              }
89              else if (cmd.equals(Constants.SUBSCRIBE)) {
90                  subscribePage(req);
91              }
92              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
93                  unsubscribePage(req);
94              }
95  
96              if (Validator.isNotNull(cmd)) {
97                  String redirect = ParamUtil.getString(req, "redirect");
98  
99                  if (page != null) {
100                     boolean saveAndContinue = ParamUtil.getBoolean(
101                         req, "saveAndContinue");
102 
103                     if (saveAndContinue) {
104                         redirect = getSaveAndContinueRedirect(
105                             config, req, page, redirect);
106                     }
107                     else if (redirect.endsWith("title=")) {
108                         redirect += page.getTitle();
109                     }
110                 }
111 
112                 sendRedirect(req, res, redirect);
113             }
114         }
115         catch (Exception e) {
116             if (e instanceof NoSuchNodeException ||
117                 e instanceof NoSuchPageException ||
118                 e instanceof PrincipalException) {
119 
120                 SessionErrors.add(req, e.getClass().getName());
121 
122                 setForward(req, "portlet.wiki.error");
123             }
124             else if (e instanceof PageContentException ||
125                      e instanceof PageVersionException ||
126                      e instanceof PageTitleException) {
127 
128                 SessionErrors.add(req, e.getClass().getName());
129             }
130             else if (e instanceof TagsEntryException) {
131                 SessionErrors.add(req, e.getClass().getName(), e);
132             }
133             else {
134                 throw e;
135             }
136         }
137     }
138 
139     public ActionForward render(
140             ActionMapping mapping, ActionForm form, PortletConfig config,
141             RenderRequest req, RenderResponse res)
142         throws Exception {
143 
144         try {
145             ActionUtil.getNode(req);
146             getPage(req);
147         }
148         catch (Exception e) {
149             if (e instanceof NoSuchNodeException ||
150                 e instanceof PageTitleException ||
151                 e instanceof PrincipalException) {
152 
153                 SessionErrors.add(req, e.getClass().getName());
154 
155                 return mapping.findForward("portlet.wiki.error");
156             }
157             else if (e instanceof NoSuchPageException) {
158 
159                 // Let edit_page.jsp handle this case
160 
161             }
162             else {
163                 throw e;
164             }
165         }
166 
167         return mapping.findForward(getForward(req, "portlet.wiki.edit_page"));
168     }
169 
170     protected void deletePage(ActionRequest req) throws Exception {
171         long nodeId = ParamUtil.getLong(req, "nodeId");
172         String title = ParamUtil.getString(req, "title");
173 
174         WikiPageServiceUtil.deletePage(nodeId, title);
175     }
176 
177     protected void getPage(RenderRequest req) throws Exception {
178         long nodeId = ParamUtil.getLong(req, "nodeId");
179         String title = ParamUtil.getString(req, "title");
180         double version = ParamUtil.getDouble(req, "version");
181 
182         if (nodeId == 0) {
183             WikiNode node = (WikiNode)req.getAttribute(WebKeys.WIKI_NODE);
184 
185             if (node != null) {
186                 nodeId = node.getNodeId();
187             }
188         }
189 
190         WikiPage page = null;
191 
192         if (Validator.isNotNull(title)) {
193             try {
194                 page = WikiPageServiceUtil.getPage(nodeId, title, version);
195             }
196             catch (NoSuchPageException nspe) {
197                 if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
198                     page = WikiPageServiceUtil.addPage(
199                         nodeId, title, null, null);
200                 }
201                 else {
202                     throw nspe;
203                 }
204             }
205         }
206 
207         req.setAttribute(WebKeys.WIKI_PAGE, page);
208     }
209 
210     protected String getSaveAndContinueRedirect(
211             PortletConfig config, ActionRequest req, WikiPage page,
212             String redirect)
213         throws Exception {
214 
215         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
216             WebKeys.THEME_DISPLAY);
217 
218         Layout layout = themeDisplay.getLayout();
219 
220         String originalRedirect = ParamUtil.getString(req, "originalRedirect");
221 
222         PortletURLImpl portletURL = new PortletURLImpl(
223             (ActionRequestImpl)req, config.getPortletName(),
224             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
225 
226         portletURL.setWindowState(WindowState.MAXIMIZED);
227 
228         portletURL.setParameter("struts_action", "/wiki/edit_page");
229         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
230         portletURL.setParameter("redirect", redirect, false);
231         portletURL.setParameter("originalRedirect", originalRedirect, false);
232         portletURL.setParameter(
233             "groupId", String.valueOf(layout.getGroupId()), false);
234         portletURL.setParameter(
235             "nodeId", String.valueOf(page.getNodeId()), false);
236         portletURL.setParameter("title", page.getTitle(), false);
237 
238         return portletURL.toString();
239     }
240 
241     protected void revertPage(ActionRequest req) throws Exception {
242         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
243             WebKeys.THEME_DISPLAY);
244 
245         PortletPreferences prefs = req.getPreferences();
246 
247         long nodeId = ParamUtil.getLong(req, "nodeId");
248         String title = ParamUtil.getString(req, "title");
249         double version = ParamUtil.getDouble(req, "version");
250 
251         WikiPageServiceUtil.revertPage(
252             nodeId, title, version, prefs, themeDisplay);
253     }
254 
255     protected void subscribePage(ActionRequest req) throws Exception {
256         long nodeId = ParamUtil.getLong(req, "nodeId");
257         String title = ParamUtil.getString(req, "title");
258 
259         WikiPageServiceUtil.subscribePage(nodeId, title);
260     }
261 
262     protected void unsubscribePage(ActionRequest req) throws Exception {
263         long nodeId = ParamUtil.getLong(req, "nodeId");
264         String title = ParamUtil.getString(req, "title");
265 
266         WikiPageServiceUtil.unsubscribePage(nodeId, title);
267     }
268 
269     protected WikiPage updatePage(ActionRequest req) throws Exception {
270         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
271             WebKeys.THEME_DISPLAY);
272 
273         PortletPreferences prefs = req.getPreferences();
274 
275         long nodeId = ParamUtil.getLong(req, "nodeId");
276         String title = ParamUtil.getString(req, "title");
277         double version = ParamUtil.getDouble(req, "version");
278 
279         String content = ParamUtil.getString(req, "content");
280         String format = ParamUtil.getString(req, "format");
281         String parentTitle = ParamUtil.getString(req, "parentTitle");
282         String redirectTitle = null;
283 
284         String[] tagsEntries = StringUtil.split(
285             ParamUtil.getString(req, "tagsEntries"));
286 
287         return WikiPageServiceUtil.updatePage(
288             nodeId, title, version, content, format, parentTitle, redirectTitle,
289             tagsEntries, prefs, themeDisplay);
290     }
291 
292     protected boolean isCheckMethodOnProcessAction() {
293         return _CHECK_METHOD_ON_PROCESS_ACTION;
294     }
295 
296     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
297 
298 }