Search in sources :

Example 1 with PriceCompositeId

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);
}
Also used : ProductVariant(io.sphere.sdk.products.ProductVariant) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CollectionUtils.collectionToMap(com.commercetools.sync.commons.utils.CollectionUtils.collectionToMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) HashMap(java.util.HashMap) PriceCompositeId(com.commercetools.sync.internals.helpers.PriceCompositeId) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ProductAssetActionFactory(com.commercetools.sync.products.helpers.ProductAssetActionFactory) UnorderedCollectionSyncUtils.buildRemoveUpdateActions(com.commercetools.sync.internals.utils.UnorderedCollectionSyncUtils.buildRemoveUpdateActions) Attribute(io.sphere.sdk.products.attributes.Attribute) RemovePrice(io.sphere.sdk.products.commands.updateactions.RemovePrice) CollectionUtils.emptyIfNull(com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull) ProductVariantPriceUpdateActionUtils.buildActions(com.commercetools.sync.products.utils.ProductVariantPriceUpdateActionUtils.buildActions) Map(java.util.Map) CollectionUtils.filterCollection(com.commercetools.sync.commons.utils.CollectionUtils.filterCollection) Image(io.sphere.sdk.products.Image) SetSku(io.sphere.sdk.products.commands.updateactions.SetSku) AssetsUpdateActionUtils.buildAssetsUpdateActions(com.commercetools.sync.commons.utils.AssetsUpdateActionUtils.buildAssetsUpdateActions) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) AssetDraft(io.sphere.sdk.models.AssetDraft) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) MoveImageToPosition(io.sphere.sdk.products.commands.updateactions.MoveImageToPosition) RemoveImage(io.sphere.sdk.products.commands.updateactions.RemoveImage) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Price(io.sphere.sdk.products.Price) ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) PriceDraft(io.sphere.sdk.products.PriceDraft) Product(io.sphere.sdk.products.Product) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) AddPrice(io.sphere.sdk.products.commands.updateactions.AddPrice) Optional(java.util.Optional) UpdateActionsSortUtils.sortPriceActions(com.commercetools.sync.internals.utils.UpdateActionsSortUtils.sortPriceActions) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) RemovePrice(io.sphere.sdk.products.commands.updateactions.RemovePrice) Price(io.sphere.sdk.products.Price) AddPrice(io.sphere.sdk.products.commands.updateactions.AddPrice) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction(com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) PriceCompositeId(com.commercetools.sync.internals.helpers.PriceCompositeId) PriceDraft(io.sphere.sdk.products.PriceDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Aggregations

BuildUpdateActionException (com.commercetools.sync.commons.exceptions.BuildUpdateActionException)1 SyncException (com.commercetools.sync.commons.exceptions.SyncException)1 AssetsUpdateActionUtils.buildAssetsUpdateActions (com.commercetools.sync.commons.utils.AssetsUpdateActionUtils.buildAssetsUpdateActions)1 CollectionUtils.collectionToMap (com.commercetools.sync.commons.utils.CollectionUtils.collectionToMap)1 CollectionUtils.emptyIfNull (com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull)1 CollectionUtils.filterCollection (com.commercetools.sync.commons.utils.CollectionUtils.filterCollection)1 CommonTypeUpdateActionUtils.buildUpdateAction (com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction)1 PriceCompositeId (com.commercetools.sync.internals.helpers.PriceCompositeId)1 UnorderedCollectionSyncUtils.buildRemoveUpdateActions (com.commercetools.sync.internals.utils.UnorderedCollectionSyncUtils.buildRemoveUpdateActions)1 UpdateActionsSortUtils.sortPriceActions (com.commercetools.sync.internals.utils.UpdateActionsSortUtils.sortPriceActions)1 AttributeMetaData (com.commercetools.sync.products.AttributeMetaData)1 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)1 ProductAssetActionFactory (com.commercetools.sync.products.helpers.ProductAssetActionFactory)1 ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA (com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA)1 ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction (com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction)1 ProductVariantPriceUpdateActionUtils.buildActions (com.commercetools.sync.products.utils.ProductVariantPriceUpdateActionUtils.buildActions)1 UpdateAction (io.sphere.sdk.commands.UpdateAction)1 AssetDraft (io.sphere.sdk.models.AssetDraft)1 Image (io.sphere.sdk.products.Image)1 Price (io.sphere.sdk.products.Price)1