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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayWindowState;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
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.portal.util.PortalUtil;
028    import com.liferay.portlet.messageboards.LockedThreadException;
029    import com.liferay.portlet.messageboards.NoSuchCategoryException;
030    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
031    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.WindowState;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Zsolt Berentey
043     * @author Eduardo Garcia
044     */
045    public class EditEntryAction extends PortletAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, ActionRequest actionRequest,
051                            ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals(Constants.RESTORE)) {
058                                    restoreEntries(actionRequest);
059                            }
060    
061                            WindowState windowState = actionRequest.getWindowState();
062    
063                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
064                                    sendRedirect(actionRequest, actionResponse);
065                            }
066                            else {
067                                    String redirect = PortalUtil.escapeRedirect(
068                                            ParamUtil.getString(actionRequest, "redirect"));
069    
070                                    if (Validator.isNotNull(redirect)) {
071                                            actionResponse.sendRedirect(redirect);
072                                    }
073                            }
074                    }
075                    catch (Exception e) {
076                            if (e instanceof NoSuchCategoryException ||
077                                    e instanceof LockedThreadException ||
078                                    e instanceof PrincipalException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass());
081    
082                                    setForward(actionRequest, "portlet.message_boards.error");
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            protected void restoreEntries(ActionRequest actionRequest)
091                    throws PortalException, SystemException {
092    
093                    long[] restoreCategoryIds = StringUtil.split(
094                            ParamUtil.getString(actionRequest, "restoreCategoryIds"), 0L);
095    
096                    for (long restoreCategoryId : restoreCategoryIds) {
097                            MBCategoryServiceUtil.restoreCategoryFromTrash(restoreCategoryId);
098                    }
099    
100                    long[] restoreThreadIds = StringUtil.split(
101                            ParamUtil.getString(actionRequest, "restoreThreadIds"), 0L);
102    
103                    for (long restoreThreadId : restoreThreadIds) {
104                            MBThreadServiceUtil.restoreThreadFromTrash(restoreThreadId);
105                    }
106            }
107    
108    }