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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.TrashedModel;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.messageboards.LockedThreadException;
025    import com.liferay.portlet.messageboards.model.MBThread;
026    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
027    import com.liferay.portlet.trash.util.TrashUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Deepak Gothe
041     * @author Sergio Gonz??lez
042     * @author Zsolt Berentey
043     */
044    public class DeleteThreadAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.DELETE)) {
057                                    deleteThreads(actionRequest, false);
058                            }
059                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
060                                    deleteThreads(actionRequest, true);
061                            }
062    
063                            sendRedirect(actionRequest, actionResponse);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof LockedThreadException ||
067                                    e instanceof PrincipalException) {
068    
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            protected void deleteThreads(
080                            ActionRequest actionRequest, boolean moveToTrash)
081                    throws Exception {
082    
083                    long[] deleteThreadIds = null;
084    
085                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
086    
087                    if (threadId > 0) {
088                            deleteThreadIds = new long[] {threadId};
089                    }
090                    else {
091                            deleteThreadIds = StringUtil.split(
092                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
093                    }
094    
095                    List<TrashedModel> trashedModels = new ArrayList<TrashedModel>();
096    
097                    for (long deleteThreadId : deleteThreadIds) {
098                            if (moveToTrash) {
099                                    MBThread thread = MBThreadServiceUtil.moveThreadToTrash(
100                                            deleteThreadId);
101    
102                                    trashedModels.add(thread);
103                            }
104                            else {
105                                    MBThreadServiceUtil.deleteThread(deleteThreadId);
106                            }
107                    }
108    
109                    if (moveToTrash && !trashedModels.isEmpty()) {
110                            TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
111    
112                            hideDefaultSuccessMessage(actionRequest);
113                    }
114            }
115    
116    }