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