Search in sources :

Example 6 with ProductOption

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

the class AdminCatalogServiceImpl method generatePermutations.

/**
 * Generates all the possible permutations for the combinations of given ProductOptions
 * @param currentTypeIndex
 * @param currentPermutation
 * @param options
 * @return a list containing all of the possible combinations of ProductOptionValues based on grouping by the ProductOptionValue
 */
public List<List<ProductOptionValue>> generatePermutations(int currentTypeIndex, List<ProductOptionValue> currentPermutation, List<ProductOption> options) {
    List<List<ProductOptionValue>> result = new ArrayList<List<ProductOptionValue>>();
    if (currentTypeIndex == options.size()) {
        result.add(currentPermutation);
        return result;
    }
    ProductOption currentOption = options.get(currentTypeIndex);
    List<ProductOptionValue> allowedValues = currentOption.getAllowedValues();
    if (!currentOption.getUseInSkuGeneration()) {
        // This flag means do not generate skus and so do not create permutations for this ProductOption,
        // end it here and return the current list of permutations.
        result.addAll(generatePermutations(currentTypeIndex + 1, currentPermutation, options));
        return result;
    }
    // Check to make sure there is at least 1 Allowed Value, else prevent generation
    if (currentOption.getAllowedValues().isEmpty()) {
        return null;
    }
    for (ProductOptionValue option : allowedValues) {
        List<ProductOptionValue> permutation = new ArrayList<ProductOptionValue>();
        permutation.addAll(currentPermutation);
        permutation.add(option);
        result.addAll(generatePermutations(currentTypeIndex + 1, permutation, options));
    }
    if (allowedValues.size() == 0) {
        // There are still product options left in our array to compute permutations, even though this ProductOption does not have any values associated.
        result.addAll(generatePermutations(currentTypeIndex + 1, currentPermutation, options));
    }
    return result;
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with ProductOption

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

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

Example 9 with ProductOption

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

the class OrderItemServiceImpl method updateDiscreteOrderItem.

@Override
public OrderItem updateDiscreteOrderItem(OrderItem item, final DiscreteOrderItemRequest itemRequest) {
    List<ProductOption> productOptions = null;
    if (item instanceof DiscreteOrderItem) {
        productOptions = ((DiscreteOrderItem) item).getProduct().getProductOptions();
    } else if (item instanceof BundleOrderItem) {
        productOptions = ((BundleOrderItem) item).getProduct().getProductOptions();
    }
    List<String> removeKeys = new ArrayList<String>();
    if (productOptions != null && itemRequest.getItemAttributes() != null) {
        for (String name : itemRequest.getItemAttributes().keySet()) {
            // if  option.getProductOptionValidationType()  is null then it might change the sku, so we dont allow those
            for (ProductOption option : productOptions) {
                if (option.getAttributeName().equals(name) && option.getProductOptionValidationStrategyType() == null) {
                    removeKeys.add(name);
                    break;
                }
            }
        }
    }
    for (String name : removeKeys) {
        itemRequest.getItemAttributes().remove(name);
    }
    populateProductOptionAttributes(item, itemRequest.getItemAttributes());
    applyAdditionalOrderItemProperties(item);
    return item;
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ArrayList(java.util.ArrayList)

Aggregations

ProductOption (org.broadleafcommerce.core.catalog.domain.ProductOption)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Product (org.broadleafcommerce.core.catalog.domain.Product)3 ProductOptionXref (org.broadleafcommerce.core.catalog.domain.ProductOptionXref)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)2 List (java.util.List)1 Map (java.util.Map)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ProductOptionXrefImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionXrefImpl)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)1 Order (org.broadleafcommerce.core.order.domain.Order)1 OrderItemAttribute (org.broadleafcommerce.core.order.domain.OrderItemAttribute)1 RequiredAttributeNotProvidedException (org.broadleafcommerce.core.order.service.exception.RequiredAttributeNotProvidedException)1