Search in sources :

Example 1 with InventoryServiceExtensionHandler

use of org.broadleafcommerce.core.inventory.service.InventoryServiceExtensionHandler in project BroadleafCommerce by BroadleafCommerce.

the class UncacheableDataProcessor method addProductInventoryData.

protected void addProductInventoryData(Map<String, Object> attrMap, BroadleafTemplateContext context) {
    List<Long> outOfStockProducts = new ArrayList<>();
    List<Long> outOfStockSkus = new ArrayList<>();
    Set<Product> allProducts = new HashSet<>();
    Set<Sku> allSkus = new HashSet<>();
    Set<Product> products = (Set<Product>) context.getVariable("blcAllDisplayedProducts");
    Set<Sku> skus = (Set<Sku>) context.getVariable("blcAllDisplayedSkus");
    if (!CollectionUtils.isEmpty(products)) {
        allProducts.addAll(products);
    }
    if (!CollectionUtils.isEmpty(skus)) {
        allSkus.addAll(skus);
    }
    extensionManager.getProxy().modifyProductListForInventoryCheck(context, allProducts, allSkus);
    if (!allProducts.isEmpty()) {
        for (Product product : allProducts) {
            if (product.getDefaultSku() != null) {
                Boolean qtyAvailable = inventoryService.isAvailable(product.getDefaultSku(), 1);
                if (qtyAvailable != null && !qtyAvailable) {
                    outOfStockProducts.add(product.getId());
                } else {
                    InventoryServiceExtensionHandler handler = inventoryServiceExtensionManager.getProxy();
                    ExtensionResultHolder<Boolean> holder = new ExtensionResultHolder<>();
                    handler.isProductBundleAvailable(product, 1, holder);
                    Boolean available = holder.getResult();
                    if (available != null && !available) {
                        outOfStockProducts.add(product.getId());
                    }
                }
            }
        }
    } else {
        if (!allSkus.isEmpty()) {
            Map<Sku, Integer> inventoryAvailable = inventoryService.retrieveQuantitiesAvailable(allSkus);
            for (Map.Entry<Sku, Integer> entry : inventoryAvailable.entrySet()) {
                if (entry.getValue() == null || entry.getValue() < 1) {
                    outOfStockSkus.add(entry.getKey().getId());
                }
            }
        }
    }
    attrMap.put("outOfStockProducts", outOfStockProducts);
    attrMap.put("outOfStockSkus", outOfStockSkus);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) InventoryServiceExtensionHandler(org.broadleafcommerce.core.inventory.service.InventoryServiceExtensionHandler) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashMap(java.util.HashMap) Map(java.util.Map) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 InventoryServiceExtensionHandler (org.broadleafcommerce.core.inventory.service.InventoryServiceExtensionHandler)1