Search in sources :

Example 6 with ProductOptionValue

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

the class ProductOptionsProcessor method addAllProductOptionsToModel.

protected void addAllProductOptionsToModel(Map<String, Object> newModelVars, Product product) {
    List<ProductOption> productOptions = product.getProductOptions();
    List<ProductOptionDTO> dtos = new ArrayList<>();
    for (ProductOption option : productOptions) {
        ProductOptionDTO dto = new ProductOptionDTO();
        dto.setId(option.getId());
        dto.setType(option.getType().getType());
        Map<Long, String> values = new HashMap<>();
        for (ProductOptionValue value : option.getAllowedValues()) {
            values.put(value.getId(), value.getAttributeValue());
        }
        dto.setValues(values);
        dtos.add(dto);
    }
    writeJSONToModel(newModelVars, "allProductOptions", dtos);
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 7 with ProductOptionValue

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

the class i18nUpdateCartServiceExtensionHandler method translateOrderItem.

protected void translateOrderItem(OrderItem orderItem, Sku sku) {
    if (sku != null) {
        orderItem.setName(sku.getName());
        if (sku.getProductOptionValues() != null) {
            for (ProductOptionValue optionValue : sku.getProductOptionValues()) {
                String key = optionValue.getProductOption().getAttributeName();
                OrderItemAttribute attr = orderItem.getOrderItemAttributes().get(key);
                if (attr != null) {
                    attr.setValue(optionValue.getAttributeValue());
                } else {
                    OrderItemAttribute attribute = new OrderItemAttributeImpl();
                    attribute.setName(key);
                    attribute.setValue(optionValue.getAttributeValue());
                    attribute.setOrderItem(orderItem);
                    orderItem.getOrderItemAttributes().put(key, attribute);
                }
            }
        }
    }
}
Also used : ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) OrderItemAttribute(org.broadleafcommerce.core.order.domain.OrderItemAttribute) OrderItemAttributeImpl(org.broadleafcommerce.core.order.domain.OrderItemAttributeImpl)

Example 8 with ProductOptionValue

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

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

the class SkuCustomPersistenceHandler method updateProductOptionFieldsForFetch.

/**
 * Sets the {@link ProductOptionValue}s of the given {@link Sku}s in a list format for display in a ListGrid context.
 *
 * @param records
 * @param payload
 * @return
 */
public void updateProductOptionFieldsForFetch(List<Serializable> records, Entity[] payload) {
    for (int i = 0; i < records.size(); i++) {
        Sku sku = (Sku) records.get(i);
        Entity entity = payload[i];
        List<ProductOptionValue> optionValues = BLCCollectionUtils.collectList(sku.getProductOptionValueXrefs(), new TypedTransformer<ProductOptionValue>() {

            @Override
            public ProductOptionValue transform(Object input) {
                return ((SkuProductOptionValueXref) input).getProductOptionValue();
            }
        });
        for (ProductOptionValue value : optionValues) {
            Property optionProperty = new Property();
            optionProperty.setName(PRODUCT_OPTION_FIELD_PREFIX + value.getProductOption().getId());
            optionProperty.setValue(value.getId().toString());
            optionProperty.setDisplayValue(value.getAttributeValue());
            entity.addProperty(optionProperty);
        }
        if (CollectionUtils.isNotEmpty(optionValues)) {
            entity.addProperty(getConsolidatedOptionProperty(optionValues));
        } else {
            entity.addProperty(getBlankConsolidatedOptionProperty());
        }
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 10 with ProductOptionValue

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

the class AdminCatalogServiceImpl method generateSkusFromProduct.

@Override
public Integer generateSkusFromProduct(Long productId) {
    Product product = catalogService.findProductById(productId);
    if (CollectionUtils.isEmpty(product.getProductOptions())) {
        return -1;
    }
    List<List<ProductOptionValue>> allPermutations = generatePermutations(0, new ArrayList<ProductOptionValue>(), product.getProductOptions());
    // return -2 to indicate that one of the Product Options used in Sku generation has no Allowed Values
    if (allPermutations == null) {
        return -2;
    }
    LOG.info("Total number of permutations: " + allPermutations.size());
    LOG.info(allPermutations);
    // determine the permutations that I already have Skus for
    List<List<ProductOptionValue>> previouslyGeneratedPermutations = new ArrayList<List<ProductOptionValue>>();
    if (CollectionUtils.isNotEmpty(product.getAdditionalSkus())) {
        for (Sku additionalSku : product.getAdditionalSkus()) {
            if (CollectionUtils.isNotEmpty(additionalSku.getProductOptionValues())) {
                previouslyGeneratedPermutations.add(additionalSku.getProductOptionValues());
            }
        }
    }
    List<List<ProductOptionValue>> permutationsToGenerate = new ArrayList<List<ProductOptionValue>>();
    for (List<ProductOptionValue> permutation : allPermutations) {
        boolean previouslyGenerated = false;
        for (List<ProductOptionValue> generatedPermutation : previouslyGeneratedPermutations) {
            if (isSamePermutation(permutation, generatedPermutation)) {
                previouslyGenerated = true;
                break;
            }
        }
        if (!previouslyGenerated) {
            permutationsToGenerate.add(permutation);
        }
    }
    int numPermutationsCreated = 0;
    if (extensionManager != null) {
        ExtensionResultHolder<Integer> result = new ExtensionResultHolder<Integer>();
        ExtensionResultStatusType resultStatusType = extensionManager.getProxy().persistSkuPermutation(product, permutationsToGenerate, result);
        if (ExtensionResultStatusType.HANDLED == resultStatusType) {
            numPermutationsCreated = result.getResult();
        }
    }
    return numPermutationsCreated;
}
Also used : ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) List(java.util.List) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)12 ArrayList (java.util.ArrayList)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 Property (org.broadleafcommerce.openadmin.dto.Property)4 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 HashMap (java.util.HashMap)2 List (java.util.List)2 ProductOption (org.broadleafcommerce.core.catalog.domain.ProductOption)2 SkuProductOptionValueXref (org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 ProductOptionValueImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)1 SkuBundleItem (org.broadleafcommerce.core.catalog.domain.SkuBundleItem)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1