Class CustomerServiceImpl
- java.lang.Object
-
- com.commercetools.sync.services.impl.CustomerServiceImpl
-
- All Implemented Interfaces:
CustomerService
public final class CustomerServiceImpl extends java.lang.Object implements CustomerService
-
-
Field Summary
Fields Modifier and Type Field Description protected com.github.benmanes.caffeine.cache.Cache<java.lang.String,java.lang.String>
keyToIdCache
protected static int
MAXIMUM_ALLOWED_UPDATE_ACTIONS
-
Constructor Summary
Constructors Constructor Description CustomerServiceImpl(CustomerSyncOptions syncOptions)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,java.lang.String>>
cacheKeysToIds(java.util.Set<java.lang.String> keysToCache)
Given a set of keys this method caches in-memory a mapping of key -> id only for those keys which are not already cached.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.customers.Customer>>
createCustomer(io.sphere.sdk.customers.CustomerDraft customerDraft)
Given a resource draft of typeCustomerDraft
, this method attempts to create a resourceCustomer
based on the draft, in the CTP project defined by the sync options.java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>>
fetchCachedCustomerId(java.lang.String key)
Given akey
, if it is blank (null/empty), a completed future with an empty optional is returned.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.customers.Customer>>
fetchCustomerByKey(java.lang.String key)
Given a customer key, this method fetches a customer that matches this given key in the CTP project defined in a potentially injectedSphereClient
.java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.customers.Customer>>
fetchMatchingCustomersByKeys(java.util.Set<java.lang.String> customerKeys)
Given aSet
of customer keys, this method fetches a set of all the customers, matching the given set of keys in the CTP project, defined in an injectedSphereClient
.java.util.concurrent.CompletionStage<io.sphere.sdk.customers.Customer>
updateCustomer(io.sphere.sdk.customers.Customer customer, java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.customers.Customer>> updateActions)
Given aCustomer
and aList
<UpdateAction
<Customer
>>, this method issues an update request with these update actions on thisCustomer
in the CTP project defined in a potentially injectedSphereClient
.
-
-
-
Field Detail
-
keyToIdCache
protected final com.github.benmanes.caffeine.cache.Cache<java.lang.String,java.lang.String> keyToIdCache
-
MAXIMUM_ALLOWED_UPDATE_ACTIONS
protected static final int MAXIMUM_ALLOWED_UPDATE_ACTIONS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CustomerServiceImpl
public CustomerServiceImpl(@Nonnull CustomerSyncOptions syncOptions)
-
-
Method Detail
-
cacheKeysToIds
@Nonnull public java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,java.lang.String>> cacheKeysToIds(@Nonnull java.util.Set<java.lang.String> keysToCache)
Description copied from interface:CustomerService
Given a set of keys this method caches in-memory a mapping of key -> id only for those keys which are not already cached.- Specified by:
cacheKeysToIds
in interfaceCustomerService
- Parameters:
keysToCache
- a set of keys to cache.- Returns:
- a map of key to ids of the requested keys.
-
fetchMatchingCustomersByKeys
@Nonnull public java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.customers.Customer>> fetchMatchingCustomersByKeys(@Nonnull java.util.Set<java.lang.String> customerKeys)
Description copied from interface:CustomerService
Given aSet
of customer keys, this method fetches a set of all the customers, matching the given set of keys in the CTP project, defined in an injectedSphereClient
. A mapping of the key to the id of the fetched customers is persisted in an in-memory map.- Specified by:
fetchMatchingCustomersByKeys
in interfaceCustomerService
- Parameters:
customerKeys
- set of customer keys to fetch matching resources by.- Returns:
CompletionStage
<Set
<Customer
>> in which the result of it's completion contains aSet
of all matching customers.
-
fetchCustomerByKey
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.customers.Customer>> fetchCustomerByKey(@Nullable java.lang.String key)
Description copied from interface:CustomerService
Given a customer key, this method fetches a customer that matches this given key in the CTP project defined in a potentially injectedSphereClient
. If there is no matching resource an emptyOptional
will be returned in the returned future. A mapping of the key to the id of the fetched customer is persisted in an in -memory map.- Specified by:
fetchCustomerByKey
in interfaceCustomerService
- Parameters:
key
- the key of the resource to fetch- Returns:
CompletionStage
<Optional
> in which the result of it's completion contains anOptional
that contains the matchingCustomer
if exists, otherwise empty.
-
fetchCachedCustomerId
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedCustomerId(@Nonnull java.lang.String key)
Description copied from interface:CustomerService
Given akey
, if it is blank (null/empty), a completed future with an empty optional is returned. Otherwise this method checks if the cached map of resource keys -> ids contains the given key. If it does, an optional containing the matching id is returned. If the cache doesn't contain the key; this method attempts to fetch the id of the key from the CTP project, caches it and returns aCompletionStage
<Optional
<String
>> in which the result of it's completion could contain anOptional
holding the id or an emptyOptional
if no customer was found in the CTP project with this key.- Specified by:
fetchCachedCustomerId
in interfaceCustomerService
- Parameters:
key
- the key by which a customer id should be fetched from the CTP project.- Returns:
CompletionStage
<Optional
<String
>> in which the result of it's completion could contain anOptional
holding the id or an emptyOptional
if no customer was found in the CTP project with this key.
-
createCustomer
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.customers.Customer>> createCustomer(@Nonnull io.sphere.sdk.customers.CustomerDraft customerDraft)
Description copied from interface:CustomerService
Given a resource draft of typeCustomerDraft
, this method attempts to create a resourceCustomer
based on the draft, in the CTP project defined by the sync options.A completion stage containing an empty option and the error callback will be triggered in those cases:
- the draft has a blank key
- the create request fails on CTP
On the other hand, if the resource gets created successfully on CTP, the created resource's id and key are cached and the method returns a
CompletionStage
in which the result of it's completion contains an instanceOptional
of the resource which was created.- Specified by:
createCustomer
in interfaceCustomerService
- Parameters:
customerDraft
- the resource draft to create a resource based off of.- Returns:
- a
CompletionStage
containing an optional with the created resource if successful otherwise an empty optional.
-
updateCustomer
@Nonnull public java.util.concurrent.CompletionStage<io.sphere.sdk.customers.Customer> updateCustomer(@Nonnull io.sphere.sdk.customers.Customer customer, @Nonnull java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.customers.Customer>> updateActions)
Description copied from interface:CustomerService
Given aCustomer
and aList
<UpdateAction
<Customer
>>, this method issues an update request with these update actions on thisCustomer
in the CTP project defined in a potentially injectedSphereClient
. This method returnsCompletionStage
<Customer
> in which the result of it's completion contains an instance of theCustomer
which was updated in the CTP project.- Specified by:
updateCustomer
in interfaceCustomerService
- Parameters:
customer
- theCustomer
to update.updateActions
- the update actions to update theCustomer
with.- Returns:
CompletionStage
<Customer
> containing as a result of it's completion an instance of theCustomer
which was updated in the CTP project or aSphereException
.
-
-