|
| 1 | +package com.commercetools.sdk; |
| 2 | + |
| 3 | +import com.commercetools.api.models.channel.ChannelReference; |
| 4 | +import com.commercetools.api.models.standalone_price.StandalonePrice; |
| 5 | +import com.commercetools.importapi.models.common.ChannelKeyReference; |
| 6 | +import com.commercetools.importapi.models.common.DiscountedPrice; |
| 7 | +import com.commercetools.importapi.models.common.PriceTier; |
| 8 | +import com.commercetools.importapi.models.standalone_prices.StandalonePriceImport; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static com.commercetools.sdk.CommonImportUtil.getImportApiCustom; |
| 13 | +import static com.commercetools.sdk.CommonImportUtil.importApiTypedMoney; |
| 14 | +import static com.commercetools.sdk.ProductUtil.toProductDiscountKeyReference; |
| 15 | + |
| 16 | +public class StandalonePriceUtil { |
| 17 | + public static StandalonePriceImport toStandalonePriceImport(StandalonePrice price) { |
| 18 | + return StandalonePriceImport.builder() |
| 19 | + .key(price.getKey()) // required field |
| 20 | + .sku(price.getSku()) // required field |
| 21 | + .value(v -> importApiTypedMoney(price.getValue(), v)) // required field |
| 22 | + .country(price.getCountry()) |
| 23 | + .customerGroup(CustomerUtil.toCustomerGroupKeyReference(price.getCustomerGroup())) |
| 24 | + .channel(toImportApiChannelKeyReference(price.getChannel())) |
| 25 | + .validFrom(price.getValidFrom()) |
| 26 | + .validUntil(price.getValidUntil()) |
| 27 | + .tiers(toImportApiPriceTiers(price.getTiers())) |
| 28 | + .discounted(toImportApiDiscountedPrice(price.getDiscounted())) |
| 29 | + .custom(getImportApiCustom(price.getCustom())) |
| 30 | + .active(price.getActive()) |
| 31 | + .build(); |
| 32 | + } |
| 33 | + |
| 34 | + private static DiscountedPrice toImportApiDiscountedPrice( |
| 35 | + com.commercetools.api.models.common.DiscountedPrice discounted) { |
| 36 | + return DiscountedPrice.builder().value(v -> importApiTypedMoney(discounted.getValue(), v)) |
| 37 | + .discount(toProductDiscountKeyReference(discounted.getDiscount())) |
| 38 | + .build(); |
| 39 | + } |
| 40 | + |
| 41 | + private static List<PriceTier> toImportApiPriceTiers(List<com.commercetools.api.models.common.PriceTier> tiers) { |
| 42 | + if (tiers == null) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + return tiers.stream().map(StandalonePriceUtil::toImportApiPriceTier).toList(); |
| 46 | + } |
| 47 | + |
| 48 | + private static PriceTier toImportApiPriceTier(com.commercetools.api.models.common.PriceTier tier) { |
| 49 | + return PriceTier.builder() |
| 50 | + .minimumQuantity(tier.getMinimumQuantity()) |
| 51 | + .value(v -> importApiTypedMoney(tier.getValue(), v)) |
| 52 | + .build(); |
| 53 | + } |
| 54 | + |
| 55 | + private static ChannelKeyReference toImportApiChannelKeyReference(ChannelReference channel) { |
| 56 | + if (channel == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + return ChannelKeyReference.builder().key(channel.getId()).build(); |
| 60 | + } |
| 61 | +} |
0 commit comments