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.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
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(actionRequest, false);
062                            }
063                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
064                                    deleteThreads(actionRequest, true);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof LockedThreadException ||
071                                    e instanceof PrincipalException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    setForward(actionRequest, "portlet.message_boards.error");
076                            }
077                            else {
078                                    throw e;
079                            }
080                    }
081            }
082    
083            protected void deleteThreads(
084                            ActionRequest actionRequest, boolean moveToTrash)
085                    throws Exception {
086    
087                    String deleteEntryTitle = null;
088    
089                    long[] deleteThreadIds = null;
090    
091                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
092    
093                    if (threadId > 0) {
094                            deleteThreadIds = new long[] {threadId};
095                    }
096                    else {
097                            deleteThreadIds = StringUtil.split(
098                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
099                    }
100    
101                    for (int i = 0; i < deleteThreadIds.length; i++) {
102                            long deleteThreadId = deleteThreadIds[i];
103    
104                            if (moveToTrash) {
105                                    MBThread thread = MBThreadServiceUtil.moveThreadToTrash(
106                                            deleteThreadId);
107    
108                                    if (i == 0) {
109                                            MBMessage message = MBMessageLocalServiceUtil.getMessage(
110                                                    thread.getRootMessageId());
111    
112                                            deleteEntryTitle = message.getSubject();
113                                    }
114                            }
115                            else {
116                                    MBThreadServiceUtil.deleteThread(deleteThreadId);
117                            }
118                    }
119    
120                    if (moveToTrash && (deleteThreadIds.length > 0)) {
121                            Map<String, String[]> data = new HashMap<String, String[]>();
122    
123                            data.put(
124                                    "deleteEntryClassName",
125                                    new String[] {MBThread.class.getName()});
126    
127                            if (Validator.isNotNull(deleteEntryTitle)) {
128                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
129                            }
130    
131                            data.put(
132                                    "restoreThreadIds", ArrayUtil.toStringArray(deleteThreadIds));
133    
134                            SessionMessages.add(
135                                    actionRequest,
136                                    PortalUtil.getPortletId(actionRequest) +
137                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
138    
139                            hideDefaultSuccessMessage(actionRequest);
140                    }
141            }
142    
143    }