Search in sources :

Example 6 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerOrdersController method orderDetails.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/order.html", method = { RequestMethod.GET, RequestMethod.POST })
public String orderDetails(final Model model, final HttpServletRequest request, @RequestParam(value = "orderId", required = true) final String orderId) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = (Language) request.getAttribute(Constants.LANGUAGE);
    if (StringUtils.isBlank(orderId)) {
        LOGGER.error("Order Id can not be null or empty");
    }
    LOGGER.info("Fetching order details for Id " + orderId);
    // get order id
    Long lOrderId = null;
    try {
        lOrderId = Long.parseLong(orderId);
    } catch (NumberFormatException nfe) {
        LOGGER.error("Cannot parse orderId to long " + orderId);
        return "redirect:/" + Constants.SHOP_URI;
    }
    // check if order belongs to customer logged in
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Customer customer = null;
    if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
        customer = customerFacade.getCustomerByUserName(auth.getName(), store);
    }
    if (customer == null) {
        return "redirect:/" + Constants.SHOP_URI;
    }
    ReadableOrder order = orderFacade.getReadableOrder(lOrderId, store, customer.getDefaultLanguage());
    model.addAttribute("order", order);
    // check if any downloads exist for this order
    List<OrderProductDownload> orderProductDownloads = orderProdctDownloadService.getByOrderId(order.getId());
    if (CollectionUtils.isNotEmpty(orderProductDownloads)) {
        ReadableOrderProductDownloadPopulator populator = new ReadableOrderProductDownloadPopulator();
        List<ReadableOrderProductDownload> downloads = new ArrayList<ReadableOrderProductDownload>();
        for (OrderProductDownload download : orderProductDownloads) {
            ReadableOrderProductDownload view = new ReadableOrderProductDownload();
            populator.populate(download, view, store, language);
            downloads.add(view);
        }
        model.addAttribute("downloads", downloads);
    }
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.customerOrder).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : ReadableOrderProductDownloadPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductDownloadPopulator) Customer(com.salesmanager.core.model.customer.Customer) ReadableOrderProductDownload(com.salesmanager.shop.model.order.ReadableOrderProductDownload) ArrayList(java.util.ArrayList) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) Language(com.salesmanager.core.model.reference.language.Language) Authentication(org.springframework.security.core.Authentication) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) ReadableOrderProductDownload(com.salesmanager.shop.model.order.ReadableOrderProductDownload) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with Customer

use of com.salesmanager.core.model.customer.Customer 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 8 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerProductReviewController method displayProductReview.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/review.html", method = RequestMethod.GET)
public String displayProductReview(@RequestParam Long productId, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = super.getLanguage(request);
    // get product
    Product product = productService.getById(productId);
    if (product == null) {
        return "redirect:" + Constants.SHOP_URI;
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        return "redirect:" + Constants.SHOP_URI;
    }
    // create readable product
    ReadableProduct readableProduct = new ReadableProduct();
    ReadableProductPopulator readableProductPopulator = new ReadableProductPopulator();
    readableProductPopulator.setPricingService(pricingService);
    readableProductPopulator.setimageUtils(imageUtils);
    readableProductPopulator.populate(product, readableProduct, store, language);
    model.addAttribute("product", readableProduct);
    Customer customer = customerFacade.getCustomerByUserName(request.getRemoteUser(), store);
    List<ProductReview> reviews = productReviewService.getByProduct(product, language);
    for (ProductReview r : reviews) {
        if (r.getCustomer().getId().longValue() == customer.getId().longValue()) {
            ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
            ReadableProductReview rev = new ReadableProductReview();
            reviewPopulator.populate(r, rev, store, language);
            model.addAttribute("customerReview", rev);
            break;
        }
    }
    ProductReview review = new ProductReview();
    review.setCustomer(customer);
    review.setProduct(product);
    ReadableProductReview productReview = new ReadableProductReview();
    ReadableProductReviewPopulator reviewPopulator = new ReadableProductReviewPopulator();
    reviewPopulator.populate(review, productReview, store, language);
    model.addAttribute("review", productReview);
    model.addAttribute("reviews", reviews);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.review).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) Customer(com.salesmanager.core.model.customer.Customer) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableProductReview(com.salesmanager.shop.model.catalog.product.ReadableProductReview) ProductReview(com.salesmanager.core.model.catalog.product.review.ProductReview) PersistableProductReview(com.salesmanager.shop.model.catalog.product.PersistableProductReview) ReadableProductReviewPopulator(com.salesmanager.shop.populator.catalog.ReadableProductReviewPopulator) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class ShoppingCartController method shoppingCart.

private String shoppingCart(final Model model, final HttpServletRequest request, final HttpServletResponse response, final Locale locale) throws Exception {
    LOG.debug("Starting to calculate shopping cart...");
    Language language = (Language) request.getAttribute(Constants.LANGUAGE);
    // meta information
    PageInformation pageInformation = new PageInformation();
    pageInformation.setPageTitle(messages.getMessage("label.cart.placeorder", locale));
    request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
    /**
     * there must be a cart in the session *
     */
    String cartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
    if (StringUtils.isBlank(cartCode)) {
        // display empty cart
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
        return template.toString();
    }
    ShoppingCartData shoppingCart = shoppingCartFacade.getShoppingCartData(customer, store, cartCode, language);
    if (shoppingCart == null) {
        // display empty cart
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
        return template.toString();
    }
    Language lang = languageUtils.getRequestLanguage(request, response);
    // Filter unavailables
    List<ShoppingCartItem> unavailables = new ArrayList<ShoppingCartItem>();
    List<ShoppingCartItem> availables = new ArrayList<ShoppingCartItem>();
    // Take out items no more available
    List<ShoppingCartItem> items = shoppingCart.getShoppingCartItems();
    for (ShoppingCartItem item : items) {
        String code = item.getProductCode();
        Product p = productService.getByCode(code, lang);
        if (!p.isAvailable()) {
            unavailables.add(item);
        } else {
            availables.add(item);
        }
    }
    shoppingCart.setShoppingCartItems(availables);
    shoppingCart.setUnavailables(unavailables);
    model.addAttribute("cart", shoppingCart);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) PageInformation(com.salesmanager.shop.model.shop.PageInformation) Customer(com.salesmanager.core.model.customer.Customer) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData)

Example 10 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class ShoppingCartController method removeShoppingCartItem.

/**
 * Removes an item from the Shopping Cart (AJAX exposed method)
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/removeShoppingCartItem.html" }, method = { RequestMethod.GET, RequestMethod.POST })
String removeShoppingCartItem(final Long lineItemId, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    // Looks in the HttpSession to see if a customer is logged in
    // get any shopping cart for this user
    // ** need to check if the item has property, similar items may exist but with different properties
    // String attributes = request.getParameter("attribute");//attributes id are sent as 1|2|5|
    // this will help with hte removal of the appropriate item
    // remove the item shoppingCartService.create
    // create JSON representation of the shopping cart
    // return the JSON structure in AjaxResponse
    // store the shopping cart in the http session
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = (Language) request.getAttribute(Constants.LANGUAGE);
    Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
    /**
     * there must be a cart in the session *
     */
    String cartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
    if (StringUtils.isBlank(cartCode)) {
        return "redirect:/shop";
    }
    ShoppingCartData shoppingCart = shoppingCartFacade.getShoppingCartData(customer, store, cartCode, language);
    ShoppingCartData shoppingCartData = shoppingCartFacade.removeCartItem(lineItemId, shoppingCart.getCode(), store, language);
    if (shoppingCartData == null) {
        return "redirect:/shop";
    }
    if (CollectionUtils.isEmpty(shoppingCartData.getShoppingCartItems())) {
        shoppingCartFacade.deleteShoppingCart(shoppingCartData.getId(), store);
        return "redirect:/shop";
    }
    return Constants.REDIRECT_PREFIX + "/shop/cart/shoppingCart.html";
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Customer (com.salesmanager.core.model.customer.Customer)71 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)33 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)32 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 Language (com.salesmanager.core.model.reference.language.Language)26 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)17 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)17 ConversionException (com.salesmanager.core.business.exception.ConversionException)16 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)16 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)12 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)12 Authentication (org.springframework.security.core.Authentication)12 Date (java.util.Date)11 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)10 ArrayList (java.util.ArrayList)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 Product (com.salesmanager.core.model.catalog.product.Product)9 Country (com.salesmanager.core.model.reference.country.Country)9