Class StateServiceImpl
- java.lang.Object
-
- com.commercetools.sync.services.impl.StateServiceImpl
-
- All Implemented Interfaces:
StateService
public final class StateServiceImpl extends java.lang.Object implements StateService
-
-
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 StateServiceImpl(StateSyncOptions 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> keys)
Filters out the keys which are already cached and fetches only the not-cached state keys from the CTP project defined in an injectedSphereClient
and stores a mapping for every state to id in the cached map of keys -> ids and returns this cached map.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.states.State>>
createState(io.sphere.sdk.states.StateDraft stateDraft)
Given a resource draft of typeStateDraft
, this method attempts to create a resourceState
based on it in the CTP project defined by the sync options.java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>>
fetchCachedStateId(java.lang.String key)
Given akey
, this method first checks if a cached map of state keys -> ids is not empty.java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.states.State>>
fetchMatchingStatesByKeys(java.util.Set<java.lang.String> stateKeys)
Given aSet
of state keys, this method fetches a set of all the states, matching given set of keys in the CTP project, defined in an injectedSphereClient
.java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.states.State>>
fetchMatchingStatesByKeysWithTransitions(java.util.Set<java.lang.String> stateKeys)
Given aSet
of state keys, this method fetches a set of all the states with expanded transitions, matching given set of keys in the CTP project, defined in an injectedSphereClient
.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.states.State>>
fetchState(java.lang.String key)
Given a state key, this method fetches a state that matches given key in the CTP project defined in a potentially injectedSphereClient
.java.util.concurrent.CompletionStage<io.sphere.sdk.states.State>
updateState(io.sphere.sdk.states.State state, java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.states.State>> updateActions)
Given aState
and aList
<UpdateAction
<State
>>, this method issues an update request with these update actions on thisState
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
-
StateServiceImpl
public StateServiceImpl(@Nonnull StateSyncOptions 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> keys)
Description copied from interface:StateService
Filters out the keys which are already cached and fetches only the not-cached state keys from the CTP project defined in an injectedSphereClient
and stores a mapping for every state to id in the cached map of keys -> ids and returns this cached map.Note: If all the supplied keys are already cached, the cached map is returned right away with no request to CTP.
- Specified by:
cacheKeysToIds
in interfaceStateService
- Parameters:
keys
- the state keys to fetch and cache the ids for.- Returns:
CompletionStage
<Map
> in which the result of it's completion contains a map of all state keys -> ids
-
fetchCachedStateId
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedStateId(@Nullable java.lang.String key)
Description copied from interface:StateService
Given akey
, this method first checks if a cached map of state keys -> ids is not empty. If not, it returns a completed future that contains an optional that contains what this key maps to in the cache. If the cache is empty, the method populates the cache with the mapping of all state keys to ids in the CTP project, by querying the CTP project for all states.After that, the method returns a
CompletionStage
<Optional
<String
>> in which the result of it's completion could contain anOptional
with the id inside of it or an emptyOptional
if noState
was found in the CTP project with this key.- Specified by:
fetchCachedStateId
in interfaceStateService
- Parameters:
key
- the key by which aState
id should be fetched from the CTP project.- Returns:
CompletionStage
<Optional
<String
>> in which the result of its completion could contain anOptional
with the id inside of it or an emptyOptional
if noState
was found in the CTP project with this key.
-
fetchMatchingStatesByKeys
@Nonnull public java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.states.State>> fetchMatchingStatesByKeys(@Nonnull java.util.Set<java.lang.String> stateKeys)
Description copied from interface:StateService
Given aSet
of state keys, this method fetches a set of all the states, matching given set of keys in the CTP project, defined in an injectedSphereClient
. A mapping of the key to the id of the fetched states is persisted in an in-memory map.- Specified by:
fetchMatchingStatesByKeys
in interfaceStateService
- Parameters:
stateKeys
- set of state keys to fetch matching states by.- Returns:
CompletionStage
<Map
> in which the result of it's completion contains aSet
of all matching states.
-
fetchMatchingStatesByKeysWithTransitions
@Nonnull public java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.states.State>> fetchMatchingStatesByKeysWithTransitions(@Nonnull java.util.Set<java.lang.String> stateKeys)
Description copied from interface:StateService
Given aSet
of state keys, this method fetches a set of all the states with expanded transitions, matching given set of keys in the CTP project, defined in an injectedSphereClient
. A mapping of the key to the id of the fetched states is persisted in an in-memory map.- Specified by:
fetchMatchingStatesByKeysWithTransitions
in interfaceStateService
- Parameters:
stateKeys
- set of state keys to fetch matching states by.- Returns:
CompletionStage
<Map
> in which the result of it's completion contains aSet
of all matching states with expanded transitions.
-
fetchState
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.states.State>> fetchState(@Nullable java.lang.String key)
Description copied from interface:StateService
Given a state key, this method fetches a state that matches given key in the CTP project defined in a potentially injectedSphereClient
. If there is no matching state an emptyOptional
will be returned in the returned future. A mapping of the key to the id of the fetched state is persisted in an in -memory map.- Specified by:
fetchState
in interfaceStateService
- Parameters:
key
- the key of the state to fetch.- Returns:
CompletionStage
<Optional
> in which the result of it's completion contains anOptional
that contains the matchingState
if exists, otherwise empty.
-
createState
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.states.State>> createState(@Nonnull io.sphere.sdk.states.StateDraft stateDraft)
Description copied from interface:StateService
Given a resource draft of typeStateDraft
, this method attempts to create a resourceState
based on it 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, then 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:
createState
in interfaceStateService
- Parameters:
stateDraft
- 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.
-
updateState
@Nonnull public java.util.concurrent.CompletionStage<io.sphere.sdk.states.State> updateState(@Nonnull io.sphere.sdk.states.State state, @Nonnull java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.states.State>> updateActions)
Description copied from interface:StateService
Given aState
and aList
<UpdateAction
<State
>>, this method issues an update request with these update actions on thisState
in the CTP project defined in a potentially injectedSphereClient
. This method returnsCompletionStage
<State
> in which the result of it's completion contains an instance of theState
which was updated in the CTP project.- Specified by:
updateState
in interfaceStateService
- Parameters:
state
- theState
to update.updateActions
- the update actions to update theState
with.- Returns:
CompletionStage
<State
> containing as a result of it's completion an instance of theState
which was updated in the CTP project or aSphereException
.
-
-