001    /**
002     * Copyright (c) 2000-2010 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.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.messageboards.model.MBBan;
025    import com.liferay.portlet.messageboards.service.MBBanServiceUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Michael Young
036     */
037    public class BanUserAction extends PortletAction {
038    
039            public void processAction(
040                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
041                            ActionRequest actionRequest, ActionResponse actionResponse)
042                    throws Exception {
043    
044                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
045    
046                    try {
047                            if (cmd.equals("ban")) {
048                                    banUser(actionRequest);
049                            }
050                            else if (cmd.equals("unban")) {
051                                    unbanUser(actionRequest);
052                            }
053    
054                            sendRedirect(actionRequest, actionResponse);
055                    }
056                    catch (Exception e) {
057                            if (e instanceof PrincipalException) {
058                                    SessionErrors.add(actionRequest, e.getClass().getName());
059    
060                                    setForward(actionRequest, "portlet.message_boards.error");
061                            }
062                            else {
063                                    throw e;
064                            }
065                    }
066            }
067    
068            protected void banUser(ActionRequest actionRequest) throws Exception {
069                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
070    
071                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
072                            MBBan.class.getName(), actionRequest);
073    
074                    MBBanServiceUtil.addBan(banUserId, serviceContext);
075            }
076    
077            protected void unbanUser(ActionRequest actionRequest) throws Exception {
078                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
079    
080                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
081                            MBBan.class.getName(), actionRequest);
082    
083                    MBBanServiceUtil.deleteBan(banUserId, serviceContext);
084            }
085    
086    }