com.ctc.storefront.security.impl.DefaultGuestCheckoutCartCleanStrategy.java Source code

Java tutorial

Introduction

Here is the source code for com.ctc.storefront.security.impl.DefaultGuestCheckoutCartCleanStrategy.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2016 SAP SE or an SAP affiliate company.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with SAP.
 */
package com.ctc.storefront.security.impl;

import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants;
import de.hybris.platform.commerceservices.strategies.CheckoutCustomerStrategy;
import de.hybris.platform.core.model.order.CartModel;
import de.hybris.platform.order.CartService;
import de.hybris.platform.servicelayer.session.SessionService;
import de.hybris.platform.servicelayer.user.UserService;
import com.ctc.storefront.security.GuestCheckoutCartCleanStrategy;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;

public class DefaultGuestCheckoutCartCleanStrategy implements GuestCheckoutCartCleanStrategy {
    public static final String AJAX_REQUEST_HEADER_NAME = "X-Requested-With";

    private Pattern checkoutURLPattern;
    private CheckoutCustomerStrategy checkoutCustomerStrategy;
    private CartService cartService;
    private SessionService sessionService;
    private UserService userService;

    @Override
    public void cleanGuestCart(final HttpServletRequest request) {

        if (isAnonymousCheckout() && StringUtils.isBlank(request.getHeader(AJAX_REQUEST_HEADER_NAME))
                && isGetMethod(request) && !checkWhetherURLContainsCheckoutPattern(request)) {
            final CartModel cartModel = getCartService().getSessionCart();
            cartModel.setDeliveryAddress(null);
            cartModel.setDeliveryMode(null);
            cartModel.setPaymentInfo(null);
            cartModel.setUser(getUserService().getAnonymousUser());
            getCartService().saveOrder(cartModel);
            getSessionService().removeAttribute(WebConstants.ANONYMOUS_CHECKOUT);
            getSessionService().removeAttribute(WebConstants.ANONYMOUS_CHECKOUT_GUID);
        }

    }

    @Override
    public boolean checkWhetherURLContainsCheckoutPattern(final HttpServletRequest request) {
        return getCheckoutURLPattern().matcher(request.getRequestURL().toString()).matches();
    }

    protected boolean isGetMethod(final HttpServletRequest httpRequest) {
        return "GET".equalsIgnoreCase(httpRequest.getMethod());
    }

    protected CheckoutCustomerStrategy getCheckoutCustomerStrategy() {
        return checkoutCustomerStrategy;
    }

    protected boolean isAnonymousCheckout() {
        return Boolean.TRUE.equals(getSessionService().getAttribute(WebConstants.ANONYMOUS_CHECKOUT))
                && getCheckoutCustomerStrategy().isAnonymousCheckout();
    }

    @Required
    public void setCheckoutCustomerStrategy(final CheckoutCustomerStrategy checkoutCustomerStrategy) {
        this.checkoutCustomerStrategy = checkoutCustomerStrategy;
    }

    protected CartService getCartService() {
        return cartService;
    }

    @Required
    public void setCartService(final CartService cartService) {
        this.cartService = cartService;
    }

    protected SessionService getSessionService() {
        return sessionService;
    }

    @Required
    public void setSessionService(final SessionService sessionService) {
        this.sessionService = sessionService;
    }

    protected UserService getUserService() {
        return userService;
    }

    @Required
    public void setUserService(final UserService userService) {
        this.userService = userService;
    }

    public Pattern getCheckoutURLPattern() {
        return checkoutURLPattern;
    }

    @Required
    public void setCheckoutURLPattern(final String checkoutURLPattern) {
        this.checkoutURLPattern = Pattern.compile(checkoutURLPattern);
    }

}