Search in sources :

Example 1 with ProductOptionXref

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Predicate(javax.persistence.criteria.Predicate) ProductOptionXrefImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionXrefImpl)

Example 2 with ProductOptionXref

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);
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) SkuAccessor(org.broadleafcommerce.core.order.domain.SkuAccessor)

Example 3 with ProductOptionXref

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;
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) HashMap(java.util.HashMap) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref)

Example 4 with ProductOptionXref

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;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ActivityMessages(org.broadleafcommerce.core.workflow.ActivityMessages) OrderItemAttribute(org.broadleafcommerce.core.order.domain.OrderItemAttribute) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref) Product(org.broadleafcommerce.core.catalog.domain.Product) RequiredAttributeNotProvidedException(org.broadleafcommerce.core.order.service.exception.RequiredAttributeNotProvidedException)

Aggregations

ProductOptionXref (org.broadleafcommerce.core.catalog.domain.ProductOptionXref)4 Product (org.broadleafcommerce.core.catalog.domain.Product)3 ProductOption (org.broadleafcommerce.core.catalog.domain.ProductOption)3 ArrayList (java.util.ArrayList)2 Order (org.broadleafcommerce.core.order.domain.Order)2 HashMap (java.util.HashMap)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ProductOptionXrefImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionXrefImpl)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)1 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)1 OrderItemAttribute (org.broadleafcommerce.core.order.domain.OrderItemAttribute)1 SkuAccessor (org.broadleafcommerce.core.order.domain.SkuAccessor)1 RequiredAttributeNotProvidedException (org.broadleafcommerce.core.order.service.exception.RequiredAttributeNotProvidedException)1 ActivityMessages (org.broadleafcommerce.core.workflow.ActivityMessages)1