package org.broadleafcommerce.profile.web;

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

import org.broadleafcommerce.profile.dao.CustomerDao;
import org.broadleafcommerce.profile.domain.Customer;
import org.springframework.stereotype.Component;

@Component("customerState")
public class CustomerState {

    private static final String DEFAULTSESSIONATTRIBUTENAME = "customer_session";

    @Resource
    private CustomerDao customerDao;

    public Customer getCustomer(HttpServletRequest request) {
        Object sessionReference = request.getSession().getAttribute(DEFAULTSESSIONATTRIBUTENAME);
        Customer customer;
        if (sessionReference instanceof Long) {
            Long customerId = (Long) sessionReference;
            if (customerId != null) {
                customer = customerDao.readCustomerById(customerId);
            } else {
                customer = null;
            }
        } else {
            customer = null;
        }

        return customer;
    }

    public void setCustomer(Customer customer, HttpServletRequest request) {
        request.getSession().setAttribute(DEFAULTSESSIONATTRIBUTENAME, customer.getId());
    }

    public Long getCustomerId(HttpServletRequest request) {
        return getCustomer(request) == null ? null : getCustomer(request).getId();
    }
}

@@ -161,18 +161,21 @@
 rofile.d
-a
 o
+main
 .Custome
@@ -175,19 +175,16 @@
 Customer
-Dao
 ;%0D%0Aimpor
@@ -207,38 +207,39 @@
 ommerce.profile.
-domain
+service
 .Customer;%0D%0Aimpo
@@ -223,32 +223,39 @@
 service.Customer
+Service
 ;%0D%0Aimport org.sp
@@ -474,19 +474,23 @@
 Customer
-Dao
+Service
  custome
@@ -490,19 +490,23 @@
 customer
-Dao
+Service
 ;%0D%0A%0D%0A   
@@ -875,24 +875,30 @@
 omer
-Dao.read
+Service.create
 Customer
 ById
@@ -897,10 +897,12 @@
 omer
-By
+From
 Id(c

support anonymous browse, add to cart, checkout