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.json.JSONObject;
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.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portlet.messageboards.NoSuchMessageException;
029    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
030    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
031    import com.liferay.portlet.trash.service.TrashEntryServiceUtil;
032    import com.liferay.portlet.trash.util.TrashUtil;
033    import com.liferay.taglib.util.RestoreEntryUtil;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Eudaldo Alonso
047     */
048    public class EditMessageAttachmentsAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.CHECK)) {
061                                    JSONObject jsonObject = RestoreEntryUtil.checkEntry(
062                                            actionRequest);
063    
064                                    writeJSON(actionRequest, actionResponse, jsonObject);
065    
066                                    return;
067                            }
068                            else if (cmd.equals(Constants.DELETE)) {
069                                    deleteAttachment(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.EMPTY_TRASH)) {
072                                    emptyTrash(actionRequest);
073                            }
074                            else if (cmd.equals(Constants.RENAME)) {
075                                    restoreRename(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.RESTORE)) {
078                                    restoreEntries(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.OVERRIDE)) {
081                                    restoreOverride(actionRequest);
082                            }
083    
084                            if (Validator.isNotNull(cmd)) {
085                                    String redirect = ParamUtil.getString(
086                                            actionRequest, "redirect");
087    
088                                    sendRedirect(actionRequest, actionResponse, redirect);
089                            }
090                    }
091                    catch (Exception e) {
092                            if (e instanceof PrincipalException) {
093                                    SessionErrors.add(actionRequest, e.getClass());
094    
095                                    setForward(actionRequest, "portlet.message_boards.error");
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101            }
102    
103            @Override
104            public ActionForward render(
105                            ActionMapping actionMapping, ActionForm actionForm,
106                            PortletConfig portletConfig, RenderRequest renderRequest,
107                            RenderResponse renderResponse)
108                    throws Exception {
109    
110                    try {
111                            ActionUtil.getMessage(renderRequest);
112                    }
113                    catch (Exception e) {
114                            if (e instanceof NoSuchMessageException ||
115                                    e instanceof PrincipalException) {
116    
117                                    SessionErrors.add(renderRequest, e.getClass());
118    
119                                    return actionMapping.findForward(
120                                            "portlet.message_boards.error");
121                            }
122                            else {
123                                    throw e;
124                            }
125                    }
126    
127                    return actionMapping.findForward(
128                            getForward(
129                                    renderRequest,
130                                    "portlet.message_boards.view_deleted_message_attachments"));
131            }
132    
133            protected void deleteAttachment(ActionRequest actionRequest)
134                    throws PortalException {
135    
136                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
137    
138                    String fileName = ParamUtil.getString(actionRequest, "fileName");
139    
140                    MBMessageLocalServiceUtil.deleteMessageAttachment(messageId, fileName);
141            }
142    
143            protected void emptyTrash(ActionRequest actionRequest) throws Exception {
144                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
145    
146                    MBMessageServiceUtil.emptyMessageAttachments(messageId);
147            }
148    
149            protected void restoreEntries(ActionRequest actionRequest)
150                    throws Exception {
151    
152                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
153    
154                    if (trashEntryId > 0) {
155                            TrashEntryServiceUtil.restoreEntry(trashEntryId);
156    
157                            return;
158                    }
159    
160                    long[] restoreEntryIds = StringUtil.split(
161                            ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);
162    
163                    for (long restoreEntryId : restoreEntryIds) {
164                            TrashEntryServiceUtil.restoreEntry(restoreEntryId);
165                    }
166            }
167    
168            protected void restoreOverride(ActionRequest actionRequest)
169                    throws Exception {
170    
171                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
172    
173                    long duplicateEntryId = ParamUtil.getLong(
174                            actionRequest, "duplicateEntryId");
175    
176                    TrashEntryServiceUtil.restoreEntry(
177                            trashEntryId, duplicateEntryId, null);
178            }
179    
180            protected void restoreRename(ActionRequest actionRequest) throws Exception {
181                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
182                            WebKeys.THEME_DISPLAY);
183    
184                    long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
185    
186                    String newName = ParamUtil.getString(actionRequest, "newName");
187    
188                    if (Validator.isNull(newName)) {
189                            String oldName = ParamUtil.getString(actionRequest, "oldName");
190    
191                            newName = TrashUtil.getNewName(themeDisplay, null, 0, oldName);
192                    }
193    
194                    TrashEntryServiceUtil.restoreEntry(trashEntryId, 0, newName);
195            }
196    
197    }