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