Search in sources :

Example 1 with SkuProductOptionValueXrefImpl

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

the class SkuCustomPersistenceHandler method associateProductOptionValuesToSku.

/**
 * This initially removes all of the product option values that are currently related to the Sku and then re-associates
 * the {@link ProductOptionValue}s
 * @param entity
 * @param adminInstance
 */
protected void associateProductOptionValuesToSku(Entity entity, Sku adminInstance, DynamicEntityDao dynamicEntityDao) {
    // Get the list of product option value ids that were selected from the form
    List<Long> productOptionValueIds = new ArrayList<>();
    for (Property property : getProductOptionProperties(entity)) {
        Long propId = Long.parseLong(property.getValue());
        productOptionValueIds.add(propId);
        property.setIsDirty(true);
    }
    // Only process associations if product option value changes came in via the form
    if (CollectionUtils.isNotEmpty(productOptionValueIds)) {
        // remove the current list of product option values from the Sku
        if (adminInstance.getProductOptionValueXrefs().size() > 0) {
            Iterator<SkuProductOptionValueXref> iterator = adminInstance.getProductOptionValueXrefs().iterator();
            while (iterator.hasNext()) {
                dynamicEntityDao.remove(iterator.next());
            }
            dynamicEntityDao.merge(adminInstance);
        }
        // Associate the product option values from the form with the Sku
        for (Long id : productOptionValueIds) {
            // Simply find the changed ProductOptionValues directly - seems to work better with sandboxing code
            ProductOptionValue pov = (ProductOptionValue) dynamicEntityDao.find(ProductOptionValueImpl.class, id);
            SkuProductOptionValueXref xref = new SkuProductOptionValueXrefImpl(adminInstance, pov);
            xref = dynamicEntityDao.merge(xref);
            adminInstance.getProductOptionValueXrefs().add(xref);
        }
    }
}
Also used : SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) SkuProductOptionValueXrefImpl(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXrefImpl) Property(org.broadleafcommerce.openadmin.dto.Property) ProductOptionValueImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)

Example 2 with SkuProductOptionValueXrefImpl

use of org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXrefImpl 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

ArrayList (java.util.ArrayList)2 SkuProductOptionValueXrefImpl (org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXrefImpl)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)1 ProductOptionValueImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1 SkuProductOptionValueXref (org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref)1 Property (org.broadleafcommerce.openadmin.dto.Property)1