Skip to content

Commit

Permalink
#325: Remove tight coupling of applyCreateCallback from create services.
Browse files Browse the repository at this point in the history
  • Loading branch information
heshamMassoud committed Nov 24, 2018
1 parent 325ed1d commit 1c7d2c7
Showing 1 changed file with 0 additions and 53 deletions.
@@ -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 <U> Resource (e.g. {@link io.sphere.sdk.products.Product}, {@link io.sphere.sdk.categories.Category}, etc..
Expand All @@ -31,59 +25,12 @@ class BaseService<U extends Resource<U>, V> {
boolean isCached = false;
final Map<String, String> 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<U, V> 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<Optional<U>> applyCallbackAndCreate(
@Nonnull final V resourceDraft,
@Nullable final String draftKey,
@Nonnull final Function<V, DraftBasedCreateCommand<U, V>> createCommandFunction) {
if (isBlank(draftKey)) {
syncOptions.applyErrorCallback(format(CREATE_FAILED, draftKey, "Draft key is blank!"));
return CompletableFuture.completedFuture(Optional.empty());
} else {
final BiFunction<U, Throwable, Optional<U>> 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<U> 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
Expand Down

0 comments on commit 1c7d2c7

Please sign in to comment.