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.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 mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.RESTORE)) {
057                                    restoreEntries(actionRequest);
058                            }
059    
060                            WindowState windowState = actionRequest.getWindowState();
061    
062                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
063                                    sendRedirect(actionRequest, actionResponse);
064                            }
065                            else {
066                                    String redirect = PortalUtil.escapeRedirect(
067                                            ParamUtil.getString(actionRequest, "redirect"));
068    
069                                    if (Validator.isNotNull(redirect)) {
070                                            actionResponse.sendRedirect(redirect);
071                                    }
072                            }
073                    }
074                    catch (Exception e) {
075                            if (e instanceof NoSuchCategoryException ||
076                                    e instanceof LockedThreadException ||
077                                    e instanceof PrincipalException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass());
080    
081                                    setForward(actionRequest, "portlet.message_boards.error");
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            protected void restoreEntries(ActionRequest actionRequest)
090                    throws PortalException, SystemException {
091    
092                    long[] restoreCategoryIds = StringUtil.split(
093                            ParamUtil.getString(actionRequest, "restoreCategoryIds"), 0L);
094    
095                    for (long restoreCategoryId : restoreCategoryIds) {
096                            MBCategoryServiceUtil.restoreCategoryFromTrash(restoreCategoryId);
097                    }
098    
099                    long[] restoreThreadIds = StringUtil.split(
100                            ParamUtil.getString(actionRequest, "restoreThreadIds"), 0L);
101    
102                    for (long restoreThreadId : restoreThreadIds) {
103                            MBThreadServiceUtil.restoreThreadFromTrash(restoreThreadId);
104                    }
105            }
106    
107    }