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.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 actionMapping, ActionForm actionForm,
041                            PortletConfig portletConfig, ActionRequest actionRequest,
042                            ActionResponse actionResponse)
043                    throws Exception {
044    
045                    try {
046                            deleteThreads(actionRequest, actionResponse);
047    
048                            sendRedirect(actionRequest, actionResponse);
049                    }
050                    catch (Exception e) {
051                            if (e instanceof LockedThreadException ||
052                                    e instanceof PrincipalException) {
053    
054                                    SessionErrors.add(actionRequest, e.getClass());
055    
056                                    setForward(actionRequest, "portlet.message_boards.error");
057                            }
058                            else {
059                                    throw e;
060                            }
061                    }
062            }
063    
064            protected void deleteThreads(
065                            ActionRequest actionRequest, ActionResponse actionResponse)
066                    throws Exception {
067    
068                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
069    
070                    if (threadId > 0) {
071                            MBThreadServiceUtil.deleteThread(threadId);
072                    }
073                    else {
074                            long[] deleteThreadIds = StringUtil.split(
075                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
076    
077                            for (int i = 0; i < deleteThreadIds.length; i++) {
078                                    MBThreadServiceUtil.deleteThread(deleteThreadIds[i]);
079                            }
080                    }
081            }
082    
083    }