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()));
}
});
}
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;
}
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)));
});
}
Aggregations