Search in sources :

Example 11 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class ProductSyncUtilsTest method buildActions_FromDraftsWithSameForAllAttribute_ShouldBuildUpdateActions.

@Test
void buildActions_FromDraftsWithSameForAllAttribute_ShouldBuildUpdateActions() {
    final ProductVariant masterVariant = oldProduct.getMasterVariant();
    final AttributeDraft brandNameAttribute = AttributeDraft.of("brandName", "sameForAllBrand");
    final ProductVariantDraft newMasterVariant = ProductVariantDraftBuilder.of(masterVariant).plusAttribute(brandNameAttribute).build();
    final ProductVariantDraft variant = ProductVariantDraftBuilder.of().key("v2").sku("3065834").plusAttribute(brandNameAttribute).build();
    final ProductDraft newProductDraft = createProductDraftBuilder(PRODUCT_KEY_1_WITH_PRICES_RESOURCE_PATH, ProductType.referenceOfId("anyProductType")).masterVariant(newMasterVariant).plusVariants(variant).build();
    final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
    final AttributeMetaData brandName = AttributeMetaData.of(AttributeDefinitionBuilder.of("brandName", null, null).attributeConstraint(AttributeConstraint.SAME_FOR_ALL).build());
    attributesMetaData.put("brandName", brandName);
    final List<UpdateAction<Product>> updateActions = ProductSyncUtils.buildActions(oldProduct, newProductDraft, productSyncOptions, attributesMetaData);
    // check that we only have one generated action for all the variants and no duplicates
    // and is ordered correctly before addVariant action
    assertThat(updateActions.size()).isEqualTo(3);
    assertThat(updateActions).containsOnlyOnce(SetAttributeInAllVariants.of(AttributeDraft.of("brandName", "sameForAllBrand"), true));
    assertThat(updateActions.get(0)).isEqualTo(SetAttributeInAllVariants.of(AttributeDraft.of("brandName", "sameForAllBrand"), true));
    assertThat(updateActions.get(1)).isEqualTo(AddVariant.of(Arrays.asList(AttributeDraft.of("brandName", "sameForAllBrand")), null, "3065834", true).withKey("v2"));
    assertThat(updateActions.get(2)).isEqualTo(Publish.of());
}
Also used : ProductDraft(io.sphere.sdk.products.ProductDraft) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) LocalizedString(io.sphere.sdk.models.LocalizedString) ProductVariant(io.sphere.sdk.products.ProductVariant) Test(org.junit.jupiter.api.Test)

Example 12 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class ProductUpdateActionUtilsTest method buildVariantsUpdateActions_doesNotRemoveMaster.

@Test
void buildVariantsUpdateActions_doesNotRemoveMaster() {
    final ProductProjection productOld = createProductFromJson(OLD_PROD_WITH_VARIANTS);
    final ProductDraft productDraftNew = createProductDraftFromJson(NEW_PROD_DRAFT_WITH_VARIANTS_MOVE_MASTER);
    final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(mock(SphereClient.class)).syncFilter(SyncFilter.of()).build();
    final Map<String, AttributeMetaData> attributesMetaData = new HashMap<>();
    final AttributeMetaData priceInfo = AttributeMetaData.of(AttributeDefinitionBuilder.of("priceInfo", null, null).build());
    attributesMetaData.put("priceInfo", priceInfo);
    final List<UpdateAction<Product>> updateActions = buildVariantsUpdateActions(productOld, productDraftNew, productSyncOptions, attributesMetaData);
    // check remove variants are the first in the list, but not the master variant
    assertThat(updateActions.subList(0, 3)).containsExactlyInAnyOrder(RemoveVariant.ofVariantId(2, true), RemoveVariant.ofVariantId(3, true), RemoveVariant.ofVariantId(4, true));
    // change master variant must be always after variants are added/updated,
    // because it is set by SKU and we should be sure the master variant is already added and SKUs
    // are actual.
    assertThat(updateActions).endsWith(ChangeMasterVariant.ofSku("var-7-sku", true));
    // Old master variant should NOT be removed because it exists in
    // NEW_PROD_DRAFT_WITH_VARIANTS_MOVE_MASTER
    final ProductVariant oldMasterVariant = productOld.getMasterVariant();
    assertThat(updateActions).filteredOn(action -> {
        // verify old master variant is not removed
        if (action instanceof RemoveVariant) {
            RemoveVariant removeVariantAction = (RemoveVariant) action;
            return Objects.equals(oldMasterVariant.getId(), removeVariantAction.getId()) || Objects.equals(oldMasterVariant.getSku(), removeVariantAction.getSku());
        }
        return false;
    }).isEmpty();
}
Also used : ProductVariant(io.sphere.sdk.products.ProductVariant) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProductUpdateActionUtils.getAllVariants(com.commercetools.sync.products.utils.ProductUpdateActionUtils.getAllVariants) UpdateAction(io.sphere.sdk.commands.UpdateAction) Collections.singletonList(java.util.Collections.singletonList) BLANK_NEW_MASTER_VARIANT_KEY(com.commercetools.sync.products.utils.ProductUpdateActionUtils.BLANK_NEW_MASTER_VARIANT_KEY) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) SphereClient(io.sphere.sdk.client.SphereClient) AddVariant(io.sphere.sdk.products.commands.updateactions.AddVariant) SetSku(io.sphere.sdk.products.commands.updateactions.SetSku) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ChangeMasterVariant(io.sphere.sdk.products.commands.updateactions.ChangeMasterVariant) AssetDraft(io.sphere.sdk.models.AssetDraft) AssetDraftBuilder(io.sphere.sdk.models.AssetDraftBuilder) Collections.emptyList(java.util.Collections.emptyList) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) ProductSyncMockUtils.createProductDraftFromJson(com.commercetools.sync.products.ProductSyncMockUtils.createProductDraftFromJson) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) Product(io.sphere.sdk.products.Product) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) LocalizedString.ofEnglish(io.sphere.sdk.models.LocalizedString.ofEnglish) ProductSyncOptionsBuilder(com.commercetools.sync.products.ProductSyncOptionsBuilder) AttributeConstraint(io.sphere.sdk.products.attributes.AttributeConstraint) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) AddExternalImage(io.sphere.sdk.products.commands.updateactions.AddExternalImage) ProductType(io.sphere.sdk.producttypes.ProductType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BLANK_NEW_MASTER_VARIANT_SKU(com.commercetools.sync.products.utils.ProductUpdateActionUtils.BLANK_NEW_MASTER_VARIANT_SKU) SyncFilter(com.commercetools.sync.products.SyncFilter) Image(io.sphere.sdk.products.Image) ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) SetAttribute(io.sphere.sdk.products.commands.updateactions.SetAttribute) ProductSyncMockUtils.createProductFromJson(com.commercetools.sync.products.ProductSyncMockUtils.createProductFromJson) AttributeDefinitionBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionBuilder) Collections.emptyMap(java.util.Collections.emptyMap) RemoveImage(io.sphere.sdk.products.commands.updateactions.RemoveImage) AssetSourceBuilder(io.sphere.sdk.models.AssetSourceBuilder) BLANK_OLD_MASTER_VARIANT_KEY(com.commercetools.sync.products.utils.ProductUpdateActionUtils.BLANK_OLD_MASTER_VARIANT_KEY) RemoveVariant(io.sphere.sdk.products.commands.updateactions.RemoveVariant) PriceDraft(io.sphere.sdk.products.PriceDraft) Collectors.toList(java.util.stream.Collectors.toList) AttributeDraft(io.sphere.sdk.products.attributes.AttributeDraft) ProductUpdateActionUtils.buildAddVariantUpdateActionFromDraft(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildAddVariantUpdateActionFromDraft) ProductUpdateActionUtils.buildVariantsUpdateActions(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildVariantsUpdateActions) ProductVariantDraftBuilder(io.sphere.sdk.products.ProductVariantDraftBuilder) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) HashMap(java.util.HashMap) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeMasterVariantUpdateAction) ProductProjection(io.sphere.sdk.products.ProductProjection) ProductDraft(io.sphere.sdk.products.ProductDraft) SphereClient(io.sphere.sdk.client.SphereClient) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ProductVariant(io.sphere.sdk.products.ProductVariant) RemoveVariant(io.sphere.sdk.products.commands.updateactions.RemoveVariant) Test(org.junit.jupiter.api.Test)

Example 13 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class ProductUpdateActionUtils method collectAllVariantUpdateActions.

@Nonnull
private static List<UpdateAction<Product>> collectAllVariantUpdateActions(@Nonnull final List<UpdateAction<Product>> sameForAllUpdateActions, @Nonnull final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductVariant oldProductVariant, @Nonnull final ProductVariantDraft newProductVariant, @Nonnull final Map<String, AttributeMetaData> attributesMetaData, @Nonnull final ProductSyncOptions syncOptions) {
    final ArrayList<UpdateAction<Product>> updateActions = new ArrayList<>();
    final SyncFilter syncFilter = syncOptions.getSyncFilter();
    updateActions.addAll(buildActionsIfPassesFilter(syncFilter, ATTRIBUTES, () -> emptyIfNull(buildProductVariantAttributesUpdateActions(oldProduct, newProduct, oldProductVariant, newProductVariant, attributesMetaData, syncOptions)).stream().filter(collectedUpdateAction -> hasDuplicateSameForAllAction(sameForAllUpdateActions, collectedUpdateAction)).collect(Collectors.toList())));
    updateActions.addAll(buildActionsIfPassesFilter(syncFilter, IMAGES, () -> buildProductVariantImagesUpdateActions(oldProductVariant, newProductVariant)));
    updateActions.addAll(buildActionsIfPassesFilter(syncFilter, PRICES, () -> buildProductVariantPricesUpdateActions(oldProduct, newProduct, oldProductVariant, newProductVariant, syncOptions)));
    updateActions.addAll(buildActionsIfPassesFilter(syncFilter, ASSETS, () -> buildProductVariantAssetsUpdateActions(oldProduct, newProduct, oldProductVariant, newProductVariant, syncOptions)));
    buildActionIfPassesFilter(syncFilter, SKU, () -> buildProductVariantSkuUpdateAction(oldProductVariant, newProductVariant)).ifPresent(updateActions::add);
    return updateActions;
}
Also used : ProductVariant(io.sphere.sdk.products.ProductVariant) AddToCategory(io.sphere.sdk.products.commands.updateactions.AddToCategory) SKU(com.commercetools.sync.products.ActionGroup.SKU) Reference(io.sphere.sdk.models.Reference) ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CommonTypeUpdateActionUtils.buildUpdateActionForReferences(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateActionForReferences) SetCategoryOrderHint(io.sphere.sdk.products.commands.updateactions.SetCategoryOrderHint) UpdateAction(io.sphere.sdk.commands.UpdateAction) SearchKeywords(io.sphere.sdk.search.SearchKeywords) BaseSyncOptions(com.commercetools.sync.commons.BaseSyncOptions) ProductVariantUpdateActionUtils.buildProductVariantImagesUpdateActions(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantImagesUpdateActions) Collections.singletonList(java.util.Collections.singletonList) ChangeName(io.sphere.sdk.products.commands.updateactions.ChangeName) SetTaxCategory(io.sphere.sdk.products.commands.updateactions.SetTaxCategory) UnorderedCollectionSyncUtils.buildRemoveUpdateActions(com.commercetools.sync.internals.utils.UnorderedCollectionSyncUtils.buildRemoveUpdateActions) Map(java.util.Map) PRICES(com.commercetools.sync.products.ActionGroup.PRICES) AddVariant(io.sphere.sdk.products.commands.updateactions.AddVariant) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) ChangeMasterVariant(io.sphere.sdk.products.commands.updateactions.ChangeMasterVariant) Collectors.toSet(java.util.stream.Collectors.toSet) ProductVariantUpdateActionUtils.buildProductVariantAttributesUpdateActions(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantAttributesUpdateActions) Collections.emptyList(java.util.Collections.emptyList) Category(io.sphere.sdk.categories.Category) SetAttributeInAllVariants(io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants) Set(java.util.Set) ActionGroup(com.commercetools.sync.products.ActionGroup) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) Product(io.sphere.sdk.products.Product) CommonTypeUpdateActionUtils.areResourceIdentifiersEqual(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.areResourceIdentifiersEqual) State(io.sphere.sdk.states.State) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ChangeSlug(io.sphere.sdk.products.commands.updateactions.ChangeSlug) FilterUtils.executeSupplierIfPassesFilter(com.commercetools.sync.commons.utils.FilterUtils.executeSupplierIfPassesFilter) Objects(java.util.Objects) LocalizedString(io.sphere.sdk.models.LocalizedString) IMAGES(com.commercetools.sync.products.ActionGroup.IMAGES) List(java.util.List) Publish(io.sphere.sdk.products.commands.updateactions.Publish) Optional(java.util.Optional) Objects.nonNull(java.util.Objects.nonNull) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) SetMetaKeywords(io.sphere.sdk.products.commands.updateactions.SetMetaKeywords) SetSearchKeywords(io.sphere.sdk.products.commands.updateactions.SetSearchKeywords) CollectionUtils.collectionToMap(com.commercetools.sync.commons.utils.CollectionUtils.collectionToMap) SetMetaTitle(io.sphere.sdk.products.commands.updateactions.SetMetaTitle) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) SetDescription(io.sphere.sdk.products.commands.updateactions.SetDescription) TransitionState(io.sphere.sdk.products.commands.updateactions.TransitionState) SyncFilter(com.commercetools.sync.products.SyncFilter) CollectionUtils.emptyIfNull(com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull) ATTRIBUTES(com.commercetools.sync.products.ActionGroup.ATTRIBUTES) ProductVariantUpdateActionUtils.buildProductVariantSkuUpdateAction(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantSkuUpdateAction) CollectionUtils.filterCollection(com.commercetools.sync.commons.utils.CollectionUtils.filterCollection) SetMetaDescription(io.sphere.sdk.products.commands.updateactions.SetMetaDescription) Unpublish(io.sphere.sdk.products.commands.updateactions.Unpublish) Nonnull(javax.annotation.Nonnull) Collections.emptyMap(java.util.Collections.emptyMap) Optional.ofNullable(java.util.Optional.ofNullable) ProductVariantUpdateActionUtils.buildProductVariantPricesUpdateActions(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantPricesUpdateActions) ASSETS(com.commercetools.sync.products.ActionGroup.ASSETS) RemoveVariant(io.sphere.sdk.products.commands.updateactions.RemoveVariant) Collectors.toList(java.util.stream.Collectors.toList) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) CommonTypeUpdateActionUtils.buildUpdateActions(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateActions) BooleanUtils.toBoolean(org.apache.commons.lang3.BooleanUtils.toBoolean) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) ProductVariantUpdateActionUtils.buildProductVariantAssetsUpdateActions(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantAssetsUpdateActions) RemoveFromCategory(io.sphere.sdk.products.commands.updateactions.RemoveFromCategory) Collections(java.util.Collections) Referenceable(io.sphere.sdk.models.Referenceable) ResourceImpl(io.sphere.sdk.models.ResourceImpl) SyncFilter(com.commercetools.sync.products.SyncFilter) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductVariantUpdateActionUtils.buildProductVariantSkuUpdateAction(com.commercetools.sync.products.utils.ProductVariantUpdateActionUtils.buildProductVariantSkuUpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 14 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class ProductVariantAttributeUpdateActionUtils method buildProductVariantAttributeUpdateAction.

/**
 * Compares the attributes of a {@link AttributeDraft} and a {@link Attribute} to build either a
 * {@link io.sphere.sdk.products.commands.updateactions.SetAttribute} or a {@link
 * io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants}.
 *
 * <p>If the attribute is sameForAll a {@link
 * io.sphere.sdk.products.commands.updateactions.SetAttributeInAllVariants} is built. Otherwise, a
 * {@link io.sphere.sdk.products.commands.updateactions.SetAttribute} is built.
 *
 * <p>If both the {@link AttributeDraft} and the {@link Attribute} have identical values, then no
 * update action is needed and hence an empty {@link List} is returned.
 *
 * @param variantId the id of the variant of that the attribute belong to. It is used only in the
 *     error messages if any.
 * @param oldProductVariantAttribute the {@link Attribute} which should be updated.
 * @param newProductVariantAttribute the {@link AttributeDraft} where we get the new value.
 * @param attributesMetaData a map of attribute name -&gt; {@link AttributeMetaData}; which
 *     defines attribute information: its name and whether it has the constraint "SameForAll" or
 *     not.
 * @return A filled optional with the update action or an empty optional if the attributes are
 *     identical.
 * @throws BuildUpdateActionException thrown if attribute as not found in the {@code
 *     attributeMetaData} or if the attribute is required and the new value is null.
 */
@Nonnull
public static Optional<UpdateAction<Product>> buildProductVariantAttributeUpdateAction(final int variantId, @Nullable final Attribute oldProductVariantAttribute, @Nonnull final AttributeDraft newProductVariantAttribute, @Nonnull final Map<String, AttributeMetaData> attributesMetaData) throws BuildUpdateActionException {
    final String newProductVariantAttributeName = newProductVariantAttribute.getName();
    final JsonNode newProductVariantAttributeValue = newProductVariantAttribute.getValue();
    final JsonNode oldProductVariantAttributeValue = oldProductVariantAttribute != null ? oldProductVariantAttribute.getValueAsJsonNode() : null;
    final AttributeMetaData attributeMetaData = attributesMetaData.get(newProductVariantAttributeName);
    if (attributeMetaData == null) {
        final String errorMessage = format(ATTRIBUTE_NOT_IN_ATTRIBUTE_METADATA, newProductVariantAttributeName);
        throw new BuildUpdateActionException(errorMessage);
    }
    return attributeMetaData.isSameForAll() ? buildUpdateAction(oldProductVariantAttributeValue, newProductVariantAttributeValue, () -> SetAttributeInAllVariants.of(newProductVariantAttribute, true)) : buildUpdateAction(oldProductVariantAttributeValue, newProductVariantAttributeValue, () -> SetAttribute.of(variantId, newProductVariantAttribute, true));
}
Also used : AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) JsonNode(com.fasterxml.jackson.databind.JsonNode) BuildUpdateActionException(com.commercetools.sync.commons.exceptions.BuildUpdateActionException) Nonnull(javax.annotation.Nonnull)

Example 15 with AttributeMetaData

use of com.commercetools.sync.products.AttributeMetaData in project commercetools-sync-java by commercetools.

the class ProductSyncUtils method buildActions.

/**
 * Compares all the fields (including the variants see {@link
 * ProductUpdateActionUtils#buildVariantsUpdateActions(ProductProjection, ProductDraft,
 * ProductSyncOptions, Map)}) of a {@link ProductProjection} and a {@link ProductDraft}, given
 * that each of these fields pass the specified {@link SyncFilter}. It returns a {@link List} of
 * {@link UpdateAction}&lt;{@link Product}&gt; as a result. If no update action is needed, for
 * example in case where both the {@link ProductProjection} and the {@link ProductDraft} have the
 * same names, an empty {@link List} is returned. Then it applies a specified filter function in
 * the {@link ProductSyncOptions} instance on the resultant list and returns this result.
 *
 * @param oldProduct the productprojection which should be updated.
 * @param newProduct the product draft where we get the new data.
 * @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).
 * @param attributesMetaData a map of attribute name -&gt; {@link AttributeMetaData}; which
 *     defines each attribute's information: its name and whether it has the constraint
 *     "SameForAll" or not.
 * @return A list of product-specific update actions.
 */
@Nonnull
public static List<UpdateAction<Product>> buildActions(@Nonnull final ProductProjection oldProduct, @Nonnull final ProductDraft newProduct, @Nonnull final ProductSyncOptions syncOptions, @Nonnull final Map<String, AttributeMetaData> attributesMetaData) {
    final SyncFilter syncFilter = syncOptions.getSyncFilter();
    final List<UpdateAction<Product>> updateActions = new ArrayList<>(filterEmptyOptionals(buildActionIfPassesFilter(syncFilter, ActionGroup.NAME, () -> buildChangeNameUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.DESCRIPTION, () -> buildSetDescriptionUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.SLUG, () -> buildChangeSlugUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.SEARCHKEYWORDS, () -> buildSetSearchKeywordsUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.METATITLE, () -> buildSetMetaTitleUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.METADESCRIPTION, () -> buildSetMetaDescriptionUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.METAKEYWORDS, () -> buildSetMetaKeywordsUpdateAction(oldProduct, newProduct)), buildActionIfPassesFilter(syncFilter, ActionGroup.TAXCATEGORY, () -> buildSetTaxCategoryUpdateAction(oldProduct, newProduct).map(action -> (UpdateAction<Product>) action)), buildActionIfPassesFilter(syncFilter, ActionGroup.STATE, () -> buildTransitionStateUpdateAction(oldProduct, newProduct).map(action -> (UpdateAction<Product>) action))));
    final List<UpdateAction<Product>> productCategoryUpdateActions = buildActionsIfPassesFilter(syncFilter, ActionGroup.CATEGORIES, () -> buildCategoryActions(oldProduct, newProduct));
    updateActions.addAll(productCategoryUpdateActions);
    updateActions.addAll(buildVariantsUpdateActions(oldProduct, newProduct, syncOptions, attributesMetaData));
    // lastly publish/unpublish product
    final boolean hasNewUpdateActions = updateActions.size() > 0;
    buildPublishOrUnpublishUpdateAction(oldProduct, newProduct, hasNewUpdateActions).ifPresent(updateActions::add);
    return prioritizeUpdateActions(updateActions, oldProduct.getMasterVariant().getId());
}
Also used : ProductUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaDescriptionUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductUpdateActionUtils.buildSetSearchKeywordsUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetSearchKeywordsUpdateAction) ProductUpdateActionUtils.buildActionIfPassesFilter(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildActionIfPassesFilter) ProductUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeSlugUpdateAction) ProductUpdateActionUtils.buildSetCategoryOrderHintUpdateActions(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetCategoryOrderHintUpdateActions) ArrayList(java.util.ArrayList) SyncFilter(com.commercetools.sync.products.SyncFilter) ProductUpdateActionUtils.buildPublishOrUnpublishUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildPublishOrUnpublishUpdateAction) CollectionUtils.emptyIfNull(com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull) ProductUpdateActionUtils.buildAddToCategoryUpdateActions(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildAddToCategoryUpdateActions) Map(java.util.Map) ProductUpdateActionUtils.buildSetTaxCategoryUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetTaxCategoryUpdateAction) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) Nonnull(javax.annotation.Nonnull) ProductUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeNameUpdateAction) Predicate(java.util.function.Predicate) ProductUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaKeywordsUpdateAction) ProductUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetDescriptionUpdateAction) RemoveVariant(io.sphere.sdk.products.commands.updateactions.RemoveVariant) ActionGroup(com.commercetools.sync.products.ActionGroup) AttributeMetaData(com.commercetools.sync.products.AttributeMetaData) Product(io.sphere.sdk.products.Product) Collectors(java.util.stream.Collectors) ProductUpdateActionUtils.buildActionsIfPassesFilter(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildActionsIfPassesFilter) List(java.util.List) ProductUpdateActionUtils.buildRemoveFromCategoryUpdateActions(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildRemoveFromCategoryUpdateActions) OptionalUtils.filterEmptyOptionals(com.commercetools.sync.commons.utils.OptionalUtils.filterEmptyOptionals) ProductUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaTitleUpdateAction) ProductUpdateActionUtils.buildVariantsUpdateActions(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildVariantsUpdateActions) ProductUpdateActionUtils.buildTransitionStateUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildTransitionStateUpdateAction) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) SyncFilter(com.commercetools.sync.products.SyncFilter) ProductUpdateActionUtils.buildSetMetaDescriptionUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaDescriptionUpdateAction) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductUpdateActionUtils.buildSetSearchKeywordsUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetSearchKeywordsUpdateAction) ProductUpdateActionUtils.buildChangeSlugUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeSlugUpdateAction) ProductUpdateActionUtils.buildPublishOrUnpublishUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildPublishOrUnpublishUpdateAction) ProductUpdateActionUtils.buildSetTaxCategoryUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetTaxCategoryUpdateAction) ProductUpdateActionUtils.buildChangeNameUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildChangeNameUpdateAction) ProductUpdateActionUtils.buildSetMetaKeywordsUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaKeywordsUpdateAction) ProductUpdateActionUtils.buildSetDescriptionUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetDescriptionUpdateAction) ProductUpdateActionUtils.buildSetMetaTitleUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildSetMetaTitleUpdateAction) ProductUpdateActionUtils.buildTransitionStateUpdateAction(com.commercetools.sync.products.utils.ProductUpdateActionUtils.buildTransitionStateUpdateAction) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Aggregations

AttributeMetaData (com.commercetools.sync.products.AttributeMetaData)30 HashMap (java.util.HashMap)28 UpdateAction (io.sphere.sdk.commands.UpdateAction)26 Test (org.junit.jupiter.api.Test)26 AttributeDraft (io.sphere.sdk.products.attributes.AttributeDraft)21 SetAttribute (io.sphere.sdk.products.commands.updateactions.SetAttribute)21 Attribute (io.sphere.sdk.products.attributes.Attribute)20 AttributeDefinition (io.sphere.sdk.products.attributes.AttributeDefinition)18 ProductDraft (io.sphere.sdk.products.ProductDraft)8 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)6 ProductProjection (io.sphere.sdk.products.ProductProjection)6 ProductVariantDraft (io.sphere.sdk.products.ProductVariantDraft)6 SyncFilter (com.commercetools.sync.products.SyncFilter)4 ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction (com.commercetools.sync.products.utils.ProductVariantAttributeUpdateActionUtils.buildProductVariantAttributeUpdateAction)4 LocalizedString (io.sphere.sdk.models.LocalizedString)4 CollectionUtils.emptyIfNull (com.commercetools.sync.commons.utils.CollectionUtils.emptyIfNull)3 ActionGroup (com.commercetools.sync.products.ActionGroup)3 Product (io.sphere.sdk.products.Product)3 ProductVariant (io.sphere.sdk.products.ProductVariant)3 RemoveVariant (io.sphere.sdk.products.commands.updateactions.RemoveVariant)3