package org.broadleafcommerce.session;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.broadleafcommerce.order.service.MergeCartResponse;
import org.broadleafcommerce.order.service.OrderService;
import org.broadleafcommerce.profile.domain.Customer;
import org.broadleafcommerce.profile.service.CustomerService;
import org.broadleafcommerce.security.CustomerState;
import org.broadleafcommerce.security.MergeCartProcessor;
import org.springframework.security.Authentication;
import org.springframework.stereotype.Service;

@Service("mergeCartProcessor")
public class MergeCartProcessorImpl implements MergeCartProcessor {

    public final static String BC_MERGE_CART_ITEMS_ADDED_SESSION_KEY = "merge_cart_items_added";

    public final static String BC_MERGE_CART_ITEMS_REMOVED_SESSION_KEY = "merge_cart_items_removed";

    @Resource
    private CustomerService customerService;

    @Resource
    private OrderService orderService;

    public void execute(HttpServletRequest request, HttpServletResponse response, Authentication authResult) {
        Customer loggedInCustomer = customerService.readCustomerByUsername((String) authResult.getPrincipal());
        Customer anonymousCustomer = CustomerState.getCustomer(request);
        Long anonymousCartId = orderService.findCartForCustomer(anonymousCustomer) == null ? null : orderService.findCartForCustomer(anonymousCustomer).getId();
        MergeCartResponse mergeCartResponse = orderService.mergeCart(loggedInCustomer, anonymousCartId);
        if (!mergeCartResponse.getAddedItems().isEmpty()) {
            request.getSession().setAttribute(BC_MERGE_CART_ITEMS_ADDED_SESSION_KEY, mergeCartResponse.getAddedItems());
        }
        if (!mergeCartResponse.getRemovedItems().isEmpty()) {
            request.getSession().setAttribute(BC_MERGE_CART_ITEMS_REMOVED_SESSION_KEY, mergeCartResponse.getRemovedItems());
        }
    }
}

@@ -734,173 +734,119 @@
    p
-ublic final static String BC_MERGE_CART_ITEMS_ADDED_SESSION_KEY = %22merge_cart_items_added%22;%0D%0A%0D%0A    public final static String BC_MERGE_CART_ITEMS_REMOVED_SESSION_KEY
+rivate String mergeCartItemsAddedKey = %22merge_cart_items_added%22;%0D%0A%0D%0A    private String mergeCartItemsRemovedKey
  = %22
@@ -1673,45 +1673,30 @@
 ute(
-BC_MERGE_CART_ITEMS_ADDED_SESSION_KEY
+mergeCartItemsAddedKey
 , me
@@ -1854,47 +1854,32 @@
 ute(
-BC_MERGE_CART_ITEMS_REMOVED_SESSION_KEY
+mergeCartItemsRemovedKey
 , me
@@ -1932,12 +1932,516 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public String getMergeCartItemsAddedKey() %7B%0D%0A        return mergeCartItemsAddedKey;%0D%0A    %7D%0D%0A%0D%0A    public void setMergeCartItemsAddedKey(String mergeCartItemsAddedKey) %7B%0D%0A        this.mergeCartItemsAddedKey = mergeCartItemsAddedKey;%0D%0A    %7D%0D%0A%0D%0A    public String getMergeCartItemsRemovedKey() %7B%0D%0A        return mergeCartItemsRemovedKey;%0D%0A    %7D%0D%0A%0D%0A    public void setMergeCartItemsRemovedKey(String mergeCartItemsRemovedKey) %7B%0D%0A        this.mergeCartItemsRemovedKey = mergeCartItemsRemovedKey;%0D%0A    %7D%0D
 %0A%7D%0D%0A

add merge cart response to allow for added/removed items as well as merged order returned