Search in sources :

Example 31 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class CustomerStateRequestProcessor method resolveAnonymousCustomer.

/**
 * <p>Implementors can subclass to change how anonymous customers are created. Note that this method is intended to actually create the anonymous
 * customer if one does not exist. If you are looking to just get the current anonymous customer (if it exists) then instead use the
 * {@link #getAnonymousCustomer(WebRequest)} method.<p>
 *
 * <p>The intended behavior of this method is as follows:</p>
 *
 * <ul>
 *  <li>Look for a {@link Customer} on the session</li>
 *  <ul>
 *      <li>If a customer is found in session, keep using the session-based customer</li>
 *      <li>If a customer is not found in session</li>
 *      <ul>
 *          <li>Look for a customer ID in session</li>
 *          <li>If a customer ID is found in session:</li>
 *          <ul><li>Look up the customer in the database</ul></li>
 *      </ul>
 *      <li>If no there is no customer ID in session (and thus no {@link Customer})</li>
 *      <ol>
 *          <li>Create a new customer</li>
 *          <li>Put the newly-created {@link Customer} in session</li>
 *      </ol>
 *  </ul>
 * </ul>
 *
 * @param request
 * @return
 * @see {@link #getAnonymousCustomer(WebRequest)}
 * @see {@link #getAnonymousCustomerAttributeName()}
 * @see {@link #getAnonymousCustomerIdAttributeName()}
 */
public Customer resolveAnonymousCustomer(WebRequest request) {
    Customer customer;
    customer = getAnonymousCustomer(request);
    // and store the entire customer in session (don't persist to DB just yet)
    if (customer == null) {
        customer = customerService.createNewCustomer();
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.setAttribute(getAnonymousCustomerSessionAttributeName(), customer, WebRequest.SCOPE_GLOBAL_SESSION);
        }
    }
    customer.setAnonymous(true);
    return customer;
}
Also used : Customer(org.broadleafcommerce.profile.core.domain.Customer)

Example 32 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class CustomerStateRequestProcessor method getAnonymousCustomer.

/**
 * Returns the anonymous customer that was saved in session. This first checks for a full customer in session (meaning
 * that the customer has not already been persisted) and returns that. If there is no full customer in session (and
 * there is instead just an anonymous customer ID) then this will look up the customer from the database using that and
 * return it.
 *
 * @param request the current request
 * @return the anonymous customer in session or null if there is no anonymous customer represented in session
 * @see {@link #getAnonymousCustomerSessionAttributeName()}
 * @see {@link #getAnonymousCustomerIdSessionAttributeName()}
 */
public Customer getAnonymousCustomer(WebRequest request) {
    if (anonymousCustomerExtensionManager != null) {
        ExtensionResultHolder<Customer> resultHolder = new ExtensionResultHolder<Customer>();
        anonymousCustomerExtensionManager.getProxy().getAnonymousCustomer(resultHolder, request);
        if (resultHolder.getResult() != null) {
            return resultHolder.getResult();
        }
    }
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        Customer anonymousCustomer = (Customer) request.getAttribute(getAnonymousCustomerSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
        if (anonymousCustomer == null) {
            // Customer is not in session, see if we have just a customer ID in session (the anonymous customer might have
            // already been persisted)
            Long customerId = (Long) request.getAttribute(getAnonymousCustomerIdSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION);
            if (customerId != null) {
                // we have a customer ID in session, look up the customer from the database to ensure we have an up-to-date
                // customer to store in CustomerState
                anonymousCustomer = customerService.readCustomerById(customerId);
            }
        }
        return anonymousCustomer;
    }
    return null;
}
Also used : Customer(org.broadleafcommerce.profile.core.domain.Customer) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 33 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class CustomerStateRequestProcessor method process.

@Override
public void process(WebRequest request) {
    Customer customer = null;
    Long overrideId = null;
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        overrideId = (Long) request.getAttribute(OVERRIDE_CUSTOMER_SESSION_ATTR_NAME, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    if (overrideId != null) {
        customer = customerService.readCustomerById(overrideId);
        if (customer != null && !customer.isRegistered() && !customer.isLoggedIn() && !customer.isCookied()) {
            customer.setAnonymous(true);
        }
    } else {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if ((authentication != null) && !(authentication instanceof AnonymousAuthenticationToken)) {
            String userName = authentication.getName();
            customer = (Customer) BroadleafRequestCustomerResolverImpl.getRequestCustomerResolver().getCustomer(request);
            if (userName != null && (customer == null || !userName.equals(customer.getUsername()))) {
                // can only get here if the authenticated user does not match the user in session
                customer = customerService.readCustomerByUsername(userName);
                if (logger.isDebugEnabled() && customer != null) {
                    logger.debug("Customer found by username " + userName);
                }
            }
            if (customer != null) {
                String lastPublishedEventClass = (String) BLCRequestUtils.getSessionAttributeIfOk(request, LAST_PUBLISHED_EVENT_CLASS_SESSION_ATTRIBUTE_NAME);
                String eventUsername = (String) BLCRequestUtils.getSessionAttributeIfOk(request, LAST_PUBLISHED_EVENT_USERNAME_SESSION_ATTRIBUTE_NAME);
                if (authentication instanceof RememberMeAuthenticationToken) {
                    // set transient property of customer
                    customer.setCookied(true);
                    boolean publishRememberMeEvent = true;
                    if (CustomerAuthenticatedFromCookieEvent.class.getName().equals(lastPublishedEventClass)) {
                        if (userName.equals(eventUsername)) {
                            publishRememberMeEvent = false;
                        }
                    }
                    if (publishRememberMeEvent) {
                        CustomerAuthenticatedFromCookieEvent cookieEvent = new CustomerAuthenticatedFromCookieEvent(customer, this.getClass().getName());
                        publishEvent(cookieEvent, request, CustomerAuthenticatedFromCookieEvent.class.getName(), userName);
                    }
                } else if (authentication instanceof UsernamePasswordAuthenticationToken) {
                    customer.setLoggedIn(true);
                    boolean publishLoggedInEvent = true;
                    if (CustomerLoggedInEvent.class.getName().equals(lastPublishedEventClass)) {
                        if (userName.equals(eventUsername)) {
                            publishLoggedInEvent = false;
                        }
                    }
                    if (publishLoggedInEvent) {
                        CustomerLoggedInEvent loggedInEvent = new CustomerLoggedInEvent(customer, this.getClass().getName());
                        publishEvent(loggedInEvent, request, CustomerLoggedInEvent.class.getName(), userName);
                    }
                }
            } else {
                customer = resolveAuthenticatedCustomer(authentication);
            }
        }
    }
    if (customer == null) {
        // This is an anonymous customer.
        // TODO: Handle a custom cookie (different than remember me) that is just for anonymous users.
        // This can be used to remember their cart from a previous visit.
        // Cookie logic probably needs to be configurable - with TCS as the exception.
        customer = resolveAnonymousCustomer(request);
    } else {
        // Does this customer need to have an anonymous customer's data merged into it?
        customer = mergeCustomerIfRequired(request, customer);
    }
    CustomerState.setCustomer(customer);
    // Setup customer for content rule processing
    @SuppressWarnings("unchecked") Map<String, Object> ruleMap = (Map<String, Object>) request.getAttribute(BLC_RULE_MAP_PARAM, WebRequest.SCOPE_REQUEST);
    if (ruleMap == null) {
        ruleMap = new HashMap<String, Object>();
    }
    ruleMap.put("customer", customer);
    request.setAttribute(BLC_RULE_MAP_PARAM, ruleMap, WebRequest.SCOPE_REQUEST);
}
Also used : Customer(org.broadleafcommerce.profile.core.domain.Customer) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Authentication(org.springframework.security.core.Authentication) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class RegistrationServiceImpl method initCustomerRegistrationForm.

@Override
public RegisterCustomerForm initCustomerRegistrationForm() {
    Customer customer = CustomerState.getCustomer();
    if (customer == null || !customer.isAnonymous()) {
        customer = customerService.createCustomerFromId(null);
    }
    RegisterCustomerForm customerRegistrationForm = new RegisterCustomerForm();
    customerRegistrationForm.setCustomer(customer);
    return customerRegistrationForm;
}
Also used : Customer(org.broadleafcommerce.profile.core.domain.Customer) RegisterCustomerForm(org.broadleafcommerce.profile.web.core.form.RegisterCustomerForm)

Example 35 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class EmailClickTrackingFilter method doFilter.

/* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
@Override
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    String emailId = request.getParameter("email_id");
    if (emailId != null) {
        /*
             * The direct parameter map from the request object returns its values
             * in arrays (at least for the tomcat implementation). We make our own
             * parameter map to avoid this situation.
             */
        Map<String, String> parameterMap = new HashMap<String, String>();
        Enumeration names = request.getParameterNames();
        String customerId = request.getParameter("customerId");
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            if (!"customerId".equals(name)) {
                parameterMap.put(name, request.getParameter(name));
            }
        }
        if (customerId == null) {
            // Attempt to get customerId from current cookied customer
            Customer customer = customerState.getCustomer((HttpServletRequest) request);
            if (customer != null && customer.getId() != null) {
                customerId = customer.getId().toString();
            }
        }
        Map<String, String> extraValues = new HashMap<String, String>();
        extraValues.put("requestUri", ((HttpServletRequest) request).getRequestURI());
        emailTrackingManager.recordClick(Long.valueOf(emailId), parameterMap, customerId, extraValues);
    }
    chain.doFilter(request, response);
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Customer(org.broadleafcommerce.profile.core.domain.Customer)

Aggregations

Customer (org.broadleafcommerce.profile.core.domain.Customer)98 Order (org.broadleafcommerce.core.order.domain.Order)41 Transactional (org.springframework.transaction.annotation.Transactional)34 Test (org.testng.annotations.Test)33 Address (org.broadleafcommerce.profile.core.domain.Address)14 Rollback (org.springframework.test.annotation.Rollback)11 HashMap (java.util.HashMap)9 CustomerAddress (org.broadleafcommerce.profile.core.domain.CustomerAddress)9 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)6 ArrayList (java.util.ArrayList)5 Money (org.broadleafcommerce.common.money.Money)5 Category (org.broadleafcommerce.core.catalog.domain.Category)5 Product (org.broadleafcommerce.core.catalog.domain.Product)5 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)5 CommonSetupBaseTest (org.broadleafcommerce.test.CommonSetupBaseTest)5 CustomerImpl (org.broadleafcommerce.profile.core.domain.CustomerImpl)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)3 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)3