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