Search in sources :

Example 41 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomObjectSync method handleError.

/**
 * Given a {@link String} {@code errorMessage} and a {@link Throwable} {@code exception}, this
 * method calls the optional error callback specified in the {@code syncOptions} and updates the
 * {@code statistics} instance by incrementing the total number of failed custom objects to sync.
 *
 * @param errorMessage The error message describing the reason(s) of failure.
 * @param exception The exception that called caused the failure, if any.
 * @param failedTimes The number of times that the failed custom objects counter is incremented.
 */
private void handleError(@Nonnull final String errorMessage, @Nonnull final Throwable exception, final int failedTimes) {
    SyncException syncException = new SyncException(errorMessage, exception);
    syncOptions.applyErrorCallback(syncException);
    statistics.incrementFailed(failedTimes);
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException)

Example 42 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class ProductSync method resolveNowReadyReferences.

@Nonnull
private CompletionStage<Void> resolveNowReadyReferences(final Map<String, String> keyToIdCache) {
    // We delete anyways the keys from the statistics before we attempt resolution, because even if
    // resolution fails
    // the products that failed to be synced would be counted as failed.
    final Set<String> referencingDraftKeys = readyToResolve.stream().map(statistics::removeAndGetReferencingKeys).filter(Objects::nonNull).flatMap(Set::stream).collect(Collectors.toSet());
    if (referencingDraftKeys.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    final Set<ProductDraft> readyToSync = new HashSet<>();
    final Set<WaitingToBeResolvedProducts> waitingDraftsToBeUpdated = new HashSet<>();
    return unresolvedReferencesService.fetch(referencingDraftKeys, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
        final Set<WaitingToBeResolvedProducts> waitingDrafts = fetchResponse.getKey();
        final Throwable fetchException = fetchResponse.getValue();
        if (fetchException != null) {
            final String errorMessage = format(UNRESOLVED_REFERENCES_STORE_FETCH_FAILED, referencingDraftKeys);
            handleError(new SyncException(errorMessage, fetchException), referencingDraftKeys.size());
            return CompletableFuture.completedFuture(null);
        }
        waitingDrafts.forEach(waitingDraft -> {
            final Set<String> missingReferencedProductKeys = waitingDraft.getMissingReferencedProductKeys();
            missingReferencedProductKeys.removeAll(readyToResolve);
            if (missingReferencedProductKeys.isEmpty()) {
                readyToSync.add(waitingDraft.getProductDraft());
            } else {
                waitingDraftsToBeUpdated.add(waitingDraft);
            }
        });
        return updateWaitingDrafts(waitingDraftsToBeUpdated).thenCompose(aVoid -> syncBatch(readyToSync, keyToIdCache)).thenCompose(aVoid -> removeFromWaiting(readyToSync));
    });
}
Also used : CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) ProductUpdateActionUtils.getAllVariants(com.commercetools.sync.products.utils.ProductUpdateActionUtils.getAllVariants) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductBatchValidator(com.commercetools.sync.products.helpers.ProductBatchValidator) Collectors.toMap(java.util.stream.Collectors.toMap) ProductTypeService(com.commercetools.sync.services.ProductTypeService) Map(java.util.Map) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) TaxCategorySyncOptionsBuilder(com.commercetools.sync.taxcategories.TaxCategorySyncOptionsBuilder) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) StateService(com.commercetools.sync.services.StateService) Product(io.sphere.sdk.products.Product) ChannelServiceImpl(com.commercetools.sync.services.impl.ChannelServiceImpl) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) Objects(java.util.Objects) CustomerSyncOptionsBuilder(com.commercetools.sync.customers.CustomerSyncOptionsBuilder) ChannelService(com.commercetools.sync.services.ChannelService) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) CompletionStage(java.util.concurrent.CompletionStage) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Function.identity(java.util.function.Function.identity) TaxCategoryServiceImpl(com.commercetools.sync.services.impl.TaxCategoryServiceImpl) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CompletableFuture(java.util.concurrent.CompletableFuture) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) HashSet(java.util.HashSet) CustomerGroupServiceImpl(com.commercetools.sync.services.impl.CustomerGroupServiceImpl) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) ProductSyncUtils.buildActions(com.commercetools.sync.products.utils.ProductSyncUtils.buildActions) TypeService(com.commercetools.sync.services.TypeService) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CustomerService(com.commercetools.sync.services.CustomerService) ChannelRole(io.sphere.sdk.channels.ChannelRole) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) Optional.ofNullable(java.util.Optional.ofNullable) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductDraft(io.sphere.sdk.products.ProductDraft) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) Objects(java.util.Objects) SyncException(com.commercetools.sync.commons.exceptions.SyncException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 43 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class ProductSync method syncDraft.

@Nonnull
private CompletionStage<Void> syncDraft(@Nonnull final Set<ProductProjection> oldProducts, @Nonnull final ProductDraft newProductDraft) {
    final Map<String, ProductProjection> oldProductMap = oldProducts.stream().collect(toMap(ProductProjection::getKey, identity()));
    return productReferenceResolver.resolveReferences(newProductDraft).thenCompose(resolvedDraft -> {
        final ProductProjection oldProduct = oldProductMap.get(newProductDraft.getKey());
        return ofNullable(oldProduct).map(ProductProjection -> fetchProductAttributesMetadataAndUpdate(oldProduct, resolvedDraft)).orElseGet(() -> applyCallbackAndCreate(resolvedDraft));
    }).exceptionally(completionException -> {
        final String errorMessage = format(FAILED_TO_PROCESS, newProductDraft.getKey(), completionException.getMessage());
        handleError(new SyncException(errorMessage, completionException), 1);
        return null;
    });
}
Also used : CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) ProductUpdateActionUtils.getAllVariants(com.commercetools.sync.products.utils.ProductUpdateActionUtils.getAllVariants) UpdateAction(io.sphere.sdk.commands.UpdateAction) ProductBatchValidator(com.commercetools.sync.products.helpers.ProductBatchValidator) Collectors.toMap(java.util.stream.Collectors.toMap) ProductTypeService(com.commercetools.sync.services.ProductTypeService) Map(java.util.Map) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductProjection(io.sphere.sdk.products.ProductProjection) UnresolvedReferencesServiceImpl(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl) TaxCategorySyncOptionsBuilder(com.commercetools.sync.taxcategories.TaxCategorySyncOptionsBuilder) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CategoryService(com.commercetools.sync.services.CategoryService) StateService(com.commercetools.sync.services.StateService) Product(io.sphere.sdk.products.Product) ChannelServiceImpl(com.commercetools.sync.services.impl.ChannelServiceImpl) StateServiceImpl(com.commercetools.sync.services.impl.StateServiceImpl) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) CustomObjectSyncOptionsBuilder(com.commercetools.sync.customobjects.CustomObjectSyncOptionsBuilder) Objects(java.util.Objects) CustomerSyncOptionsBuilder(com.commercetools.sync.customers.CustomerSyncOptionsBuilder) ChannelService(com.commercetools.sync.services.ChannelService) ProductServiceImpl(com.commercetools.sync.services.impl.ProductServiceImpl) List(java.util.List) ProductSyncStatistics(com.commercetools.sync.products.helpers.ProductSyncStatistics) CompletionStage(java.util.concurrent.CompletionStage) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) Function.identity(java.util.function.Function.identity) TaxCategoryServiceImpl(com.commercetools.sync.services.impl.TaxCategoryServiceImpl) Optional(java.util.Optional) CustomObjectServiceImpl(com.commercetools.sync.services.impl.CustomObjectServiceImpl) CategoryServiceImpl(com.commercetools.sync.services.impl.CategoryServiceImpl) CustomObjectService(com.commercetools.sync.services.CustomObjectService) CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY(com.commercetools.sync.services.impl.UnresolvedReferencesServiceImpl.CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY) WaitingToBeResolvedProducts(com.commercetools.sync.commons.models.WaitingToBeResolvedProducts) CompletableFuture(java.util.concurrent.CompletableFuture) ProductReferenceResolver(com.commercetools.sync.products.helpers.ProductReferenceResolver) HashSet(java.util.HashSet) CustomerGroupServiceImpl(com.commercetools.sync.services.impl.CustomerGroupServiceImpl) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) ProductSyncUtils.buildActions(com.commercetools.sync.products.utils.ProductSyncUtils.buildActions) TypeService(com.commercetools.sync.services.TypeService) UnresolvedReferencesService(com.commercetools.sync.services.UnresolvedReferencesService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CustomerService(com.commercetools.sync.services.CustomerService) ChannelRole(io.sphere.sdk.channels.ChannelRole) StateSyncOptionsBuilder(com.commercetools.sync.states.StateSyncOptionsBuilder) Optional.ofNullable(java.util.Optional.ofNullable) CategorySyncOptionsBuilder(com.commercetools.sync.categories.CategorySyncOptionsBuilder) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) ProductService(com.commercetools.sync.services.ProductService) Collections(java.util.Collections) ProductProjection(io.sphere.sdk.products.ProductProjection) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Example 44 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomerSync method syncBatch.

@Nonnull
private CompletionStage<Void> syncBatch(@Nonnull final Set<Customer> oldCustomers, @Nonnull final Set<CustomerDraft> newCustomerDrafts) {
    final Map<String, Customer> oldCustomerMap = oldCustomers.stream().collect(toMap(Customer::getKey, identity()));
    return CompletableFuture.allOf(newCustomerDrafts.stream().map(customerDraft -> referenceResolver.resolveReferences(customerDraft).thenCompose(resolvedCustomerDraft -> syncDraft(oldCustomerMap, resolvedCustomerDraft)).exceptionally(completionException -> {
        final String errorMessage = format(FAILED_TO_PROCESS, customerDraft.getKey(), completionException.getMessage());
        handleError(new SyncException(errorMessage, completionException), 1);
        return null;
    })).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new));
}
Also used : CustomerBatchValidator(com.commercetools.sync.customers.helpers.CustomerBatchValidator) CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) UpdateAction(io.sphere.sdk.commands.UpdateAction) CompletableFuture(java.util.concurrent.CompletableFuture) CustomerSyncUtils.buildActions(com.commercetools.sync.customers.utils.CustomerSyncUtils.buildActions) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) CustomerGroupServiceImpl(com.commercetools.sync.services.impl.CustomerGroupServiceImpl) Collectors.toMap(java.util.stream.Collectors.toMap) TypeService(com.commercetools.sync.services.TypeService) Map(java.util.Map) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) Nonnull(javax.annotation.Nonnull) Collectors.toSet(java.util.stream.Collectors.toSet) CustomerService(com.commercetools.sync.services.CustomerService) Set(java.util.Set) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) Customer(io.sphere.sdk.customers.Customer) String.format(java.lang.String.format) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Function.identity(java.util.function.Function.identity) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) Optional(java.util.Optional) CustomerReferenceResolver(com.commercetools.sync.customers.helpers.CustomerReferenceResolver) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) CompletableFuture(java.util.concurrent.CompletableFuture) Customer(io.sphere.sdk.customers.Customer) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletionStage(java.util.concurrent.CompletionStage) Nonnull(javax.annotation.Nonnull)

Example 45 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomerSync method fetchAndUpdate.

@Nonnull
private CompletionStage<Void> fetchAndUpdate(@Nonnull final Customer oldCustomer, @Nonnull final CustomerDraft newCustomerDraft) {
    final String customerKey = oldCustomer.getKey();
    return customerService.fetchCustomerByKey(customerKey).handle(ImmutablePair::of).thenCompose(fetchResponse -> {
        final Optional<Customer> fetchedCustomerOptional = fetchResponse.getKey();
        final Throwable exception = fetchResponse.getValue();
        if (exception != null) {
            final String errorMessage = format(CTP_CUSTOMER_UPDATE_FAILED, customerKey, "Failed to fetch from CTP while retrying after concurrency modification.");
            handleError(new SyncException(errorMessage, exception), 1);
            return CompletableFuture.completedFuture(null);
        }
        return fetchedCustomerOptional.map(fetchedCustomer -> buildActionsAndUpdate(fetchedCustomer, newCustomerDraft)).orElseGet(() -> {
            final String errorMessage = format(CTP_CUSTOMER_UPDATE_FAILED, customerKey, "Not found when attempting to fetch while retrying after concurrency modification.");
            handleError(new SyncException(errorMessage, null), 1);
            return CompletableFuture.completedFuture(null);
        });
    });
}
Also used : CustomerBatchValidator(com.commercetools.sync.customers.helpers.CustomerBatchValidator) CustomerServiceImpl(com.commercetools.sync.services.impl.CustomerServiceImpl) SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) UpdateAction(io.sphere.sdk.commands.UpdateAction) CompletableFuture(java.util.concurrent.CompletableFuture) CustomerSyncUtils.buildActions(com.commercetools.sync.customers.utils.CustomerSyncUtils.buildActions) CustomerSyncStatistics(com.commercetools.sync.customers.helpers.CustomerSyncStatistics) CustomerGroupServiceImpl(com.commercetools.sync.services.impl.CustomerGroupServiceImpl) Collectors.toMap(java.util.stream.Collectors.toMap) TypeService(com.commercetools.sync.services.TypeService) Map(java.util.Map) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) Nonnull(javax.annotation.Nonnull) Collectors.toSet(java.util.stream.Collectors.toSet) CustomerService(com.commercetools.sync.services.CustomerService) Set(java.util.Set) TypeServiceImpl(com.commercetools.sync.services.impl.TypeServiceImpl) Customer(io.sphere.sdk.customers.Customer) String.format(java.lang.String.format) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Function.identity(java.util.function.Function.identity) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) Optional(java.util.Optional) CustomerReferenceResolver(com.commercetools.sync.customers.helpers.CustomerReferenceResolver) CustomerDraft(io.sphere.sdk.customers.CustomerDraft) Customer(io.sphere.sdk.customers.Customer) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Aggregations

SyncException (com.commercetools.sync.commons.exceptions.SyncException)74 Nonnull (javax.annotation.Nonnull)47 UpdateAction (io.sphere.sdk.commands.UpdateAction)45 Optional (java.util.Optional)39 List (java.util.List)37 CompletionStage (java.util.concurrent.CompletionStage)25 SphereClient (io.sphere.sdk.client.SphereClient)24 String.format (java.lang.String.format)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)20 Test (org.junit.jupiter.api.Test)20 TriConsumer (com.commercetools.sync.commons.utils.TriConsumer)18 Set (java.util.Set)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 Nullable (javax.annotation.Nullable)16 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Category (io.sphere.sdk.categories.Category)13 BaseSync (com.commercetools.sync.commons.BaseSync)12 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)12