001    /**
002     * Copyright (c) 2000-2013 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portlet.documentlibrary.action.EditFileEntryAction;
023    import com.liferay.portlet.wiki.NoSuchNodeException;
024    import com.liferay.portlet.wiki.NoSuchPageException;
025    import com.liferay.portlet.wiki.model.WikiPage;
026    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
027    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
028    
029    import java.util.List;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Eudaldo Alonso
043     */
044    public class EditNodeAttachmentAction extends EditFileEntryAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.EMPTY_TRASH)) {
057                                    emptyTrash(actionRequest);
058                            }
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchNodeException ||
064                                    e instanceof NoSuchPageException ||
065                                    e instanceof PrincipalException) {
066    
067                                    SessionErrors.add(actionRequest, e.getClass());
068    
069                                    setForward(actionRequest, "portlet.wiki.error");
070                            }
071                            else {
072                                    throw e;
073                            }
074                    }
075            }
076    
077            @Override
078            public ActionForward render(
079                            ActionMapping actionMapping, ActionForm actionForm,
080                            PortletConfig portletConfig, RenderRequest renderRequest,
081                            RenderResponse renderResponse)
082                    throws Exception {
083    
084                    try {
085                            ActionUtil.getNode(renderRequest);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchNodeException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(renderRequest, e.getClass());
092    
093                                    return actionMapping.findForward("portlet.wiki.error");
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099    
100                    return actionMapping.findForward(
101                            getForward(
102                                    renderRequest, "portlet.wiki.view_node_deleted_attachments"));
103            }
104    
105            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
106                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
107    
108                    List<WikiPage> wikiPages = WikiPageLocalServiceUtil.getPages(
109                            nodeId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
110    
111                    for (WikiPage wikiPage : wikiPages) {
112                            if (wikiPage.getDeletedAttachmentsFileEntriesCount() > 0) {
113                                    WikiPageServiceUtil.deleteTrashPageAttachments(
114                                            nodeId, wikiPage.getTitle());
115                            }
116                    }
117            }
118    
119    }