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