Search in sources :

Example 1 with ReferenceResolutionException

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

the class AttributeDefinitionReferenceResolver method resolveReferences.

/**
 * Given an {@link AttributeDefinitionDraft} this method attempts to resolve the ProductType
 * references, which can exist on attributeDefinition with an AttributeType: NestedType or SetType
 * of NestedType, to return a {@link CompletionStage} which contains a new instance of the draft
 * with the resolved references.
 *
 * @param attributeDefinitionDraft the attributeDefinitionDraft to resolve its references.
 * @return a {@link CompletionStage} that contains as a result a new attributeDefinitionDraft
 *     instance with resolved references or if there is no productType existing with the given key
 *     the draft will be returned as is without the reference resolved. In case an error occurs
 *     during reference resolution, a {@link ReferenceResolutionException} is thrown.
 */
@Nonnull
public CompletionStage<AttributeDefinitionDraft> resolveReferences(@Nonnull final AttributeDefinitionDraft attributeDefinitionDraft) {
    final AttributeDefinitionDraftBuilder draftBuilder = AttributeDefinitionDraftBuilder.of(attributeDefinitionDraft);
    return resolveNestedAttributeTypeReferences(draftBuilder).handle(ImmutablePair::new).thenCompose(result -> {
        final Throwable exception = result.getValue();
        final AttributeDefinitionDraftBuilder resolvedBuilder = result.getKey();
        if (exception == null) {
            return completedFuture(resolvedBuilder.build());
        } else {
            final String errorMessage = format("Failed to resolve references on attribute definition with name '%s'.", attributeDefinitionDraft.getName());
            return exceptionallyCompletedFuture(new ReferenceResolutionException(errorMessage, exception.getCause()));
        }
    });
}
Also used : ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) AttributeDefinitionDraftBuilder(io.sphere.sdk.products.attributes.AttributeDefinitionDraftBuilder) Nonnull(javax.annotation.Nonnull)

Example 2 with ReferenceResolutionException

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

the class InventoryReferenceResolver method fetchOrCreateAndResolveReference.

/**
 * Given an {@link InventoryEntryDraftBuilder} and a {@code channelKey} this method fetches the
 * actual id of the channel corresponding to this key, ideally from a cache. Then it sets this id
 * on the supply channel reference id of the inventory entry draft builder. If the id is not found
 * in cache nor the CTP project and {@code ensureChannel} option is set to true, a new channel
 * will be created with this key and the role {@code "InventorySupply"}. However, if the {@code
 * ensureChannel} is set to false, the future is completed exceptionally with a {@link
 * ReferenceResolutionException}.
 *
 * @param draftBuilder the inventory draft builder to read it's values (key, sku, channel) and
 *     then to write resolved resource identifiers.
 * @param channelKey the key of the channel to resolve it's actual id on the draft.
 * @return a {@link CompletionStage} that contains as a result the same {@code draftBuilder}
 *     inventory draft builder instance with resolved supply channel resource identifier or an
 *     exception.
 */
@Nonnull
private CompletionStage<InventoryEntryDraftBuilder> fetchOrCreateAndResolveReference(@Nonnull final InventoryEntryDraftBuilder draftBuilder, @Nonnull final String channelKey) {
    final CompletionStage<InventoryEntryDraftBuilder> inventoryEntryDraftCompletionStage = channelService.fetchCachedChannelId(channelKey).thenCompose(resolvedChannelIdOptional -> resolvedChannelIdOptional.map(resolvedChannelId -> setChannelReference(resolvedChannelId, draftBuilder)).orElseGet(() -> createChannelAndSetReference(channelKey, draftBuilder)));
    final CompletableFuture<InventoryEntryDraftBuilder> result = new CompletableFuture<>();
    inventoryEntryDraftCompletionStage.whenComplete((resolvedDraftBuilder, exception) -> {
        if (exception != null) {
            result.completeExceptionally(new ReferenceResolutionException(format(FAILED_TO_RESOLVE_SUPPLY_CHANNEL, draftBuilder.getSku(), exception.getCause().getMessage()), exception));
        } else {
            result.complete(resolvedDraftBuilder);
        }
    });
    return result;
}
Also used : InventoryEntryDraftBuilder(io.sphere.sdk.inventory.InventoryEntryDraftBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) Nonnull(javax.annotation.Nonnull)

Example 3 with ReferenceResolutionException

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

the class ProductReferenceResolver method fetchAndResolveCategoryReferences.

@Nonnull
private CompletionStage<ProductDraftBuilder> fetchAndResolveCategoryReferences(@Nonnull final ProductDraftBuilder draftBuilder, @Nonnull final Set<String> categoryKeys, @Nonnull final List<ResourceIdentifier<Category>> directCategoryReferences) {
    final Map<String, String> categoryOrderHintsMap = new HashMap<>();
    final CategoryOrderHints categoryOrderHints = draftBuilder.getCategoryOrderHints();
    final Map<String, Category> keyToCategory = new HashMap<>();
    return categoryService.fetchMatchingCategoriesByKeys(categoryKeys).thenApply(categories -> categories.stream().map(category -> {
        keyToCategory.put(category.getKey(), category);
        if (categoryOrderHints != null) {
            ofNullable(categoryOrderHints.get(category.getKey())).ifPresent(orderHintValue -> categoryOrderHintsMap.put(category.getId(), orderHintValue));
        }
        return Category.referenceOfId(category.getId()).toResourceIdentifier();
    }).collect(toSet())).thenCompose(categoryReferences -> {
        String keysNotExists = categoryKeys.stream().filter(categoryKey -> !keyToCategory.containsKey(categoryKey)).collect(joining(", "));
        if (!isBlank(keysNotExists)) {
            final String errorMessage = format(CATEGORIES_DO_NOT_EXIST, keysNotExists);
            return exceptionallyCompletedFuture(new ReferenceResolutionException(format(FAILED_TO_RESOLVE_REFERENCE, Category.resourceTypeId(), draftBuilder.getKey(), errorMessage)));
        }
        categoryReferences.addAll(directCategoryReferences);
        return completedFuture(draftBuilder.categories(categoryReferences).categoryOrderHints(CategoryOrderHints.of(categoryOrderHintsMap)));
    });
}
Also used : ProductVariantDraft(io.sphere.sdk.products.ProductVariantDraft) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ProductType(io.sphere.sdk.producttypes.ProductType) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProductTypeService(com.commercetools.sync.services.ProductTypeService) TypeService(com.commercetools.sync.services.TypeService) Map(java.util.Map) CompletableFutureUtils.exceptionallyCompletedFuture(io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture) TaxCategoryService(com.commercetools.sync.services.TaxCategoryService) CustomObjectCompositeIdentifier(com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier) ProductDraft(io.sphere.sdk.products.ProductDraft) ProductDraftBuilder(io.sphere.sdk.products.ProductDraftBuilder) Nonnull(javax.annotation.Nonnull) Collectors.toSet(java.util.stream.Collectors.toSet) CustomerService(com.commercetools.sync.services.CustomerService) Optional.ofNullable(java.util.Optional.ofNullable) Category(io.sphere.sdk.categories.Category) Set(java.util.Set) CompletableFutureUtils.collectionOfFuturesToFutureOfCollection(com.commercetools.sync.commons.utils.CompletableFutureUtils.collectionOfFuturesToFutureOfCollection) CategoryService(com.commercetools.sync.services.CategoryService) StateService(com.commercetools.sync.services.StateService) State(io.sphere.sdk.states.State) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Collectors.toList(java.util.stream.Collectors.toList) ChannelService(com.commercetools.sync.services.ChannelService) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) BaseReferenceResolver(com.commercetools.sync.commons.helpers.BaseReferenceResolver) CustomerGroupService(com.commercetools.sync.services.CustomerGroupService) ProductService(com.commercetools.sync.services.ProductService) CompletableFutureUtils.mapValuesToFutureOfCompletedValues(com.commercetools.sync.commons.utils.CompletableFutureUtils.mapValuesToFutureOfCompletedValues) Collections(java.util.Collections) ProductSyncOptions(com.commercetools.sync.products.ProductSyncOptions) CustomObjectService(com.commercetools.sync.services.CustomObjectService) ResourceIdentifier(io.sphere.sdk.models.ResourceIdentifier) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) Category(io.sphere.sdk.categories.Category) TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) HashMap(java.util.HashMap) ReferenceResolutionException(com.commercetools.sync.commons.exceptions.ReferenceResolutionException) CategoryOrderHints(io.sphere.sdk.products.CategoryOrderHints) Nonnull(javax.annotation.Nonnull)

Aggregations

ReferenceResolutionException (com.commercetools.sync.commons.exceptions.ReferenceResolutionException)3 Nonnull (javax.annotation.Nonnull)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 BaseReferenceResolver (com.commercetools.sync.commons.helpers.BaseReferenceResolver)1 CompletableFutureUtils.collectionOfFuturesToFutureOfCollection (com.commercetools.sync.commons.utils.CompletableFutureUtils.collectionOfFuturesToFutureOfCollection)1 CompletableFutureUtils.mapValuesToFutureOfCompletedValues (com.commercetools.sync.commons.utils.CompletableFutureUtils.mapValuesToFutureOfCompletedValues)1 CustomObjectCompositeIdentifier (com.commercetools.sync.customobjects.helpers.CustomObjectCompositeIdentifier)1 ProductSyncOptions (com.commercetools.sync.products.ProductSyncOptions)1 CategoryService (com.commercetools.sync.services.CategoryService)1 ChannelService (com.commercetools.sync.services.ChannelService)1 CustomObjectService (com.commercetools.sync.services.CustomObjectService)1 CustomerGroupService (com.commercetools.sync.services.CustomerGroupService)1 CustomerService (com.commercetools.sync.services.CustomerService)1 ProductService (com.commercetools.sync.services.ProductService)1 ProductTypeService (com.commercetools.sync.services.ProductTypeService)1 StateService (com.commercetools.sync.services.StateService)1 TaxCategoryService (com.commercetools.sync.services.TaxCategoryService)1 TypeService (com.commercetools.sync.services.TypeService)1 Category (io.sphere.sdk.categories.Category)1 InventoryEntryDraftBuilder (io.sphere.sdk.inventory.InventoryEntryDraftBuilder)1