Search in sources :

Example 21 with SyncException

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

the class ProductTypeSync 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 product types 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 product types counter is incremented.
 * @param oldProductType existing product type that could be updated.
 * @param newProductType draft containing data that could differ from data in {@code
 *     oldProductType}.
 * @param updateActions the update actions to update the {@link ProductType} with.
 */
private void handleError(@Nonnull final String errorMessage, @Nullable final Throwable exception, final int failedTimes, @Nullable final ProductType oldProductType, @Nullable final ProductTypeDraft newProductType, @Nullable final List<UpdateAction<ProductType>> updateActions) {
    SyncException syncException = exception != null ? new SyncException(errorMessage, exception) : new SyncException(errorMessage);
    syncOptions.applyErrorCallback(syncException, oldProductType, newProductType, updateActions);
    statistics.incrementFailed(failedTimes);
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException)

Example 22 with SyncException

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

the class ProductTypeSync method resolveMissingNestedReferences.

/**
 * Given a map of product type keys pointing to a set of attribute definition drafts which are now
 * ready to be added for this product type. This method first converts the drafts to {@link
 * AddAttributeDefinition} actions in which the reference id value (which is a key) is resolved to
 * an actual UUID of the product type key pointed by this key. Then, for each product type, the
 * method issues an update request containing all the actions.
 *
 * @return a {@link CompletionStage} which contains an empty result after execution of all the
 *     update requests.
 */
@Nonnull
private CompletionStage<Void> resolveMissingNestedReferences(@Nonnull final Map<String, Set<AttributeDefinitionDraft>> productTypesToUpdate) {
    final Set<String> keys = productTypesToUpdate.keySet();
    return productTypeService.fetchMatchingProductTypesByKeys(keys).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
        final Set<ProductType> matchingProductTypes = fetchResponse.getKey();
        final Throwable exception = fetchResponse.getValue();
        if (exception != null) {
            final String errorMessage = format(CTP_PRODUCT_TYPE_FETCH_FAILED, keys);
            syncOptions.applyErrorCallback(new SyncException(errorMessage, exception));
            return CompletableFuture.completedFuture(null);
        } else {
            final Map<String, ProductType> keyToProductType = matchingProductTypes.stream().collect(Collectors.toMap(ProductType::getKey, productType -> productType));
            return CompletableFuture.allOf(productTypesToUpdate.entrySet().stream().map(entry -> {
                final String productTypeToUpdateKey = entry.getKey();
                final Set<AttributeDefinitionDraft> attributeDefinitionDrafts = entry.getValue();
                final List<UpdateAction<ProductType>> actionsWithResolvedReferences = draftsToActions(attributeDefinitionDrafts);
                final ProductType productTypeToUpdate = keyToProductType.get(productTypeToUpdateKey);
                return resolveMissingNestedReferences(productTypeToUpdate, actionsWithResolvedReferences);
            }).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new));
        }
    });
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException) BaseSync(com.commercetools.sync.commons.BaseSync) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ProductTypeReferenceResolver(com.commercetools.sync.producttypes.helpers.ProductTypeReferenceResolver) ProductType(io.sphere.sdk.producttypes.ProductType) UpdateAction(io.sphere.sdk.commands.UpdateAction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddAttributeDefinition(io.sphere.sdk.producttypes.commands.updateactions.AddAttributeDefinition) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Collectors.toMap(java.util.stream.Collectors.toMap) ProductTypeBatchValidator.getProductTypeKey(com.commercetools.sync.producttypes.helpers.ProductTypeBatchValidator.getProductTypeKey) ProductTypeService(com.commercetools.sync.services.ProductTypeService) Map(java.util.Map) SyncUtils.batchElements(com.commercetools.sync.commons.utils.SyncUtils.batchElements) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ProductTypeDraftBuilder(io.sphere.sdk.producttypes.ProductTypeDraftBuilder) Optional.ofNullable(java.util.Optional.ofNullable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProductTypeDraft(io.sphere.sdk.producttypes.ProductTypeDraft) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException) ProductTypeBatchValidator(com.commercetools.sync.producttypes.helpers.ProductTypeBatchValidator) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) ProductTypeSyncUtils.buildActions(com.commercetools.sync.producttypes.utils.ProductTypeSyncUtils.buildActions) CompletionStage(java.util.concurrent.CompletionStage) ProductTypeServiceImpl(com.commercetools.sync.services.impl.ProductTypeServiceImpl) AttributeType(io.sphere.sdk.products.attributes.AttributeType) AttributeDefinitionReferenceResolver(com.commercetools.sync.producttypes.helpers.AttributeDefinitionReferenceResolver) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) ProductTypeSyncStatistics(com.commercetools.sync.producttypes.helpers.ProductTypeSyncStatistics) CompletableFuture(java.util.concurrent.CompletableFuture) Set(java.util.Set) ProductType(io.sphere.sdk.producttypes.ProductType) ArrayList(java.util.ArrayList) List(java.util.List) SyncException(com.commercetools.sync.commons.exceptions.SyncException) CompletionStage(java.util.concurrent.CompletionStage) Nonnull(javax.annotation.Nonnull)

Example 23 with SyncException

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

the class ProductTypeBatchValidator method getReferencedProductTypeKeys.

@Nonnull
private static Set<String> getReferencedProductTypeKeys(@Nonnull final ProductTypeDraft productTypeDraft) throws SyncException {
    final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productTypeDraft.getAttributes();
    if (attributeDefinitionDrafts == null || attributeDefinitionDrafts.isEmpty()) {
        return emptySet();
    }
    final Set<String> referencedProductTypeKeys = new HashSet<>();
    final List<String> invalidAttributeDefinitionNames = new ArrayList<>();
    for (AttributeDefinitionDraft attributeDefinitionDraft : attributeDefinitionDrafts) {
        if (attributeDefinitionDraft != null) {
            final AttributeType attributeType = attributeDefinitionDraft.getAttributeType();
            try {
                getProductTypeKey(attributeType).ifPresent(referencedProductTypeKeys::add);
            } catch (InvalidReferenceException invalidReferenceException) {
                invalidAttributeDefinitionNames.add(attributeDefinitionDraft.getName());
            }
        }
    }
    if (!invalidAttributeDefinitionNames.isEmpty()) {
        final String errorMessage = format(PRODUCT_TYPE_HAS_INVALID_REFERENCES, productTypeDraft.getKey(), invalidAttributeDefinitionNames);
        throw new SyncException(errorMessage, new InvalidReferenceException(BLANK_ID_VALUE_ON_REFERENCE));
    }
    return referencedProductTypeKeys;
}
Also used : NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) AttributeType(io.sphere.sdk.products.attributes.AttributeType) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) ArrayList(java.util.ArrayList) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 24 with SyncException

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

the class TextLineItemUpdateActionUtils method buildUpdateActions.

/**
 * The decisions in the calculating update actions are documented on the
 * `docs/adr/0002-shopping-lists-lineitem-and-textlineitem-update-actions.md`
 */
@Nonnull
private static List<UpdateAction<ShoppingList>> buildUpdateActions(@Nonnull final ShoppingList oldShoppingList, @Nonnull final ShoppingListDraft newShoppingList, @Nonnull final List<TextLineItem> oldTextLineItems, @Nonnull final List<TextLineItemDraft> newTextLineItems, @Nonnull final ShoppingListSyncOptions syncOptions) {
    final List<UpdateAction<ShoppingList>> updateActions = new ArrayList<>();
    final int minSize = Math.min(oldTextLineItems.size(), newTextLineItems.size());
    for (int i = 0; i < minSize; i++) {
        final TextLineItem oldTextLineItem = oldTextLineItems.get(i);
        final TextLineItemDraft newTextLineItem = newTextLineItems.get(i);
        if (newTextLineItem.getName() == null || newTextLineItem.getName().getLocales().isEmpty()) {
            /*
        checking the name of the oldTextLineItem is not needed, because it's required.
        with this check below, it's avoided bad request case like:

        "detailedErrorMessage": "actions -> name: Missing required value"
        */
            syncOptions.applyErrorCallback(new SyncException(format("TextLineItemDraft at position '%d' of the ShoppingListDraft with key '%s' has no name " + "set. Please make sure all text line items have names.", i, newShoppingList.getKey())), oldShoppingList, newShoppingList, updateActions);
            return emptyList();
        }
        updateActions.addAll(buildTextLineItemUpdateActions(oldShoppingList, newShoppingList, oldTextLineItem, newTextLineItem, syncOptions));
    }
    for (int i = minSize; i < oldTextLineItems.size(); i++) {
        updateActions.add(RemoveTextLineItem.of(oldTextLineItems.get(i).getId()));
    }
    for (int i = minSize; i < newTextLineItems.size(); i++) {
        if (hasQuantity(newTextLineItems.get(i))) {
            updateActions.add(mapToAddTextLineItemAction(newTextLineItems.get(i)));
        }
    }
    return updateActions;
}
Also used : UpdateAction(io.sphere.sdk.commands.UpdateAction) CommonTypeUpdateActionUtils.buildUpdateAction(com.commercetools.sync.commons.utils.CommonTypeUpdateActionUtils.buildUpdateAction) TextLineItemDraft(io.sphere.sdk.shoppinglists.TextLineItemDraft) AddTextLineItem(io.sphere.sdk.shoppinglists.commands.updateactions.AddTextLineItem) RemoveTextLineItem(io.sphere.sdk.shoppinglists.commands.updateactions.RemoveTextLineItem) TextLineItem(io.sphere.sdk.shoppinglists.TextLineItem) ArrayList(java.util.ArrayList) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Example 25 with SyncException

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

the class StateSync method updateState.

/**
 * Given an existing {@link State} and a new {@link StateDraft}, the method calculates all the
 * update actions required to synchronize the existing state to be the same as the new one. If
 * there are update actions found, a request is made to CTP to update the existing state,
 * otherwise it doesn't issue a request.
 *
 * <p>The {@code statistics} instance is updated accordingly to whether the CTP request was
 * carried out successfully or not. If an exception was thrown on executing the request to CTP,
 * the error handling method is called.
 *
 * @param oldState existing state that could be updated.
 * @param newState draft containing data that could differ from data in {@code oldState}.
 * @return a {@link CompletionStage} which contains an empty result after execution of the update.
 */
@Nonnull
private CompletionStage<Void> updateState(@Nonnull final State oldState, @Nonnull final StateDraft newState, @Nonnull final List<UpdateAction<State>> updateActions) {
    return stateService.updateState(oldState, updateActions).handle(ImmutablePair::new).thenCompose(updateResponse -> {
        final Throwable sphereException = updateResponse.getValue();
        if (sphereException != null) {
            return executeSupplierIfConcurrentModificationException(sphereException, () -> fetchAndUpdate(oldState, newState), () -> {
                final String errorMessage = format(CTP_STATE_UPDATE_FAILED, newState.getKey(), sphereException.getMessage());
                handleError(new SyncException(errorMessage, sphereException), oldState, newState, updateActions, 1);
                return completedFuture(null);
            });
        } else {
            statistics.incrementUpdated();
            return completedFuture(null);
        }
    });
}
Also used : 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