All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commercetools.sync.cartdiscounts.utils.CartDiscountReferenceResolutionUtils Maven / Gradle / Ivy

package com.commercetools.sync.cartdiscounts.utils;

import static com.commercetools.sync.commons.utils.CustomTypeReferenceResolutionUtils.mapToCustomFieldsDraft;

import com.commercetools.api.models.cart_discount.CartDiscount;
import com.commercetools.api.models.cart_discount.CartDiscountDraft;
import com.commercetools.api.models.cart_discount.CartDiscountDraftBuilder;
import com.commercetools.api.models.cart_discount.CartDiscountValue;
import com.commercetools.api.models.cart_discount.CartDiscountValueAbsolute;
import com.commercetools.api.models.cart_discount.CartDiscountValueAbsoluteDraft;
import com.commercetools.api.models.cart_discount.CartDiscountValueAbsoluteDraftBuilder;
import com.commercetools.api.models.cart_discount.CartDiscountValueDraft;
import com.commercetools.api.models.cart_discount.CartDiscountValueFixed;
import com.commercetools.api.models.cart_discount.CartDiscountValueFixedDraft;
import com.commercetools.api.models.cart_discount.CartDiscountValueFixedDraftBuilder;
import com.commercetools.api.models.cart_discount.CartDiscountValueGiftLineItem;
import com.commercetools.api.models.cart_discount.CartDiscountValueGiftLineItemDraft;
import com.commercetools.api.models.cart_discount.CartDiscountValueGiftLineItemDraftBuilder;
import com.commercetools.api.models.cart_discount.CartDiscountValueRelative;
import com.commercetools.api.models.cart_discount.CartDiscountValueRelativeDraft;
import com.commercetools.api.models.cart_discount.CartDiscountValueRelativeDraftBuilder;
import com.commercetools.api.models.common.MoneyBuilder;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;

/**
 * Util class which provides utilities that can be used when syncing resources from a source
 * commercetools project to a target one.
 */
public final class CartDiscountReferenceResolutionUtils {

  /**
   * Returns an {@link java.util.List}<{@link CartDiscountDraft}> consisting of the results of
   * applying the mapping from {@link CartDiscount} to {@link CartDiscountDraft} with considering
   * reference resolution.
   *
   * 
   *   
   *   
   *     
   *       
   *       
   *       
   *     
   *   
   *   
   *     
   *        
   *        
   *        
   *     
   *   
   * 
Mapping of Reference fields for the reference resolution
Reference fieldfromto
custom.type{@link com.commercetools.api.models.type.TypeReference}{@link com.commercetools.api.models.type.TypeResourceIdentifier}
* *

Note: The {@link com.commercetools.api.models.type.Type} reference should contain Id * in the map(cache) with a key value. Any reference, which have its id in place and not replaced * by the key, it would not be found in the map. In this case, this reference will be considered * as existing resources on the target commercetools project and the library will issue an * update/create API request without reference resolution. * * @param cartDiscounts the cart discounts without expansion of references. * @param referenceIdToKeyCache the instance that manages cache. * @return a {@link java.util.List} of {@link CartDiscountDraft} built from the supplied {@link * java.util.List} of {@link CartDiscount}. */ @Nonnull public static List mapToCartDiscountDrafts( @Nonnull final List cartDiscounts, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { return cartDiscounts.stream() .map(cartDiscount -> mapToCartDiscountDraft(cartDiscount, referenceIdToKeyCache)) .collect(Collectors.toList()); } @Nonnull private static CartDiscountDraft mapToCartDiscountDraft( @Nonnull final CartDiscount cartDiscount, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { return CartDiscountDraftBuilder.of() .cartPredicate(cartDiscount.getCartPredicate()) .name(cartDiscount.getName()) .requiresDiscountCode(cartDiscount.getRequiresDiscountCode()) .sortOrder(cartDiscount.getSortOrder()) .target(cartDiscount.getTarget()) .value(toCartDiscountValueDraft(cartDiscount.getValue())) .key(cartDiscount.getKey()) .description(cartDiscount.getDescription()) .isActive(cartDiscount.getIsActive()) .validFrom(cartDiscount.getValidFrom()) .validUntil(cartDiscount.getValidUntil()) .stackingMode(cartDiscount.getStackingMode()) .custom(mapToCustomFieldsDraft(cartDiscount, referenceIdToKeyCache)) .build(); } private static CartDiscountValueDraft toCartDiscountValueDraft( final CartDiscountValue cartDiscountValue) { if (cartDiscountValue instanceof CartDiscountValueAbsolute) { return cloneCartDiscountValueAbsoluteDraft((CartDiscountValueAbsolute) cartDiscountValue); } if (cartDiscountValue instanceof CartDiscountValueFixed) { return cloneCartDiscountValueFixedDraft((CartDiscountValueFixed) cartDiscountValue); } if (cartDiscountValue instanceof CartDiscountValueGiftLineItem) { return cloneCartDiscountValueGiftLineItemDraft( (CartDiscountValueGiftLineItem) cartDiscountValue); } if (cartDiscountValue instanceof CartDiscountValueRelative) { return cloneCartDiscountValueRelativeDraft((CartDiscountValueRelative) cartDiscountValue); } return null; } private static CartDiscountValueRelativeDraft cloneCartDiscountValueRelativeDraft( final CartDiscountValueRelative cartDiscountValueRelative) { return CartDiscountValueRelativeDraftBuilder.of() .permyriad(cartDiscountValueRelative.getPermyriad()) .build(); } private static CartDiscountValueGiftLineItemDraft cloneCartDiscountValueGiftLineItemDraft( final CartDiscountValueGiftLineItem cartDiscountValueGiftLineItem) { final CartDiscountValueGiftLineItemDraftBuilder builder = CartDiscountValueGiftLineItemDraftBuilder.of() .product( productResourceIdentifierBuilder -> productResourceIdentifierBuilder.id( cartDiscountValueGiftLineItem.getProduct().getId())) .variantId(cartDiscountValueGiftLineItem.getVariantId()); if (cartDiscountValueGiftLineItem.getDistributionChannel() != null) { builder.distributionChannel( channelResourceIdentifierBuilder -> channelResourceIdentifierBuilder.id( cartDiscountValueGiftLineItem.getDistributionChannel().getId())); } if (cartDiscountValueGiftLineItem.getSupplyChannel() != null) { builder.supplyChannel( channelResourceIdentifierBuilder -> channelResourceIdentifierBuilder.id( cartDiscountValueGiftLineItem.getSupplyChannel().getId())); } return builder.build(); } private static CartDiscountValueFixedDraft cloneCartDiscountValueFixedDraft( final CartDiscountValueFixed cartDiscountValueFixed) { return CartDiscountValueFixedDraftBuilder.of() .money( cartDiscountValueFixed.getMoney().stream() .map( centPrecisionMoney -> MoneyBuilder.of() .currencyCode(centPrecisionMoney.getCurrencyCode()) .centAmount(centPrecisionMoney.getCentAmount()) .build()) .collect(Collectors.toList())) .build(); } private static CartDiscountValueAbsoluteDraft cloneCartDiscountValueAbsoluteDraft( final CartDiscountValueAbsolute cartDiscountValueAbsolute) { return CartDiscountValueAbsoluteDraftBuilder.of() .money( cartDiscountValueAbsolute.getMoney().stream() .map( centPrecisionMoney -> MoneyBuilder.of() .currencyCode(centPrecisionMoney.getCurrencyCode()) .centAmount(centPrecisionMoney.getCentAmount()) .build()) .collect(Collectors.toList())) .build(); } private CartDiscountReferenceResolutionUtils() {} }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy