Search in sources :

Example 1 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class CustomerLoginController method logon.

private AjaxResponse logon(String userName, String password, String storeCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
    AjaxResponse jsonObject = new AjaxResponse();
    try {
        LOG.debug("Authenticating user " + userName);
        // user goes to shop filter first so store and language are set
        MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
        Language language = (Language) request.getAttribute("LANGUAGE");
        // check if username is from the appropriate store
        Customer customerModel = customerFacade.getCustomerByUserName(userName, store);
        if (customerModel == null) {
            jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
            return jsonObject;
        }
        if (!customerModel.getMerchantStore().getCode().equals(storeCode)) {
            jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
            return jsonObject;
        }
        customerFacade.authenticate(customerModel, userName, password);
        // set customer in the http session
        super.setSessionAttribute(Constants.CUSTOMER, customerModel, request);
        jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_SUCCESS);
        jsonObject.addEntry(Constants.RESPONSE_KEY_USERNAME, customerModel.getNick());
        LOG.info("Fetching and merging Shopping Cart data");
        String sessionShoppingCartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
        if (!StringUtils.isBlank(sessionShoppingCartCode)) {
            ShoppingCart shoppingCart = customerFacade.mergeCart(customerModel, sessionShoppingCartCode, store, language);
            if (shoppingCart != null) {
                ShoppingCartData shoppingCartData = this.populateShoppingCartData(shoppingCart, store, language);
                if (shoppingCartData != null) {
                    jsonObject.addEntry(Constants.SHOPPING_CART, shoppingCartData.getCode());
                    request.getSession().setAttribute(Constants.SHOPPING_CART, shoppingCartData.getCode());
                    // set cart in the cookie
                    Cookie c = new Cookie(Constants.COOKIE_NAME_CART, shoppingCartData.getCode());
                    c.setMaxAge(60 * 24 * 3600);
                    c.setPath(Constants.SLASH);
                    response.addCookie(c);
                } else {
                    // DELETE COOKIE
                    Cookie c = new Cookie(Constants.COOKIE_NAME_CART, "");
                    c.setMaxAge(0);
                    c.setPath(Constants.SLASH);
                    response.addCookie(c);
                }
            }
        } else {
            ShoppingCart cartModel = shoppingCartService.getShoppingCart(customerModel);
            if (cartModel != null) {
                jsonObject.addEntry(Constants.SHOPPING_CART, cartModel.getShoppingCartCode());
                request.getSession().setAttribute(Constants.SHOPPING_CART, cartModel.getShoppingCartCode());
                Cookie c = new Cookie(Constants.COOKIE_NAME_CART, cartModel.getShoppingCartCode());
                c.setMaxAge(60 * 24 * 3600);
                c.setPath(Constants.SLASH);
                response.addCookie(c);
            }
        }
        StringBuilder cookieValue = new StringBuilder();
        cookieValue.append(store.getCode()).append("_").append(customerModel.getNick());
        // set username in the cookie
        Cookie c = new Cookie(Constants.COOKIE_NAME_USER, cookieValue.toString());
        c.setMaxAge(60 * 24 * 3600);
        c.setPath(Constants.SLASH);
        response.addCookie(c);
    } catch (AuthenticationException ex) {
        jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
    } catch (Exception e) {
        jsonObject.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
    }
    return jsonObject;
}
Also used : Cookie(javax.servlet.http.Cookie) Language(com.salesmanager.core.model.reference.language.Language) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) Customer(com.salesmanager.core.model.customer.Customer) SecuredCustomer(com.salesmanager.shop.model.customer.SecuredCustomer) AuthenticationException(org.springframework.security.core.AuthenticationException) AjaxResponse(com.salesmanager.core.business.utils.ajax.AjaxResponse) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) AuthenticationException(org.springframework.security.core.AuthenticationException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 2 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartServiceImpl method getByCode.

/**
 * Get a {@link ShoppingCart} for a given code. Will update the shopping
 * cart prices and items based on the actual inventory. This method will
 * remove the shopping cart if no items are attached.
 */
@Override
@Transactional
public ShoppingCart getByCode(final String code, final MerchantStore store) throws ServiceException {
    try {
        ShoppingCart shoppingCart = shoppingCartRepository.findByCode(store.getId(), code);
        if (shoppingCart == null) {
            return null;
        }
        getPopulatedShoppingCart(shoppingCart);
        if (shoppingCart.isObsolete()) {
            delete(shoppingCart);
            return null;
        } else {
            return shoppingCart;
        }
    } catch (javax.persistence.NoResultException nre) {
        return null;
    } catch (Throwable e) {
        throw new ServiceException(e);
    }
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ServiceException(com.salesmanager.core.business.exception.ServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method addToCart.

@Override
public ReadableShoppingCart addToCart(Customer customer, PersistableShoppingCartItem item, MerchantStore store, Language language) throws Exception {
    Validate.notNull(customer, "Customer cannot be null");
    Validate.notNull(customer.getId(), "Customer.id cannot be null or empty");
    ShoppingCart cartModel = shoppingCartService.getShoppingCart(customer);
    // if cart does not exist create a new one
    if (cartModel == null) {
        cartModel = new ShoppingCart();
        cartModel.setCustomerId(customer.getId());
        cartModel.setMerchantStore(store);
        cartModel.setShoppingCartCode(uniqueShoppingCartCode());
    }
    return readableShoppingCart(cartModel, item, store, language);
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)

Example 4 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method createCartModel.

@Override
public ShoppingCart createCartModel(final String shoppingCartCode, final MerchantStore store, final Customer customer) throws Exception {
    final Long CustomerId = customer != null ? customer.getId() : null;
    ShoppingCart cartModel = new ShoppingCart();
    if (StringUtils.isNotBlank(shoppingCartCode)) {
        cartModel.setShoppingCartCode(shoppingCartCode);
    } else {
        cartModel.setShoppingCartCode(uniqueShoppingCartCode());
    }
    cartModel.setMerchantStore(store);
    if (CustomerId != null) {
        cartModel.setCustomerId(CustomerId);
    }
    shoppingCartService.create(cartModel);
    return cartModel;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)

Example 5 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method setOrderId.

@Override
public void setOrderId(String code, Long orderId, MerchantStore store) throws Exception {
    ShoppingCart cart = this.getShoppingCartModel(code, store);
    if (cart == null) {
        LOG.warn("Shopping cart with code [" + code + "] not found, expected to find a cart to set order id [" + orderId + "]");
    } else {
        cart.setOrderId(orderId);
    }
    saveOrUpdateShoppingCart(cart);
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)

Aggregations

ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)38 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)18 ServiceException (com.salesmanager.core.business.exception.ServiceException)11 Customer (com.salesmanager.core.model.customer.Customer)11 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)8 HashSet (java.util.HashSet)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)7 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)6 ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)6 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)5 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)5 LocalDate (java.time.LocalDate)5 ConversionException (com.salesmanager.core.business.exception.ConversionException)4