001    /**
002     * Copyright (c) 2000-2012 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.model.MBMessage;
027    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
028    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Eudaldo Alonso
042     */
043    public class EditMessageAttachmentsAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, 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                                    moveAttachmentFromTrash(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 mapping, ActionForm form, PortletConfig portletConfig,
086                            RenderRequest renderRequest, RenderResponse renderResponse)
087                    throws Exception {
088    
089                    try {
090                            ActionUtil.getMessage(renderRequest);
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchMessageException ||
094                                    e instanceof PrincipalException) {
095    
096                                    SessionErrors.add(renderRequest, e.getClass());
097    
098                                    return mapping.findForward("portlet.message_boards.error");
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104    
105                    return mapping.findForward(
106                            getForward(
107                                    renderRequest,
108                                    "portlet.message_boards.view_deleted_message_attachments"));
109            }
110    
111            protected void deleteAttachment(ActionRequest actionRequest)
112                    throws PortalException, SystemException {
113    
114                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
115    
116                    String fileName = ParamUtil.getString(actionRequest, "fileName");
117    
118                    MBMessageLocalServiceUtil.deleteMessageAttachment(messageId, fileName);
119            }
120    
121            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
122                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
123    
124                    MBMessageServiceUtil.deleteMessageAttachments(messageId);
125            }
126    
127            protected void moveAttachmentFromTrash(ActionRequest actionRequest)
128                    throws PortalException, SystemException {
129    
130                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
131    
132                    String fileName = ParamUtil.getString(actionRequest, "fileName");
133    
134                    MBMessageLocalServiceUtil.moveMessageAttachmentFromTrash(
135                            messageId, fileName);
136    
137                    MBMessage message = MBMessageServiceUtil.getMessage(messageId);
138    
139                    message.setAttachments(true);
140    
141                    MBMessageLocalServiceUtil.updateMBMessage(message);
142            }
143    
144    }