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 mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, 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                            else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
060                                    restoreAttachmentFromTrash(actionRequest);
061                            }
062    
063                            if (Validator.isNotNull(cmd)) {
064                                    String redirect = ParamUtil.getString(
065                                            actionRequest, "redirect");
066    
067                                    sendRedirect(actionRequest, actionResponse, redirect);
068                            }
069                    }
070                    catch (Exception e) {
071                            if (e instanceof PrincipalException) {
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.message_boards.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getMessage(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchMessageException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass());
096    
097                                    return mapping.findForward("portlet.message_boards.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    return mapping.findForward(
105                            getForward(
106                                    renderRequest,
107                                    "portlet.message_boards.view_deleted_message_attachments"));
108            }
109    
110            protected void deleteAttachment(ActionRequest actionRequest)
111                    throws PortalException, SystemException {
112    
113                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
114    
115                    String fileName = ParamUtil.getString(actionRequest, "fileName");
116    
117                    MBMessageLocalServiceUtil.deleteMessageAttachment(messageId, fileName);
118            }
119    
120            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
121                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
122    
123                    MBMessageServiceUtil.deleteMessageAttachments(messageId);
124            }
125    
126            protected void restoreAttachmentFromTrash(ActionRequest actionRequest)
127                    throws PortalException, SystemException {
128    
129                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
130    
131                    String fileName = ParamUtil.getString(actionRequest, "fileName");
132    
133                    MBMessageServiceUtil.restoreMessageAttachmentFromTrash(
134                            messageId, fileName);
135            }
136    
137    }