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 mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.EMPTY_TRASH)) {
056                                    emptyTrash(actionRequest);
057                            }
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchNodeException ||
063                                    e instanceof NoSuchPageException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(actionRequest, "portlet.wiki.error");
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            @Override
077            public ActionForward render(
078                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
079                            RenderRequest renderRequest, RenderResponse renderResponse)
080                    throws Exception {
081    
082                    try {
083                            ActionUtil.getNode(renderRequest);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchNodeException ||
087                                    e instanceof PrincipalException) {
088    
089                                    SessionErrors.add(renderRequest, e.getClass());
090    
091                                    return mapping.findForward("portlet.wiki.error");
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097    
098                    return mapping.findForward(
099                            getForward(
100                                    renderRequest, "portlet.wiki.view_node_deleted_attachments"));
101            }
102    
103            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
104                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
105    
106                    List<WikiPage> wikiPages = WikiPageLocalServiceUtil.getPages(
107                            nodeId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
108    
109                    for (WikiPage wikiPage : wikiPages) {
110                            if (wikiPage.getDeletedAttachmentsFileEntriesCount() > 0) {
111                                    WikiPageServiceUtil.deleteTrashPageAttachments(
112                                            nodeId, wikiPage.getTitle());
113                            }
114                    }
115            }
116    
117    }