com.liferay.social.requests.web.internal.portlet.action.UpdateRequestMVCRenderCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.social.requests.web.internal.portlet.action.UpdateRequestMVCRenderCommand.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.social.requests.web.internal.portlet.action;

import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.social.kernel.exception.NoSuchRequestException;
import com.liferay.social.kernel.service.SocialRequestService;
import com.liferay.social.requests.web.internal.constants.SocialRequestsPortletKeys;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Adolfo Prez
 */
@Component(immediate = true, property = { "javax.portlet.name=" + SocialRequestsPortletKeys.REQUESTS,
        "mvc.command.name=/requests/update_request" }, service = MVCActionCommand.class)
public class UpdateRequestMVCRenderCommand extends BaseMVCActionCommand {

    @Override
    protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        try {
            updateRequest(actionRequest);

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

            if (Validator.isNotNull(redirect)) {
                actionResponse.sendRedirect(redirect);
            }
        } catch (NoSuchRequestException | PrincipalException e) {
            SessionErrors.add(actionRequest, e.getClass());

            actionResponse.setRenderParameter("mvcPath", "/error.jsp");
        }
    }

    @Reference(unbind = "-")
    protected void setSocialRequestService(SocialRequestService socialRequestService) {

        _socialRequestService = socialRequestService;
    }

    protected void updateRequest(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long requestId = ParamUtil.getLong(actionRequest, "requestId");
        int status = ParamUtil.getInteger(actionRequest, "status");

        _socialRequestService.updateRequest(requestId, status, themeDisplay);
    }

    @Reference
    private Portal _portal;

    private SocialRequestService _socialRequestService;

}