Search in sources :

Example 56 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder 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

ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)56 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Set (java.util.Set)4 File (java.io.File)3 Serializable (java.io.Serializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Site (org.broadleafcommerce.common.site.domain.Site)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3