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;
}
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;
}
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;
}
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();
}
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);
}
Aggregations