use of com.commercetools.sync.internals.helpers.PriceCompositeId in project commercetools-sync-java by commercetools.
the class ProductVariantUpdateActionUtils method buildProductVariantPricesUpdateActions.
/**
* Compares the {@link List} of {@link io.sphere.sdk.products.Price}s of a {@link
* ProductVariantDraft} and a {@link ProductVariant} and returns a {@link List} of {@link
* UpdateAction}<{@link Product}>. If both the {@link ProductVariantDraft} and the {@link
* ProductVariant} have identical list of prices, then no update action is needed and hence an
* empty {@link List} is returned.
*
* @param oldProduct the product which should be updated.
* @param newProduct the product draft.
* @param oldProductVariant the {@link ProductVariant} which should be updated.
* @param newProductVariant the {@link ProductVariantDraft} where we get the new list of prices.
* @param syncOptions the sync options wrapper which contains options related to the sync process
* supplied by the user. For example, custom callbacks to call in case of warnings or errors
* occurring on the build update action process. And other options (See {@link
* ProductSyncOptions} for more info).
* @return a list that contains all the update actions needed, otherwise an empty list if no
* update actions are needed.
*/
@Nonnull
public static List<UpdateAction<Product>> buildProductVariantPricesUpdateActions(@Nullable final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductVariant oldProductVariant, @Nonnull final ProductVariantDraft newProductVariant, @Nonnull final ProductSyncOptions syncOptions) {
final List<Price> oldPrices = oldProductVariant.getPrices();
final List<PriceDraft> newPrices = newProductVariant.getPrices();
final List<UpdateAction<Product>> updateActions = buildRemoveUpdateActions(oldPrices, newPrices, PriceCompositeId::of, PriceCompositeId::of, price -> RemovePrice.of(price, true));
final Integer variantId = oldProductVariant.getId();
final Map<PriceCompositeId, Price> oldPricesMap = collectionToMap(oldPrices, PriceCompositeId::of);
emptyIfNull(newPrices).forEach(newPrice -> {
if (newPrice == null) {
syncOptions.applyErrorCallback(new SyncException(format("Failed to build prices update actions " + "for one price on the variant with id '%d' and key '%s'. Reason: %s", variantId, oldProductVariant.getKey(), NULL_PRODUCT_VARIANT_PRICE)), oldProduct, newProduct, null);
} else {
final PriceCompositeId newPriceCompositeId = PriceCompositeId.of(newPrice);
final Price matchingOldPrice = oldPricesMap.get(newPriceCompositeId);
final List<UpdateAction<Product>> updateOrAddPrice = ofNullable(matchingOldPrice).map(oldPrice -> buildActions(newProduct, variantId, oldPrice, newPrice, syncOptions)).orElseGet(() -> singletonList(AddPrice.ofVariantId(variantId, newPrice, true)));
updateActions.addAll(updateOrAddPrice);
}
});
return sortPriceActions(updateActions);
}
Aggregations