Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2013 Broadleaf Commerce
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.broadleafcommerce.core.catalog.service;
 
 
 import java.util.List;
 import java.util.Set;
 
 @Service("blRelatedProductsService")
 /*
  * Service that provides method for finding a product's related products.   
  */
 public class RelatedProductsServiceImpl implements RelatedProductsService {
     
     @Resource(name="blCategoryDao")
     protected CategoryDao categoryDao;
 
     @Resource(name="blProductDao")
     protected ProductDao productDao;
     
     @Resource(name="blCatalogService")
     protected CatalogService catalogService;
 
     @Override
     public List<? extends PromotableProductfindRelatedProducts(RelatedProductDTO relatedProductDTO) {
         Product product = lookupProduct(relatedProductDTO);
         Category category = lookupCategory(relatedProductDTO);      
         
         if (..equals(relatedProductDTO.getType())) {
             return buildFeaturedProductsList(productcategoryrelatedProductDTO);
         } else if (..equals(relatedProductDTO.getType())) {
             return buildCrossSaleProductsList(productcategoryrelatedProductDTO);
         } else if (..equals(relatedProductDTO.getType())) {
             return buildUpSaleProductsList(productcategoryrelatedProductDTO);
         } else {
             throw new IllegalArgumentException("RelatedProductType " + relatedProductDTO.getType() + " not supported.");
         }       
     }
    
    
Returns the featured products for the past in product/category

Parameters:
product
category
relatedProductDTO
Returns:
 
     protected List<? extends PromotableProductbuildFeaturedProductsList(Product productCategory categoryRelatedProductDTO relatedProductDTO) {
         List<FeaturedProductreturnFeaturedProducts = null;
         
         if (product != null) {
             category = product.getDefaultCategory();
         }
         
         if (category != null) {
             if (relatedProductDTO.isCumulativeResults()) {
                 returnFeaturedProducts = category.getCumulativeFeaturedProducts();
             } else {
                 returnFeaturedProducts = category.getFeaturedProducts();
             }
         }
         
         removeCurrentProductFromReturnList(productreturnFeaturedProducts);
         returnFeaturedProducts = (List<FeaturedProduct>)removeDuplicatesFromList(returnFeaturedProducts);
         
         return resizeList(returnFeaturedProductsrelatedProductDTO.getQuantity());
     }
     
     private List<? extends PromotableProductremoveDuplicatesFromList(List <? extends PromotableProductreturnPromotableProducts) {
         Set<PromotableProductproductSet = new LinkedHashSet<PromotableProduct>();
        Set<ProductrelatedProductSet = new LinkedHashSet<Product>();
        if (returnPromotableProducts != null) {
            for(PromotableProduct p : returnPromotableProducts) {
                if (!relatedProductSet.contains(p.getRelatedProduct())){
                    productSet.add(p);
                    relatedProductSet.add(p.getRelatedProduct());
                }
            }
        } else {
            return null;
        }
        returnPromotableProducts.clear();
        returnPromotableProducts.addAll(new ArrayList(productSet));
        return returnPromotableProducts;
    }
     private void removeCurrentProductFromReturnList(Product product,
            List<? extends PromotableProductreturnPromotableProducts) {
        if (product != null && returnPromotableProducts != null) {
            Iterator<? extends PromotableProductproductIterator = returnPromotableProducts.iterator();
            while (productIterator.hasNext()) {
                PromotableProduct promotableProduct = productIterator.next();
                if (product.getId().equals(promotableProduct.getRelatedProduct().getId())) {
                    productIterator.remove();
                }               
            }           
        }
    }
    
    
Returns the upSale products for the past in product/category

Parameters:
product
category
relatedProductDTO
Returns:
    protected List<? extends PromotableProductbuildUpSaleProductsList(Product productCategory categoryRelatedProductDTO relatedProductDTO) {
        List<? extends PromotableProductreturnUpSaleProducts = null;
        
        if (product != null) {
            if (relatedProductDTO.isCumulativeResults()) {
                returnUpSaleProducts = product.getCumulativeUpSaleProducts();
            } else {
                returnUpSaleProducts = product.getUpSaleProducts();
            }
        } else if (category != null) {
            if (relatedProductDTO.isCumulativeResults()) {
                returnUpSaleProducts = category.getCumulativeUpSaleProducts();
            } else {
                returnUpSaleProducts = category.getUpSaleProducts();
            }
        }
        removeCurrentProductFromReturnList(productreturnUpSaleProducts);
        returnUpSaleProducts = removeDuplicatesFromList(returnUpSaleProducts);
        return resizeList(returnUpSaleProductsrelatedProductDTO.getQuantity());
    }
    
    
Returns the crossSale products for the past in product/category

Parameters:
product
category
relatedProductDTO
Returns:
    protected List<? extends PromotableProductbuildCrossSaleProductsList(Product productCategory categoryRelatedProductDTO relatedProductDTO) {
        List<? extends PromotableProductcrossSaleProducts = null;
        
        if (product != null) {
            if (relatedProductDTO.isCumulativeResults()) {
                crossSaleProducts = product.getCumulativeCrossSaleProducts();
            } else {
                crossSaleProducts = product.getCrossSaleProducts();
            }
        } else if (category != null) {
            if (relatedProductDTO.isCumulativeResults()) {
                crossSaleProducts = category.getCumulativeCrossSaleProducts();
            } else {
                crossSaleProducts = category.getCrossSaleProducts();
            }
        }
        removeCurrentProductFromReturnList(productcrossSaleProducts);
        crossSaleProducts = removeDuplicatesFromList(crossSaleProducts);
        
        return resizeList(crossSaleProductsrelatedProductDTO.getQuantity());
    }   
    
    
Resizes the list to match the passed in quantity. If the quantity is greater than the size of the list or null, the originalList is returned.

Parameters:
originalList
qty
Returns:
    protected List<? extends PromotableProductresizeList(List<? extends PromotableProductoriginalListInteger qty) {
        if (qty != null && originalList != null && originalList.size() > qty) {
            return originalList.subList(0, qty);
        } else {
            return originalList;
        }
    }
    
    protected Product lookupProduct(RelatedProductDTO relatedProductDTO) {
        if (relatedProductDTO.getProductId() != null) {
            return .readProductById(relatedProductDTO.getProductId());
        } else {
            return null;
        }
    }
    
    protected Category lookupCategory(RelatedProductDTO relatedProductDTO) {
        if (relatedProductDTO.getCategoryId() != null) {
            return .readCategoryById(relatedProductDTO.getCategoryId());
        } else {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X