/* * 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; }
/* * 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); }