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.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.kernel.util.Validator;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portlet.messageboards.LockedThreadException;
028    import com.liferay.portlet.messageboards.model.MBMessage;
029    import com.liferay.portlet.messageboards.model.MBThread;
030    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Deepak Gothe
045     * @author Sergio Gonz??lez
046     * @author Zsolt Berentey
047     */
048    public class DeleteThreadAction 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.DELETE)) {
061                                    deleteThreads(
062                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
063                            }
064                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
065                                    deleteThreads(
066                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
067                            }
068    
069                            sendRedirect(actionRequest, actionResponse);
070                    }
071                    catch (Exception e) {
072                            if (e instanceof LockedThreadException ||
073                                    e instanceof PrincipalException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass());
076    
077                                    setForward(actionRequest, "portlet.message_boards.error");
078                            }
079                            else {
080                                    throw e;
081                            }
082                    }
083            }
084    
085            protected void deleteThreads(
086                            LiferayPortletConfig liferayPortletConfig,
087                            ActionRequest actionRequest, boolean moveToTrash)
088                    throws Exception {
089    
090                    String deleteEntryTitle = null;
091    
092                    long[] deleteThreadIds = null;
093    
094                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
095    
096                    if (threadId > 0) {
097                            deleteThreadIds = new long[] {threadId};
098                    }
099                    else {
100                            deleteThreadIds = StringUtil.split(
101                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
102                    }
103    
104                    for (int i = 0; i < deleteThreadIds.length; i++) {
105                            long deleteThreadId = deleteThreadIds[i];
106    
107                            if (moveToTrash) {
108                                    MBThread thread = MBThreadServiceUtil.moveThreadToTrash(
109                                            deleteThreadId);
110    
111                                    if (i == 0) {
112                                            MBMessage message = MBMessageLocalServiceUtil.getMessage(
113                                                    thread.getRootMessageId());
114    
115                                            deleteEntryTitle = message.getSubject();
116                                    }
117                            }
118                            else {
119                                    MBThreadServiceUtil.deleteThread(deleteThreadId);
120                            }
121                    }
122    
123                    if (moveToTrash && (deleteThreadIds.length > 0)) {
124                            Map<String, String[]> data = new HashMap<String, String[]>();
125    
126                            data.put(
127                                    "deleteEntryClassName",
128                                    new String[] {MBThread.class.getName()});
129    
130                            if (Validator.isNotNull(deleteEntryTitle)) {
131                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
132                            }
133    
134                            data.put(
135                                    "restoreThreadIds", ArrayUtil.toStringArray(deleteThreadIds));
136    
137                            SessionMessages.add(
138                                    actionRequest,
139                                    liferayPortletConfig.getPortletId() +
140                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
141    
142                            hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
143                    }
144            }
145    
146    }