com.liferay.akismet.hook.action.AkismetEditMessageAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.akismet.hook.action.AkismetEditMessageAction.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.akismet.hook.action;

import com.liferay.akismet.util.AkismetUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.security.permission.ActionKeys;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.security.permission.PermissionThreadLocal;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.messageboards.model.MBMessage;
import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

/**
 * @author Amos Fong
 * @author Mika Koivisto
 */
public class AkismetEditMessageAction extends BaseStrutsPortletAction {

    @Override
    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        if (cmd.equals("updateStatus")) {
            try {
                updateStatus(actionRequest, actionResponse);

                String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

                actionResponse.sendRedirect(redirect);
            } catch (PrincipalException pe) {
                throw pe;
            } catch (Exception e) {
                SessionErrors.add(actionRequest, e.getClass());
            }
        } else {
            originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
        }
    }

    @Override
    public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
            RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {

        return originalStrutsPortletAction.render(portletConfig, renderRequest, renderResponse);
    }

    @Override
    public void serveResource(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
            ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

        originalStrutsPortletAction.serveResource(portletConfig, resourceRequest, resourceResponse);
    }

    protected void checkPermission(long scopeGroupId) throws PortalException {
        PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

        if (!permissionChecker.hasPermission(scopeGroupId, "com.liferay.portlet.messageboards", scopeGroupId,
                ActionKeys.BAN_USER)) {

            throw new PrincipalException();
        }
    }

    protected void updateStatus(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException {

        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        checkPermission(themeDisplay.getScopeGroupId());

        long messageId = ParamUtil.getLong(actionRequest, "messageId");

        boolean spam = ParamUtil.getBoolean(actionRequest, "spam");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

        if (spam) {
            MBMessage message = MBMessageLocalServiceUtil.updateStatus(themeDisplay.getUserId(), messageId,
                    WorkflowConstants.STATUS_DENIED, serviceContext);

            if (AkismetUtil.isMessageBoardsEnabled(message.getCompanyId())) {
                AkismetUtil.submitSpam(message);
            }
        } else {
            MBMessage message = MBMessageLocalServiceUtil.updateStatus(themeDisplay.getUserId(), messageId,
                    WorkflowConstants.STATUS_APPROVED, serviceContext);

            if (AkismetUtil.isMessageBoardsEnabled(message.getCompanyId())) {
                AkismetUtil.submitHam(message);
            }
        }
    }

}