Search in sources :

Example 46 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.

the class AdminCatalogServiceImpl method generateSkusFromProduct.

@Override
public Integer generateSkusFromProduct(Long productId) {
    Product product = catalogService.findProductById(productId);
    if (CollectionUtils.isEmpty(product.getProductOptions())) {
        return -1;
    }
    List<List<ProductOptionValue>> allPermutations = generatePermutations(0, new ArrayList<ProductOptionValue>(), product.getProductOptions());
    // return -2 to indicate that one of the Product Options used in Sku generation has no Allowed Values
    if (allPermutations == null) {
        return -2;
    }
    LOG.info("Total number of permutations: " + allPermutations.size());
    LOG.info(allPermutations);
    // determine the permutations that I already have Skus for
    List<List<ProductOptionValue>> previouslyGeneratedPermutations = new ArrayList<List<ProductOptionValue>>();
    if (CollectionUtils.isNotEmpty(product.getAdditionalSkus())) {
        for (Sku additionalSku : product.getAdditionalSkus()) {
            if (CollectionUtils.isNotEmpty(additionalSku.getProductOptionValues())) {
                previouslyGeneratedPermutations.add(additionalSku.getProductOptionValues());
            }
        }
    }
    List<List<ProductOptionValue>> permutationsToGenerate = new ArrayList<List<ProductOptionValue>>();
    for (List<ProductOptionValue> permutation : allPermutations) {
        boolean previouslyGenerated = false;
        for (List<ProductOptionValue> generatedPermutation : previouslyGeneratedPermutations) {
            if (isSamePermutation(permutation, generatedPermutation)) {
                previouslyGenerated = true;
                break;
            }
        }
        if (!previouslyGenerated) {
            permutationsToGenerate.add(permutation);
        }
    }
    int numPermutationsCreated = 0;
    if (extensionManager != null) {
        ExtensionResultHolder<Integer> result = new ExtensionResultHolder<Integer>();
        ExtensionResultStatusType resultStatusType = extensionManager.getProxy().persistSkuPermutation(product, permutationsToGenerate, result);
        if (ExtensionResultStatusType.HANDLED == resultStatusType) {
            numPermutationsCreated = result.getResult();
        }
    }
    return numPermutationsCreated;
}
Also used : ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) List(java.util.List) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 47 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.

the class DefaultAdminCatalogExtensionHandler method persistSkuPermutation.

/**
 * Simply iterates through the permutations based on the product options and persists them
 * as new {@link org.broadleafcommerce.core.catalog.domain.Sku} instances in the {@link org.broadleafcommerce.core.catalog.domain.Product}
 *
 * @param product
 * @param permutationsToGenerate
 * @param erh
 * @return
 */
@Override
public ExtensionResultStatusType persistSkuPermutation(Product product, List<List<ProductOptionValue>> permutationsToGenerate, ExtensionResultHolder<Integer> erh) {
    int numPermutationsCreated = 0;
    // For each permutation, I need them to map to a specific Sku
    for (List<ProductOptionValue> permutation : permutationsToGenerate) {
        if (permutation.isEmpty())
            continue;
        Sku permutatedSku = catalogService.createSku();
        permutatedSku.setProduct(product);
        permutatedSku.setProductOptionValues(permutation);
        permutatedSku = catalogService.saveSku(permutatedSku);
        product.getAdditionalSkus().add(permutatedSku);
        numPermutationsCreated++;
    }
    if (numPermutationsCreated != 0) {
        catalogService.saveProduct(product);
    }
    erh.setResult(numPermutationsCreated);
    return ExtensionResultStatusType.HANDLED;
}
Also used : ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 48 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.

the class ProductDaoImpl method getCriteriaForActiveProducts.

protected CriteriaQuery<Product> getCriteriaForActiveProducts(Date currentDate, Long lastId) {
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    // The root of our search is Product
    Root<ProductImpl> product = criteria.from(ProductImpl.class);
    // We need to filter on active date on the sku
    Join<Product, Sku> sku = product.join("defaultSku");
    product.fetch("defaultSku");
    // Product objects are what we want back
    criteria.select(product);
    // Ensure the product is currently active
    List<Predicate> restrictions = new ArrayList<Predicate>();
    attachActiveRestriction(currentDate, product, sku, restrictions);
    if (lastId != null) {
        restrictions.add(builder.gt(product.get("id").as(Long.class), lastId));
    }
    // Add the restrictions to the criteria query
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    // Add ordering so that paginated queries are consistent
    criteria.orderBy(builder.asc(product.get("id")));
    return criteria;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Predicate(javax.persistence.criteria.Predicate)

Example 49 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.

the class ProductDaoImpl method readFilteredActiveProductsByCategoryInternal.

protected List<Product> readFilteredActiveProductsByCategoryInternal(Long categoryId, Date currentDate, SearchCriteria searchCriteria) {
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    // The root of our search is Category since we are browsing
    Root<CategoryProductXrefImpl> productXref = criteria.from(CategoryProductXrefImpl.class);
    // We want to filter on attributes from product and sku
    Join<CategoryProductXref, Product> product = productXref.join("product");
    Join<Product, Sku> sku = product.join("defaultSku");
    Join<CategoryProductXref, Category> category = productXref.join("category");
    // Product objects are what we want back
    criteria.select(product);
    // We only want results from the determine category
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(category.get("id").in(sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId)));
    attachSearchCriteria(searchCriteria, product, sku, restrictions);
    attachActiveRestriction(currentDate, product, sku, restrictions);
    attachOrderBy(searchCriteria, product, sku, criteria);
    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<Product> typedQuery = em.createQuery(criteria);
    return typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Category(org.broadleafcommerce.core.catalog.domain.Category) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Predicate(javax.persistence.criteria.Predicate) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 50 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.

the class ProductOptionDaoImpl method readSkuIdsForProductOptionValues.

@Override
public List<Long> readSkuIdsForProductOptionValues(Long productId, String attributeName, String attributeValue, List<Long> possibleSkuIds) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Sku> criteria = cb.createQuery(Sku.class);
    Root<SkuProductOptionValueXrefImpl> root = criteria.from(SkuProductOptionValueXrefImpl.class);
    criteria.select(root.get("sku").as(Sku.class));
    List<Predicate> predicates = new ArrayList<>();
    // restrict to skus that match the product
    predicates.add(root.get("sku").get("product").get("id").in(sandBoxHelper.mergeCloneIds(ProductImpl.class, productId)));
    // restrict to skus that match the attributeName
    predicates.add(cb.equal(root.get("productOptionValue").get("productOption").get("attributeName"), attributeName));
    // restrict to skus that match the attributeValue
    predicates.add(cb.equal(root.get("productOptionValue").get("attributeValue"), attributeValue));
    // restrict to skus that have ids within the given list of skus ids
    if (CollectionUtils.isNotEmpty(possibleSkuIds)) {
        possibleSkuIds = sandBoxHelper.mergeCloneIds(SkuImpl.class, possibleSkuIds.toArray(new Long[possibleSkuIds.size()]));
        Predicate skuDomainPredicate = buildSkuDomainPredicate(cb, root.get("sku").get("id"), possibleSkuIds);
        if (skuDomainPredicate != null) {
            predicates.add(skuDomainPredicate);
        }
    }
    // restrict archived values
    attachArchivalConditionIfPossible(SkuProductOptionValueXrefImpl.class, root, cb, predicates);
    attachArchivalConditionIfPossible(SkuImpl.class, root.get("sku"), cb, predicates);
    criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    TypedQuery<Sku> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
    List<Sku> candidateSkus = query.getResultList();
    return filterCandidateSkusForArchivedStatus(candidateSkus);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) ArrayList(java.util.ArrayList) SkuProductOptionValueXrefImpl(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXrefImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Sku (org.broadleafcommerce.core.catalog.domain.Sku)80 Product (org.broadleafcommerce.core.catalog.domain.Product)34 ArrayList (java.util.ArrayList)27 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)22 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)18 Money (org.broadleafcommerce.common.money.Money)16 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)16 Order (org.broadleafcommerce.core.order.domain.Order)15 Category (org.broadleafcommerce.core.catalog.domain.Category)13 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)12 Test (org.testng.annotations.Test)12 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)10 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)10 Transactional (org.springframework.transaction.annotation.Transactional)10 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)9 HashMap (java.util.HashMap)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8