use of org.broadleafcommerce.core.catalog.domain.ProductOptionXref in project BroadleafCommerce by BroadleafCommerce.
the class ProductOptionDaoImpl method getProductIdsUsingProductOptionByIdQuery.
private TypedQuery<Long> getProductIdsUsingProductOptionByIdQuery(Long productOptionId, boolean count) {
// Set up the criteria query that specifies we want to return Products
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
// The root of our search is ProductOptionXref
Root<ProductOptionXrefImpl> productOptionXref = criteria.from(ProductOptionXrefImpl.class);
Join<ProductOptionXref, Product> product = productOptionXref.join("product");
Join<ProductOptionXref, ProductOption> productOption = productOptionXref.join("productOption");
if (count) {
criteria.select(builder.count(product));
} else {
// Product IDs are what we want back
criteria.select(product.get("id").as(Long.class));
}
criteria.distinct(true);
List<Predicate> restrictions = new ArrayList<Predicate>();
restrictions.add(productOption.get("id").in(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, productOptionId)));
// Execute the query with the restrictions
criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
return em.createQuery(criteria);
}
use of org.broadleafcommerce.core.catalog.domain.ProductOptionXref in project BroadleafCommerce by BroadleafCommerce.
the class UncacheableDataProcessor method addCartData.
protected void addCartData(Map<String, Object> attrMap) {
Order cart = CartState.getCart();
int cartQty = 0;
List<Long> cartItemIdsWithOptions = new ArrayList<>();
List<Long> cartItemIdsWithoutOptions = new ArrayList<>();
if (cart != null && cart.getOrderItems() != null) {
cartQty = cart.getItemCount();
for (OrderItem item : cart.getOrderItems()) {
if (item instanceof SkuAccessor) {
Sku sku = ((SkuAccessor) item).getSku();
if (sku != null && sku.getProduct() != null && item.getParentOrderItem() == null) {
if (useSku) {
cartItemIdsWithoutOptions.add(sku.getId());
} else {
Product product = sku.getProduct();
List<ProductOptionXref> optionXrefs = product.getProductOptionXrefs();
if (optionXrefs == null || optionXrefs.isEmpty()) {
cartItemIdsWithoutOptions.add(product.getId());
} else {
cartItemIdsWithOptions.add(product.getId());
}
}
}
}
}
}
attrMap.put("cartItemCount", cartQty);
attrMap.put("cartItemIdsWithOptions", cartItemIdsWithOptions);
attrMap.put("cartItemIdsWithoutOptions", cartItemIdsWithoutOptions);
}
use of org.broadleafcommerce.core.catalog.domain.ProductOptionXref in project BroadleafCommerce by BroadleafCommerce.
the class ValidateAddRequestActivity method findMatchingSku.
protected Sku findMatchingSku(Product product, Map<String, String> attributeValues, ActivityMessages messages) throws RequiredAttributeNotProvidedException {
Map<String, String> attributesRelevantToFindMatchingSku = new HashMap<>();
// Verify that required product-option values were set.
if (product != null) {
for (ProductOptionXref productOptionXref : ListUtils.emptyIfNull(product.getProductOptionXrefs())) {
ProductOption productOption = productOptionXref.getProductOption();
String attributeName = productOption.getAttributeName();
String attributeValue = attributeValues.get(attributeName);
boolean isRequired = productOption.getRequired();
boolean hasStrategy = productOptionValidationService.hasProductOptionValidationStrategy(productOption);
boolean isAddOrNoneTypes = productOptionValidationService.isAddOrNoneType(productOption);
if (shouldValidateWithException(isRequired, isAddOrNoneTypes, attributeValue, hasStrategy)) {
productOptionValidationService.validate(productOption, attributeValue);
}
if (hasStrategy && !isAddOrNoneTypes) {
// we need to validate; however, we will not error out
productOptionValidationService.validateWithoutException(productOption, attributeValue, messages);
}
if (productOption.getUseInSkuGeneration()) {
attributesRelevantToFindMatchingSku.put(attributeName, attributeValue);
}
}
return findMatchingSku(product, attributesRelevantToFindMatchingSku);
}
return null;
}
use of org.broadleafcommerce.core.catalog.domain.ProductOptionXref in project BroadleafCommerce by BroadleafCommerce.
the class ValidateProductOptionsActivity method execute.
@Override
public ProcessContext<CheckoutSeed> execute(ProcessContext<CheckoutSeed> context) throws Exception {
if (!useSku) {
Order order = context.getSeedData().getOrder();
List<DiscreteOrderItem> orderItems = getOrderItems(order);
for (DiscreteOrderItem discreteOI : orderItems) {
Map<String, OrderItemAttribute> attributeValues = discreteOI.getOrderItemAttributes();
Product product = discreteOI.getProduct();
if (product != null) {
for (ProductOptionXref productOptionXref : ListUtils.emptyIfNull(product.getProductOptionXrefs())) {
ProductOption productOption = productOptionXref.getProductOption();
String attributeName = productOption.getAttributeName();
OrderItemAttribute attribute = attributeValues.get(attributeName);
String attributeValue = (attribute != null) ? attribute.getValue() : null;
boolean isRequired = productOption.getRequired();
boolean hasStrategy = productOptionValidationService.hasProductOptionValidationStrategy(productOption);
boolean isAddOrNoneType = productOptionValidationService.isAddOrNoneType(productOption);
boolean isSubmitType = productOptionValidationService.isSubmitType(productOption);
if (isMissingRequiredAttribute(isRequired, hasStrategy, isAddOrNoneType, isSubmitType, attributeValue)) {
String message = "Unable to validate cart, product (" + product.getId() + ") required" + " attribute was not provided: " + attributeName;
throw new RequiredAttributeNotProvidedException(message, attributeName, String.valueOf(product.getId()));
}
boolean hasValidationType = productOption.getProductOptionValidationType() != null;
if (shouldValidateWithException(hasValidationType, hasStrategy, isAddOrNoneType, isSubmitType)) {
productOptionValidationService.validate(productOption, attributeValue);
}
if (hasStrategy && !(isAddOrNoneType || isSubmitType)) {
// we need to validate however, we will not error out
ActivityMessages messages = (ActivityMessages) context;
productOptionValidationService.validateWithoutException(productOption, attributeValue, messages);
}
}
}
}
}
return context;
}
Aggregations