/*
   * This controller method is for demonstration purposes only. It contains a call to
   * catalogService.findActiveProductsByCategory, which may return a large list. A
   * more performant solution would be to utilize data paging techniques.
   */
  protected boolean addProductsToModel(
      HttpServletRequest request, ModelMap model, CatalogSort catalogSort) {
    boolean productFound = false;

    String productId = request.getParameter("productId");
    Product product = null;
    try {
      product = catalogService.findProductById(new Long(productId));
    } catch (Exception e) {
      // If product is not found, return all values in category
    }

    if (product != null && product.isActive()) {
      productFound = validateProductAndAddToModel(product, model);
      addRatingSummaryToModel(productId, model);
    } else {
      Category currentCategory = (Category) model.get("currentCategory");
      List<Product> productList =
          catalogService.findActiveProductsByCategory(currentCategory, SystemTime.asDate());
      SearchFilterUtil.filterProducts(
          productList, request.getParameterMap(), new String[] {"manufacturer", "sku.salePrice"});

      if ((catalogSort != null) && (catalogSort.getSort() != null)) {
        populateProducts(productList, currentCategory);
        model.addAttribute("displayProducts", sortProducts(catalogSort, productList));
      } else {
        catalogSort = new CatalogSort();
        catalogSort.setSort("featured");
        populateProducts(productList, currentCategory);
        model.addAttribute("displayProducts", sortProducts(catalogSort, productList));
      }
    }

    return productFound;
  }
  public Order setUpAnonymousCartWithGiftWrap() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    Sku newSku = addTestSku("Small Plastic Crate", "Plastic Crate", "Crates");
    Sku newInactiveSku = addTestSku("Small Red Plastic Crate", "Plastic Crate", "Crates");
    Sku giftWrapSku = addTestSku("Gift Box", "Gift Box", "Gift Wraps");

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    List<OrderItem> addedItems = new ArrayList<OrderItem>();
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2));
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newInactiveSku.getId(), newProduct.getId(), newCategory.getId(), 2));

    orderService.addGiftWrapItemToOrder(
        order, createGiftWrapOrderItemRequest(giftWrapSku, 1, addedItems));

    return order;
  }
 protected int findProductPositionInList(Product product, List<Product> products) {
   for (int i = 0; i < products.size(); i++) {
     Product currentProduct = products.get(i);
     if (product.getId().equals(currentProduct.getId())) {
       return i + 1;
     }
   }
   return 0;
 }
 public DiscreteOrderItemRequest createDiscreteOrderItemRequest(Sku sku, int quantity) {
   Product product = sku.getAllParentProducts().get(0);
   DiscreteOrderItemRequest request = new DiscreteOrderItemRequest();
   request.setSku(sku);
   request.setQuantity(quantity);
   request.setProduct(product);
   request.setCategory(product.getDefaultCategory());
   return request;
 }
  private void populateProducts(List<Product> productList, Category currentCategory) {

    for (FeaturedProduct featuredProduct : currentCategory.getFeaturedProducts()) {
      for (Product product : productList) {
        if ((product.equals(featuredProduct.getProduct()))) {
          product.setPromoMessage(featuredProduct.getPromotionMessage());
        }
      }
    }
  }
  public GiftWrapOrderItemRequest createGiftWrapOrderItemRequest(
      Sku sku, int quantity, List<OrderItem> wrappedItems) {
    Product product = sku.getAllParentProducts().get(0);
    GiftWrapOrderItemRequest request = new GiftWrapOrderItemRequest();
    request.setSku(sku);
    request.setQuantity(quantity);
    request.setProduct(product);
    request.setCategory(product.getDefaultCategory());
    request.setWrappedItems(wrappedItems);

    return request;
  }
  public Order setUpNamedOrder() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Sku newSku = addTestSku("Small Cube Box", "Cube Box", "Boxes");

    Order order = orderService.createNamedOrderForCustomer("Boxes Named Order", customer);

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    orderService.addSkuToOrder(
        order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2);

    return order;
  }
  public Order setUpExistingCart(Customer customer) throws PricingException {
    Sku newSku = addTestSku("Large Plastic Crate", "Plastic Crate", "Crates");
    Sku newOtherSku = addTestSku("Large Red Plastic Crate", "Plastic Crate", "Crates");

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    Order order = cartService.createNewCartForCustomer(customer);

    orderService.addSkuToOrder(
        order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2);
    orderService.addSkuToOrder(
        order.getId(), newOtherSku.getId(), newProduct.getId(), newCategory.getId(), 2);

    return order;
  }
Esempio n. 9
0
 /*
  * The Heat Clinic does not support adding products with required product options from a category browse page
  * when JavaScript is disabled. When this occurs, we will redirect the user to the full product details page
  * for the given product so that the required options may be chosen.
  */
 @RequestMapping(
     value = "/add",
     produces = {"text/html", "*/*"})
 public String add(
     HttpServletRequest request,
     HttpServletResponse response,
     Model model,
     RedirectAttributes redirectAttributes,
     @ModelAttribute("addToCartItem") AddToCartItem addToCartItem)
     throws IOException, PricingException, AddToCartException {
   try {
     return super.add(request, response, model, addToCartItem);
   } catch (AddToCartException e) {
     Product product = catalogService.findProductById(addToCartItem.getProductId());
     return "redirect:" + product.getUrl();
   }
 }
  public Order setUpExistingCartWithInactiveSkuAndInactiveBundle(Customer customer)
      throws PricingException {
    Sku newSku = addTestSku("Large Plastic Crate", "Plastic Crate", "Crates");
    Sku newInactiveSku = addTestSku("Large Red Plastic Crate", "Plastic Crate", "Crates", false);

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    Order order = cartService.createNewCartForCustomer(customer);

    orderService.addSkuToOrder(
        order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2);
    orderService.addSkuToOrder(
        order.getId(), newInactiveSku.getId(), newProduct.getId(), newCategory.getId(), 2);

    orderService.addBundleItemToOrder(order, createBundleOrderItemRequest());
    orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithInactiveSku());

    return order;
  }
  public Order setUpAnonymousCartWithBundleGiftWrapReferringToRootItems() throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());

    Order order = cartService.createNewCartForCustomer(customer);

    Sku newSku = addTestSku("Small Plastic Bowl", "Plastic Bowl", "Bowls");
    Sku newActiveSku = addTestSku("Small Red Plastic Bowl", "Plastic Bowl", "Bowls");

    Product newProduct = newSku.getAllParentProducts().get(0);
    Category newCategory = newProduct.getDefaultCategory();

    List<OrderItem> addedItems = new ArrayList<OrderItem>();
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newSku.getId(), newProduct.getId(), newCategory.getId(), 2));
    addedItems.add(
        orderService.addSkuToOrder(
            order.getId(), newActiveSku.getId(), newProduct.getId(), newCategory.getId(), 2));

    BundleOrderItem newBundle =
        (BundleOrderItem)
            orderService.addBundleItemToOrder(order, createBundleOrderItemRequestWithGiftWrap());
    GiftWrapOrderItem giftItem = null;
    for (DiscreteOrderItem addedItem : newBundle.getDiscreteOrderItems()) {
      if (addedItem instanceof GiftWrapOrderItem) {
        giftItem = (GiftWrapOrderItem) addedItem;
      }
    }
    for (OrderItem addedItem : addedItems) {
      addedItem.setGiftWrapOrderItem(giftItem);
    }
    giftItem.getWrappedItems().addAll(addedItems);
    order = orderService.save(order, false);

    return order;
  }
 protected boolean itemMatches(
     Sku item1Sku,
     Product item1Product,
     Map<String, OrderItemAttribute> item1Attributes,
     OrderItemRequestDTO item2) {
   // Must match on SKU and options
   if (item1Sku != null && item2.getSkuId() != null) {
     if (item1Sku.getId().equals(item2.getSkuId())) {
       return true;
     }
   } else {
     if (item1Product != null && item2.getProductId() != null) {
       if (item1Product.getId().equals(item2.getProductId())) {
         return compareAttributes(item1Attributes, item2);
       }
     }
   }
   return false;
 }
  /*
   * This controller method is for demonstration purposes only. It contains a call to
   * catalogService.findActiveProductsByCategory, which may return a large list. A
   * more performant solution would be to utilize data paging techniques.
   */
  protected boolean validateProductAndAddToModel(Product product, ModelMap model) {
    Category currentCategory = (Category) model.get("currentCategory");
    Category rootCategory = (Category) model.get("rootCategory");
    int productPosition = 0;

    List<Product> productList =
        catalogService.findActiveProductsByCategory(currentCategory, SystemTime.asDate());
    if (productList != null) {
      populateProducts(productList, currentCategory);
      model.addAttribute("products", productList);
    }
    productPosition = findProductPositionInList(product, productList);
    if (productPosition == 0) {
      // look for product in its default category and override category from request URL
      currentCategory = product.getDefaultCategory();
      if (currentCategory.isActive()) {
        model.put("currentCategory", currentCategory);
        productList =
            catalogService.findActiveProductsByCategory(currentCategory, SystemTime.asDate());
        if (productList != null) {
          model.put("currentProducts", productList);
        }
        String url = currentCategory.getGeneratedUrl();

        // override category list settings using this products default
        List<Long> categoryIdList =
            catalogService.getChildCategoryURLMapByCategoryId(rootCategory.getId()).get(url);
        List<Category> categoryList = null;
        if (categoryIdList != null) {
          categoryList = new ArrayList<Category>(categoryIdList.size());
          for (Long id : categoryIdList) {
            categoryList.add(catalogService.findCategoryById(id));
          }
        }
        if (categoryList != null
            && !addCategoryListToModel(categoryList, rootCategory, url, model)) {
          productPosition = findProductPositionInList(product, productList);
        }
      }
    }

    if (productPosition != 0) {
      model.addAttribute("productError", false);
      model.addAttribute("currentProduct", product);
      model.addAttribute("productPosition", productPosition);
      if (productPosition != 1) {
        model.addAttribute("previousProduct", productList.get(productPosition - 2));
      }
      if (productPosition < productList.size()) {
        model.addAttribute("nextProduct", productList.get(productPosition));
      }
      model.addAttribute("totalProducts", productList.size());
    } else {
      model.addAttribute("productError", true);
    }

    WishlistRequest wishlistRequest = new WishlistRequest();
    wishlistRequest.setAddCategoryId(currentCategory.getId());
    wishlistRequest.setAddProductId(product.getId());
    wishlistRequest.setQuantity(1);
    wishlistRequest.setAddSkuId(((ProductSku) product).getSku().getId());
    model.addAttribute("wishlistRequest", wishlistRequest);

    return (productPosition != 0);
  }