001    /**
002     * Copyright (c) 2000-present 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.messageboards.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
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.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.messageboards.NoSuchMessageException;
025    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Eudaldo Alonso
040     */
041    public class EditMessageAttachmentsAction extends PortletAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals(Constants.DELETE)) {
054                                    deleteAttachment(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.EMPTY_TRASH)) {
057                                    emptyTrash(actionRequest);
058                            }
059    
060                            if (Validator.isNotNull(cmd)) {
061                                    String redirect = ParamUtil.getString(
062                                            actionRequest, "redirect");
063    
064                                    sendRedirect(actionRequest, actionResponse, redirect);
065                            }
066                    }
067                    catch (Exception e) {
068                            if (e instanceof PrincipalException) {
069                                    SessionErrors.add(actionRequest, e.getClass());
070    
071                                    setForward(actionRequest, "portlet.message_boards.error");
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping actionMapping, ActionForm actionForm,
082                            PortletConfig portletConfig, RenderRequest renderRequest,
083                            RenderResponse renderResponse)
084                    throws Exception {
085    
086                    try {
087                            ActionUtil.getMessage(renderRequest);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchMessageException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(renderRequest, e.getClass());
094    
095                                    return actionMapping.findForward(
096                                            "portlet.message_boards.error");
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102    
103                    return actionMapping.findForward(
104                            getForward(
105                                    renderRequest,
106                                    "portlet.message_boards.view_deleted_message_attachments"));
107            }
108    
109            protected void deleteAttachment(ActionRequest actionRequest)
110                    throws PortalException {
111    
112                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
113    
114                    String fileName = ParamUtil.getString(actionRequest, "fileName");
115    
116                    MBMessageLocalServiceUtil.deleteMessageAttachment(messageId, fileName);
117            }
118    
119            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
120                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
121    
122                    MBMessageServiceUtil.deleteMessageAttachments(messageId);
123            }
124    
125    }