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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portlet.messageboards.LockedThreadException;
023    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletConfig;
028    
029    import org.apache.struts.action.ActionForm;
030    import org.apache.struts.action.ActionMapping;
031    
032    /**
033     * @author Deepak Gothe
034     * @author Sergio González
035     */
036    public class DeleteThreadAction extends PortletAction {
037    
038            @Override
039            public void processAction(
040                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
041                            ActionRequest actionRequest, ActionResponse actionResponse)
042                    throws Exception {
043    
044                    try {
045                            deleteThreads(actionRequest, actionResponse);
046    
047                            sendRedirect(actionRequest, actionResponse);
048                    }
049                    catch (Exception e) {
050                            if (e instanceof LockedThreadException ||
051                                    e instanceof PrincipalException) {
052    
053                                    SessionErrors.add(actionRequest, e.getClass().getName());
054    
055                                    setForward(actionRequest, "portlet.message_boards.error");
056                            }
057                            else {
058                                    throw e;
059                            }
060                    }
061            }
062    
063            protected void deleteThreads(
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception {
066    
067                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
068    
069                    if (threadId > 0) {
070                            MBThreadServiceUtil.deleteThread(threadId);
071                    }
072                    else {
073                            long[] deleteThreadIds = StringUtil.split(
074                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
075    
076                            for (int i = 0; i < deleteThreadIds.length; i++) {
077                                    MBThreadServiceUtil.deleteThread(deleteThreadIds[i]);
078                            }
079                    }
080            }
081    
082    }