/*
   * 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;
  }
  @SuppressWarnings("unchecked")
  private List<Product> sortProducts(CatalogSort catalogSort, List<Product> displayProducts) {
    if (catalogSort.getSort().equals("priceL")) {
      Collections.sort(displayProducts, new BeanComparator("sku.salePrice", new NullComparator()));
    } else if (catalogSort.getSort().equals("priceH")) {
      Collections.sort(
          displayProducts,
          new ReverseComparator(new BeanComparator("sku.salePrice", new NullComparator())));
    } else if (catalogSort.getSort().equals("manufacturerA")) {
      Collections.sort(displayProducts, new BeanComparator("manufacturer", new NullComparator()));
    } else if (catalogSort.getSort().equals("manufacturerZ")) {
      Collections.sort(
          displayProducts,
          new ReverseComparator(new BeanComparator("manufacturer", new NullComparator())));
    } else if (catalogSort.getSort().equals("featured")) {
      Collections.sort(
          displayProducts,
          new ReverseComparator(new BeanComparator("promoMessage", new NullComparator())));
    }

    return displayProducts;
  }