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