|
| 1 | +package com.commercetools.sdk; |
| 2 | + |
| 3 | +import com.commercetools.api.models.common.CentPrecisionMoney; |
| 4 | +import com.commercetools.api.models.common.LocalizedString; |
| 5 | +import com.commercetools.api.models.common.Money; |
| 6 | +import com.commercetools.api.models.product.ProductProjection; |
| 7 | +import com.commercetools.api.models.product.ProductVariant; |
| 8 | +import com.commercetools.api.models.product_type.*; |
| 9 | +import com.commercetools.importapi.models.common.*; |
| 10 | +import com.commercetools.importapi.models.productdrafts.PriceDraftImport; |
| 11 | +import com.commercetools.importapi.models.productdrafts.ProductDraftImport; |
| 12 | +import com.commercetools.importapi.models.productdrafts.ProductVariantDraftImport; |
| 13 | +import com.commercetools.importapi.models.productvariants.Attribute; |
| 14 | +import com.commercetools.importapi.models.productvariants.BooleanAttribute; |
| 15 | +import com.commercetools.importapi.models.productvariants.NumberAttribute; |
| 16 | + |
| 17 | +import java.time.LocalDate; |
| 18 | +import java.time.ZonedDateTime; |
| 19 | +import java.util.*; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +public final class ProductUtil { |
| 23 | + |
| 24 | + |
| 25 | + public static ProductDraftImport toProductDraftImport(ProductProjection product) { |
| 26 | + var draft = ProductDraftImport.builder().key(product.getKey()) |
| 27 | + .productType(p -> p.key(product.getProductType().getObj().getKey())) |
| 28 | + .name(l -> l.values(product.getName().values())) |
| 29 | + .slug(l -> l.values(product.getSlug().values())) |
| 30 | + .description(d -> d.values( |
| 31 | + Optional.ofNullable(product.getDescription()).map(LocalizedString::values).orElse(null))) // if not null |
| 32 | + .categories(extractCategoryKeyReference(product)) |
| 33 | + .metaTitle(t -> t.values( |
| 34 | + Optional.ofNullable(product.getMetaTitle()).map(LocalizedString::values).orElse(null))) |
| 35 | + .metaDescription((com.commercetools.importapi.models.common.LocalizedString)product.getMetaDescription()) // if not null |
| 36 | + .metaKeywords((com.commercetools.importapi.models.common.LocalizedString) product.getMetaKeywords()) // if not null |
| 37 | + .masterVariant(extractProductVariantDraftImport(product.getMasterVariant())) |
| 38 | + .variants(extractProductVariantDraftImport(product)) |
| 39 | + .taxCategory(t -> t.key(product.getTaxCategory().getObj().getKey())) |
| 40 | + .state(s -> s.key(product.getState().getObj().getKey())) |
| 41 | + //.priceMode(product.getPriceMode()) |
| 42 | + .attributes(product.getAttributes().stream().map(ProductUtil::mapAttribute).collect(Collectors.toList())); |
| 43 | + return draft.build(); |
| 44 | + } |
| 45 | + |
| 46 | + private static List<ProductVariantDraftImport> extractProductVariantDraftImport(ProductProjection product) { |
| 47 | + return product.getVariants().stream().map(ProductUtil::extractProductVariantDraftImport).collect(Collectors.toList()); |
| 48 | + } |
| 49 | + |
| 50 | + private static ProductVariantDraftImport extractProductVariantDraftImport(ProductVariant variant) { |
| 51 | + return ProductVariantDraftImport.builder().key(variant.getKey()).sku(variant.getSku()) |
| 52 | + .setImages(i -> (Image) variant.getImages()) |
| 53 | + .prices(variant.getPrices().stream().map(p -> PriceDraftImport.builder().key(p.getKey()).value(v -> |
| 54 | + (p.getValue() instanceof CentPrecisionMoney) ? |
| 55 | + v.centPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue().getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits()) : |
| 56 | + v.highPrecisionBuilder().centAmount(p.getValue().getCentAmount()).currencyCode(p.getValue().getCurrencyCode()).fractionDigits(p.getValue().getFractionDigits()) |
| 57 | + ).build()).collect(Collectors.toList())) |
| 58 | + .attributes(variant.getAttributes().stream().map( |
| 59 | + ProductUtil::mapAttribute).collect(Collectors.toList())) |
| 60 | + .assets(variant.getAssets()); |
| 61 | + } |
| 62 | + |
| 63 | + private static CategoryKeyReference extractCategoryKeyReference(ProductProjection product) { |
| 64 | + return CategoryKeyReference.builder().key(product.getCategories().get(0).getObj().getKey()).build(); |
| 65 | + } |
| 66 | + |
| 67 | + private static Attribute mapAttribute(com.commercetools.api.models.product.Attribute attribute) { |
| 68 | + Object value = attribute.getValue(); |
| 69 | + |
| 70 | + if (value instanceof String) { |
| 71 | + return Attribute.textBuilder().name(attribute.getName()).value((String) value).build(); |
| 72 | + } |
| 73 | + if (value instanceof Integer) { |
| 74 | + return Attribute.numberBuilder().name(attribute.getName()).value(((Integer) value).doubleValue()).build(); |
| 75 | + } |
| 76 | + if (value instanceof Boolean) { |
| 77 | + return Attribute.booleanBuilder().name(attribute.getName()).value((Boolean) value).build(); |
| 78 | + } |
| 79 | + if (value instanceof Double) { |
| 80 | + return Attribute.numberBuilder().name(attribute.getName()).value(((Double) value)).build(); |
| 81 | + } |
| 82 | + if (value instanceof Long) { |
| 83 | + return Attribute.numberBuilder().name(attribute.getName()).value(((Long) value).doubleValue()).build(); |
| 84 | + } |
| 85 | + if (value instanceof LocalizedString) { |
| 86 | + return Attribute.ltextBuilder().name(attribute.getName()).value(l -> l.values(((LocalizedString) value).values())).build(); |
| 87 | + } |
| 88 | + if (value instanceof AttributePlainEnumValue) { |
| 89 | + return Attribute.enumBuilder().name(attribute.getName()).value(((AttributePlainEnumValue) value).getKey()).build(); |
| 90 | + } |
| 91 | + if (value instanceof AttributeLocalizedEnumValue) { |
| 92 | + return Attribute.enumBuilder().name(attribute.getName()).value(((AttributeLocalizedEnumValue) value).getKey()).build(); |
| 93 | + } |
| 94 | + if (value instanceof Money) { |
| 95 | + return Attribute.moneyBuilder().name(attribute.getName()).value((TypedMoney) value).build(); |
| 96 | + } |
| 97 | + if (value instanceof LocalDate) { |
| 98 | + return Attribute.dateBuilder().name(attribute.getName()).value((LocalDate) value).build(); |
| 99 | + } |
| 100 | + if (value instanceof ZonedDateTime) { |
| 101 | + return Attribute.datetimeBuilder().name(attribute.getName()).value((ZonedDateTime) value).build(); |
| 102 | + } |
| 103 | + if (value instanceof AttributeReferenceType) { |
| 104 | + return Attribute.referenceBuilder().name(attribute.getName()).value((KeyReference) value).build(); |
| 105 | + } |
| 106 | + if (value instanceof AttributeReferenceTypeId) { |
| 107 | + return Attribute.referenceBuilder().name(attribute.getName()).value((KeyReference) value).build(); |
| 108 | + } |
| 109 | + if (value instanceof AttributeConstraintEnum) { |
| 110 | + return Attribute.enumBuilder().name(attribute.getName()).value( |
| 111 | + String.valueOf((AttributeConstraintEnum) value)).build(); |
| 112 | + } |
| 113 | + if (value instanceof AttributeSetType) { |
| 114 | + var elementType = ((AttributeSetType) value).getElementType(); |
| 115 | + if (elementType instanceof BooleanAttribute) { |
| 116 | + return Attribute.booleanSetBuilder().name(attribute.getName()).value((List<Boolean>) value).build(); |
| 117 | + } |
| 118 | + if (elementType instanceof NumberAttribute) { |
| 119 | + return Attribute.numberSetBuilder().name(attribute.getName()).value((List<Double>) value).build(); |
| 120 | + } |
| 121 | + } |
| 122 | + throw new IllegalArgumentException("Unsupported type: " + value.getClass()); |
| 123 | + } |
| 124 | +} |
0 commit comments