001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.AssetCategoryException;
034    import com.liferay.portlet.asset.AssetTagException;
035    import com.liferay.portlet.wiki.DuplicatePageException;
036    import com.liferay.portlet.wiki.NoSuchNodeException;
037    import com.liferay.portlet.wiki.NoSuchPageException;
038    import com.liferay.portlet.wiki.PageContentException;
039    import com.liferay.portlet.wiki.PageTitleException;
040    import com.liferay.portlet.wiki.PageVersionException;
041    import com.liferay.portlet.wiki.model.WikiNode;
042    import com.liferay.portlet.wiki.model.WikiPage;
043    import com.liferay.portlet.wiki.model.WikiPageConstants;
044    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    import javax.portlet.PortletConfig;
049    import javax.portlet.PortletRequest;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     * @author Jorge Ferrer
060     */
061    public class EditPageAction extends PortletAction {
062    
063            @Override
064            public void processAction(
065                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
066                            ActionRequest actionRequest, ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    WikiPage page = null;
072    
073                    try {
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    page = updatePage(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deletePage(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.REVERT)) {
081                                    revertPage(actionRequest);
082                            }
083                            else if (cmd.equals(Constants.SUBSCRIBE)) {
084                                    subscribePage(actionRequest);
085                            }
086                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
087                                    unsubscribePage(actionRequest);
088                            }
089    
090                            if (Validator.isNotNull(cmd)) {
091                                    String redirect = ParamUtil.getString(
092                                            actionRequest, "redirect");
093    
094                                    int workflowAction = ParamUtil.getInteger(
095                                            actionRequest, "workflowAction",
096                                            WorkflowConstants.ACTION_PUBLISH);
097    
098                                    if (page != null) {
099                                            if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
100                                                    redirect = getSaveAndContinueRedirect(
101                                                            actionRequest, actionResponse, page, redirect);
102                                            }
103                                            else if (redirect.endsWith("title=")) {
104                                                    redirect += page.getTitle();
105                                            }
106                                    }
107    
108                                    sendRedirect(actionRequest, actionResponse, redirect);
109                            }
110                    }
111                    catch (Exception e) {
112                            if (e instanceof NoSuchNodeException ||
113                                    e instanceof NoSuchPageException ||
114                                    e instanceof PrincipalException) {
115    
116                                    SessionErrors.add(actionRequest, e.getClass().getName());
117    
118                                    setForward(actionRequest, "portlet.wiki.error");
119                            }
120                            else if (e instanceof DuplicatePageException ||
121                                             e instanceof PageContentException ||
122                                             e instanceof PageVersionException ||
123                                             e instanceof PageTitleException) {
124    
125                                    SessionErrors.add(actionRequest, e.getClass().getName());
126                            }
127                            else if (e instanceof AssetCategoryException ||
128                                             e instanceof AssetTagException) {
129    
130                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
131                            }
132                            else {
133                                    throw e;
134                            }
135                    }
136            }
137    
138            @Override
139            public ActionForward render(
140                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141                            RenderRequest renderRequest, RenderResponse renderResponse)
142                    throws Exception {
143    
144                    try {
145                            ActionUtil.getNode(renderRequest);
146    
147                            if (!SessionErrors.contains(
148                                            renderRequest, DuplicatePageException.class.getName())) {
149    
150                                    getPage(renderRequest);
151                            }
152                    }
153                    catch (Exception e) {
154                            if (e instanceof NoSuchNodeException ||
155                                    e instanceof PageTitleException ||
156                                    e instanceof PrincipalException) {
157    
158                                    SessionErrors.add(renderRequest, e.getClass().getName());
159    
160                                    return mapping.findForward("portlet.wiki.error");
161                            }
162                            else if (e instanceof NoSuchPageException) {
163    
164                                    // Let edit_page.jsp handle this case
165    
166                            }
167                            else {
168                                    throw e;
169                            }
170                    }
171    
172                    return mapping.findForward(
173                            getForward(renderRequest, "portlet.wiki.edit_page"));
174            }
175    
176            protected void deletePage(ActionRequest actionRequest) throws Exception {
177                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
178                    String title = ParamUtil.getString(actionRequest, "title");
179                    double version = ParamUtil.getDouble(actionRequest, "version");
180    
181                    if (version > 0) {
182                            WikiPageServiceUtil.deletePage(nodeId, title, version);
183                    }
184                    else {
185                            WikiPageServiceUtil.deletePage(nodeId, title);
186                    }
187            }
188    
189            protected void getPage(RenderRequest renderRequest) throws Exception {
190                    long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
191                    String title = ParamUtil.getString(renderRequest, "title");
192                    double version = ParamUtil.getDouble(renderRequest, "version");
193                    boolean removeRedirect = ParamUtil.getBoolean(
194                            renderRequest, "removeRedirect");
195    
196                    if (nodeId == 0) {
197                            WikiNode node = (WikiNode)renderRequest.getAttribute(
198                                    WebKeys.WIKI_NODE);
199    
200                            if (node != null) {
201                                    nodeId = node.getNodeId();
202                            }
203                    }
204    
205                    WikiPage page = null;
206    
207                    if (Validator.isNotNull(title)) {
208                            try {
209                                    if (version == 0) {
210                                            page = WikiPageServiceUtil.getPage(nodeId, title, null);
211                                    }
212                                    else {
213                                            page = WikiPageServiceUtil.getPage(nodeId, title, version);
214                                    }
215                            }
216                            catch (NoSuchPageException nspe1) {
217                                    try {
218                                            page = WikiPageServiceUtil.getPage(
219                                                    nodeId, title, false);
220                                    }
221                                    catch (NoSuchPageException nspe2) {
222                                            if ((title.equals(WikiPageConstants.FRONT_PAGE)) &&
223                                                    (version == 0)) {
224    
225                                                    ServiceContext serviceContext = new ServiceContext();
226    
227                                                    page = WikiPageServiceUtil.addPage(
228                                                            nodeId, title, null, WikiPageConstants.NEW, true,
229                                                            serviceContext);
230                                            }
231                                            else {
232                                                    throw nspe2;
233                                            }
234                                    }
235                            }
236    
237                            if (removeRedirect) {
238                                    page.setContent(StringPool.BLANK);
239                                    page.setRedirectTitle(StringPool.BLANK);
240                            }
241                    }
242    
243                    renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
244            }
245    
246            protected String getSaveAndContinueRedirect(
247                            ActionRequest actionRequest, ActionResponse actionResponse,
248                            WikiPage page, String redirect)
249                    throws Exception {
250    
251                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
252                            WebKeys.THEME_DISPLAY);
253    
254                    Layout layout = themeDisplay.getLayout();
255    
256                    String originalRedirect = ParamUtil.getString(
257                            actionRequest, "originalRedirect");
258    
259                    PortletURLImpl portletURL = new StrutsActionPortletURL(
260                            (PortletResponseImpl)actionResponse, themeDisplay.getPlid(),
261                            PortletRequest.RENDER_PHASE);
262    
263                    portletURL.setParameter("struts_action", "/wiki/edit_page");
264                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
265                    portletURL.setParameter("redirect", redirect, false);
266                    portletURL.setParameter("originalRedirect", originalRedirect, false);
267                    portletURL.setParameter(
268                            "groupId", String.valueOf(layout.getGroupId()), false);
269                    portletURL.setParameter(
270                            "nodeId", String.valueOf(page.getNodeId()), false);
271                    portletURL.setParameter("title", page.getTitle(), false);
272    
273                    return portletURL.toString();
274            }
275    
276            protected void revertPage(ActionRequest actionRequest) throws Exception {
277                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
278                    String title = ParamUtil.getString(actionRequest, "title");
279                    double version = ParamUtil.getDouble(actionRequest, "version");
280    
281                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
282                            WikiPage.class.getName(), actionRequest);
283    
284                    WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
285            }
286    
287            protected void subscribePage(ActionRequest actionRequest) throws Exception {
288                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
289                    String title = ParamUtil.getString(actionRequest, "title");
290    
291                    WikiPageServiceUtil.subscribePage(nodeId, title);
292            }
293    
294            protected void unsubscribePage(ActionRequest actionRequest)
295                    throws Exception {
296    
297                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
298                    String title = ParamUtil.getString(actionRequest, "title");
299    
300                    WikiPageServiceUtil.unsubscribePage(nodeId, title);
301            }
302    
303            protected WikiPage updatePage(ActionRequest actionRequest)
304                    throws Exception {
305    
306                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
307    
308                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
309                    String title = ParamUtil.getString(actionRequest, "title");
310                    double version = ParamUtil.getDouble(actionRequest, "version");
311    
312                    String content = ParamUtil.getString(actionRequest, "content");
313                    String summary = ParamUtil.getString(actionRequest, "summary");
314                    boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
315                    String format = ParamUtil.getString(actionRequest, "format");
316                    String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
317                    String redirectTitle = null;
318    
319                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
320                            WikiPage.class.getName(), actionRequest);
321    
322                    WikiPage page = null;
323    
324                    if (cmd.equals(Constants.ADD)) {
325                            page = WikiPageServiceUtil.addPage(
326                                    nodeId, title, content, summary, minorEdit, format, parentTitle,
327                                    redirectTitle, serviceContext);
328                    }
329                    else {
330                            page = WikiPageServiceUtil.updatePage(
331                                    nodeId, title, version, content, summary, minorEdit, format,
332                                    parentTitle, redirectTitle, serviceContext);
333                    }
334    
335                    return page;
336            }
337    
338            @Override
339            protected boolean isCheckMethodOnProcessAction() {
340                    return _CHECK_METHOD_ON_PROCESS_ACTION;
341            }
342    
343            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
344    
345    }