001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.struts.StrutsActionPortletURL;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.PortletResponseImpl;
032    import com.liferay.portlet.PortletURLImpl;
033    import com.liferay.portlet.asset.AssetTagException;
034    import com.liferay.portlet.wiki.DuplicatePageException;
035    import com.liferay.portlet.wiki.NoSuchNodeException;
036    import com.liferay.portlet.wiki.NoSuchPageException;
037    import com.liferay.portlet.wiki.PageContentException;
038    import com.liferay.portlet.wiki.PageTitleException;
039    import com.liferay.portlet.wiki.PageVersionException;
040    import com.liferay.portlet.wiki.model.WikiNode;
041    import com.liferay.portlet.wiki.model.WikiPage;
042    import com.liferay.portlet.wiki.model.WikiPageConstants;
043    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.PortletRequest;
049    import javax.portlet.RenderRequest;
050    import javax.portlet.RenderResponse;
051    
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Jorge Ferrer
059     */
060    public class EditPageAction extends PortletAction {
061    
062            public void processAction(
063                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    WikiPage page = null;
070    
071                    try {
072                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
073                                    page = updatePage(actionRequest);
074                            }
075                            else if (cmd.equals(Constants.DELETE)) {
076                                    deletePage(actionRequest);
077                            }
078                            else if (cmd.equals(Constants.REVERT)) {
079                                    revertPage(actionRequest);
080                            }
081                            else if (cmd.equals(Constants.SUBSCRIBE)) {
082                                    subscribePage(actionRequest);
083                            }
084                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
085                                    unsubscribePage(actionRequest);
086                            }
087    
088                            if (Validator.isNotNull(cmd)) {
089                                    String redirect = ParamUtil.getString(
090                                            actionRequest, "redirect");
091    
092                                    int workflowAction = ParamUtil.getInteger(
093                                            actionRequest, "workflowAction",
094                                            WorkflowConstants.ACTION_PUBLISH);
095    
096                                    if (page != null) {
097                                            if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
098                                                    redirect = getSaveAndContinueRedirect(
099                                                            actionRequest, actionResponse, page, redirect);
100                                            }
101                                            else if (redirect.endsWith("title=")) {
102                                                    redirect += page.getTitle();
103                                            }
104                                    }
105    
106                                    sendRedirect(actionRequest, actionResponse, redirect);
107                            }
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchNodeException ||
111                                    e instanceof NoSuchPageException ||
112                                    e instanceof PrincipalException) {
113    
114                                    SessionErrors.add(actionRequest, e.getClass().getName());
115    
116                                    setForward(actionRequest, "portlet.wiki.error");
117                            }
118                            else if (e instanceof DuplicatePageException ||
119                                             e instanceof PageContentException ||
120                                             e instanceof PageVersionException ||
121                                             e instanceof PageTitleException) {
122    
123                                    SessionErrors.add(actionRequest, e.getClass().getName());
124                            }
125                            else if (e instanceof AssetTagException) {
126                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
127                            }
128                            else {
129                                    throw e;
130                            }
131                    }
132            }
133    
134            public ActionForward render(
135                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136                            RenderRequest renderRequest, RenderResponse renderResponse)
137                    throws Exception {
138    
139                    try {
140                            ActionUtil.getNode(renderRequest);
141    
142                            if (!SessionErrors.contains(
143                                            renderRequest, DuplicatePageException.class.getName())) {
144    
145                                    getPage(renderRequest);
146                            }
147                    }
148                    catch (Exception e) {
149                            if (e instanceof NoSuchNodeException ||
150                                    e instanceof PageTitleException ||
151                                    e instanceof PrincipalException) {
152    
153                                    SessionErrors.add(renderRequest, 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(
168                            getForward(renderRequest, "portlet.wiki.edit_page"));
169            }
170    
171            protected void deletePage(ActionRequest actionRequest) throws Exception {
172                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
173                    String title = ParamUtil.getString(actionRequest, "title");
174                    double version = ParamUtil.getDouble(actionRequest, "version");
175    
176                    if (version > 0) {
177                            WikiPageServiceUtil.deletePage(nodeId, title, version);
178                    }
179                    else {
180                            WikiPageServiceUtil.deletePage(nodeId, title);
181                    }
182            }
183    
184            protected void getPage(RenderRequest renderRequest) throws Exception {
185                    long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
186                    String title = ParamUtil.getString(renderRequest, "title");
187                    double version = ParamUtil.getDouble(renderRequest, "version");
188                    boolean removeRedirect = ParamUtil.getBoolean(
189                            renderRequest, "removeRedirect");
190    
191                    if (nodeId == 0) {
192                            WikiNode node = (WikiNode)renderRequest.getAttribute(
193                                    WebKeys.WIKI_NODE);
194    
195                            if (node != null) {
196                                    nodeId = node.getNodeId();
197                            }
198                    }
199    
200                    WikiPage page = null;
201    
202                    if (Validator.isNotNull(title)) {
203                            try {
204                                    if (version == 0) {
205                                            page = WikiPageServiceUtil.getPage(nodeId, title, null);
206                                    }
207                                    else {
208                                            page = WikiPageServiceUtil.getPage(nodeId, title, version);
209                                    }
210                            }
211                            catch (NoSuchPageException nspe1) {
212                                    try {
213                                            page = WikiPageServiceUtil.getPage(
214                                                    nodeId, title, false);
215                                    }
216                                    catch (NoSuchPageException nspe2) {
217                                            if ((title.equals(WikiPageConstants.FRONT_PAGE)) &&
218                                                    (version == 0)) {
219    
220                                                    ServiceContext serviceContext = new ServiceContext();
221    
222                                                    page = WikiPageServiceUtil.addPage(
223                                                            nodeId, title, null, WikiPageConstants.NEW, true,
224                                                            serviceContext);
225                                            }
226                                            else {
227                                                    throw nspe2;
228                                            }
229                                    }
230                            }
231    
232                            if (removeRedirect) {
233                                    page.setContent(StringPool.BLANK);
234                                    page.setRedirectTitle(StringPool.BLANK);
235                            }
236                    }
237    
238                    renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
239            }
240    
241            protected String getSaveAndContinueRedirect(
242                            ActionRequest actionRequest, ActionResponse actionResponse,
243                            WikiPage page, String redirect)
244                    throws Exception {
245    
246                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
247                            WebKeys.THEME_DISPLAY);
248    
249                    Layout layout = themeDisplay.getLayout();
250    
251                    String originalRedirect = ParamUtil.getString(
252                            actionRequest, "originalRedirect");
253    
254                    PortletURLImpl portletURL = new StrutsActionPortletURL(
255                            (PortletResponseImpl)actionResponse, themeDisplay.getPlid(),
256                            PortletRequest.RENDER_PHASE);
257    
258                    portletURL.setParameter("struts_action", "/wiki/edit_page");
259                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
260                    portletURL.setParameter("redirect", redirect, false);
261                    portletURL.setParameter("originalRedirect", originalRedirect, false);
262                    portletURL.setParameter(
263                            "groupId", String.valueOf(layout.getGroupId()), false);
264                    portletURL.setParameter(
265                            "nodeId", String.valueOf(page.getNodeId()), false);
266                    portletURL.setParameter("title", page.getTitle(), false);
267    
268                    return portletURL.toString();
269            }
270    
271            protected void revertPage(ActionRequest actionRequest) throws Exception {
272                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
273                    String title = ParamUtil.getString(actionRequest, "title");
274                    double version = ParamUtil.getDouble(actionRequest, "version");
275    
276                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
277                            WikiPage.class.getName(), actionRequest);
278    
279                    WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
280            }
281    
282            protected void subscribePage(ActionRequest actionRequest) throws Exception {
283                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
284                    String title = ParamUtil.getString(actionRequest, "title");
285    
286                    WikiPageServiceUtil.subscribePage(nodeId, title);
287            }
288    
289            protected void unsubscribePage(ActionRequest actionRequest)
290                    throws Exception {
291    
292                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
293                    String title = ParamUtil.getString(actionRequest, "title");
294    
295                    WikiPageServiceUtil.unsubscribePage(nodeId, title);
296            }
297    
298            protected WikiPage updatePage(ActionRequest actionRequest)
299                    throws Exception {
300    
301                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
302    
303                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
304                    String title = ParamUtil.getString(actionRequest, "title");
305                    double version = ParamUtil.getDouble(actionRequest, "version");
306    
307                    String content = ParamUtil.getString(actionRequest, "content");
308                    String summary = ParamUtil.getString(actionRequest, "summary");
309                    boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
310                    String format = ParamUtil.getString(actionRequest, "format");
311                    String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
312                    String redirectTitle = null;
313    
314                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
315                            WikiPage.class.getName(), actionRequest);
316    
317                    WikiPage page = null;
318    
319                    if (cmd.equals(Constants.ADD)) {
320                            page = WikiPageServiceUtil.addPage(
321                                    nodeId, title, content, summary, minorEdit, format, parentTitle,
322                                    redirectTitle, serviceContext);
323                    }
324                    else {
325                            page = WikiPageServiceUtil.updatePage(
326                                    nodeId, title, version, content, summary, minorEdit, format,
327                                    parentTitle, redirectTitle, serviceContext);
328                    }
329    
330                    return page;
331            }
332    
333            protected boolean isCheckMethodOnProcessAction() {
334                    return _CHECK_METHOD_ON_PROCESS_ACTION;
335            }
336    
337            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
338    
339    }