diff --git a/src/main/java/com/commercetools/sync/services/impl/BaseService.java b/src/main/java/com/commercetools/sync/services/impl/BaseService.java index 7643015915..070640c17e 100644 --- a/src/main/java/com/commercetools/sync/services/impl/BaseService.java +++ b/src/main/java/com/commercetools/sync/services/impl/BaseService.java @@ -1,25 +1,19 @@ package com.commercetools.sync.services.impl; import com.commercetools.sync.commons.BaseSyncOptions; -import io.sphere.sdk.commands.DraftBasedCreateCommand; import io.sphere.sdk.commands.UpdateAction; import io.sphere.sdk.commands.UpdateCommand; import io.sphere.sdk.models.Resource; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; -import java.util.function.Function; import static com.commercetools.sync.commons.utils.SyncUtils.batchElements; -import static java.lang.String.format; -import static org.apache.commons.lang3.StringUtils.isBlank; /** * @param Resource (e.g. {@link io.sphere.sdk.products.Product}, {@link io.sphere.sdk.categories.Category}, etc.. @@ -31,59 +25,12 @@ class BaseService, V> { boolean isCached = false; final Map keyToIdCache = new ConcurrentHashMap<>(); - private static final String CREATE_FAILED = "Failed to create draft with key: '%s'. Reason: %s"; private static final int MAXIMUM_ALLOWED_UPDATE_ACTIONS = 500; BaseService(@Nonnull final BaseSyncOptions syncOptions) { this.syncOptions = syncOptions; } - /** - * Applies the BeforeCreateCallback function on a supplied draft, then attempts to create it on CTP if it's not - * empty, then applies the supplied handler on the response from CTP. If the draft was empty after applying the - * callback an empty optional is returned as the resulting future. - * - * @param resourceDraft draft to apply callback on and then create on CTP. - * @param draftKey draft key. - * @param createCommandFunction the create command query to create the resource on CTP. - * @return a future containing an optional which might contain the resource if successfully created or empty - * otherwise. - */ - @Nonnull - CompletionStage> applyCallbackAndCreate( - @Nonnull final V resourceDraft, - @Nullable final String draftKey, - @Nonnull final Function> createCommandFunction) { - if (isBlank(draftKey)) { - syncOptions.applyErrorCallback(format(CREATE_FAILED, draftKey, "Draft key is blank!")); - return CompletableFuture.completedFuture(Optional.empty()); - } else { - final BiFunction> responseHandler = - (createdResource, sphereException) -> - handleResourceCreation(draftKey, createdResource, sphereException); - return syncOptions.applyBeforeCreateCallBack(resourceDraft) - .map(mappedDraft -> - syncOptions.getCtpClient() - .execute(createCommandFunction.apply(mappedDraft)) - .handle(responseHandler) - ) - .orElseGet(() -> CompletableFuture.completedFuture(Optional.empty())); - } - } - - @Nonnull - private Optional handleResourceCreation(@Nonnull final String draftKey, - @Nullable final U createdResource, - @Nullable final Throwable sphereException) { - if (createdResource != null) { - keyToIdCache.put(draftKey, createdResource.getId()); - return Optional.of(createdResource); - } else { - syncOptions.applyErrorCallback(format(CREATE_FAILED, draftKey, sphereException), sphereException); - return Optional.empty(); - } - } - /** * Executes update request(s) on the {@code resource} with all the {@code updateActions} using the * {@code updateCommandFunction} while taking care of the CTP constraint of 500 update actions per request by