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.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portlet.messageboards.LockedThreadException;
027    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
028    
029    import java.util.HashMap;
030    import java.util.Map;
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 mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.DELETE)) {
056                                    deleteThreads(
057                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
058                            }
059                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
060                                    deleteThreads(
061                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
062                            }
063    
064                            sendRedirect(actionRequest, actionResponse);
065                    }
066                    catch (Exception e) {
067                            if (e instanceof LockedThreadException ||
068                                    e instanceof PrincipalException) {
069    
070                                    SessionErrors.add(actionRequest, e.getClass());
071    
072                                    setForward(actionRequest, "portlet.message_boards.error");
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            protected void deleteThreads(
081                            LiferayPortletConfig liferayPortletConfig,
082                            ActionRequest actionRequest, boolean moveToTrash)
083                    throws Exception {
084    
085                    long[] deleteThreadIds = null;
086    
087                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
088    
089                    if (threadId > 0) {
090                            deleteThreadIds = new long[] {threadId};
091                    }
092                    else {
093                            deleteThreadIds = StringUtil.split(
094                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
095                    }
096    
097                    for (long deleteThreadId : deleteThreadIds) {
098                            if (moveToTrash) {
099                                    MBThreadServiceUtil.moveThreadToTrash(deleteThreadId);
100                            }
101                            else {
102                                    MBThreadServiceUtil.deleteThread(deleteThreadId);
103                            }
104                    }
105    
106                    if (moveToTrash && (deleteThreadIds.length > 0)) {
107                            Map<String, String[]> data = new HashMap<String, String[]>();
108    
109                            data.put(
110                                    "restoreThreadIds", ArrayUtil.toStringArray(deleteThreadIds));
111    
112                            SessionMessages.add(
113                                    actionRequest,
114                                    liferayPortletConfig.getPortletId() +
115                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
116    
117                            SessionMessages.add(
118                                    actionRequest,
119                                    liferayPortletConfig.getPortletId() +
120                                            SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
121                    }
122            }
123    
124    }