BroadleafCommerce/BroadleafCommerce — pull request #365 by phillipuniverse (2013-09-12 22:33:18+00:00)
Title: Anonymous Customers can be persisted but are not resolved from the database

Previous functionality resolved anonymous customers as follows:

1. Look up a full Customer stored in session
2. If the Customer is there, set it on CustomerState
3. If the Customer is not there, create a new Customer (don't persist it) and store that in session

However, this breaks down once that session-based Customer is persisted, which occurs anytime an anonymous user adds something to their cart.

Reproducible on the demo site by doing:
1) Add 2 items to your cart
2) Go to checkout click on 'Ship to Multiple Addresses'
3) Add address 1 (it will succeed)
4) Add new Address 2 (on save it will fail)

This fails because this logic attempts to save the list of CustomerAddresses for the Customer, but uses CustomerState.getCustomer(). That customer has an empty list of CustomerAddresses so the merge fails (that's the basic jist of it; there are a few more irrelevant details as to why it fails).

Rather than attempt to synchronize between the session-based customer and the database-customer, once the system detects that the Customer has been persisted to the database, this will attempt to utilize the Hibernate-based Customer rather than anything in session.

The full functionality is below for easier review:

1. Look for a Customer on the session
- If a customer is found in session:
- Look for a Customer with the same id in the database
- If we find one in the database:
1. Remove the Customer from the session
2. Add a session variable for just the customer ID
3. Return the database-version of the customer
- If we don't find one in the database, keep using the session-based customer
- If a customer is not found in session
- Look for a customer ID in session
- If a customer ID is found in session:
- Look up the customer in the database
- If no there is no customer ID in session (and thus no Customer)
1. Create a new customer
2. Put the newly-created Customer in session

@jefffischer please take a look over this change and let me know what you think. @bpolster you might be interested in this as well.