Package com.commercetools.sync.commons
Class BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT extends com.commercetools.api.models.ResourceUpdateAction<ResourceUpdateActionT>>
- java.lang.Object
-
- com.commercetools.sync.commons.BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT>
-
- Type Parameters:
ResourceT
- Resource (e.g.Customer
)ResourceDraftT
- Resource Draft (e.g.CustomerDraft
)ResourceUpdateActionT
- extends ResourceUpdateAction (e.gCustomerChangeEmailAction
- Direct Known Subclasses:
CartDiscountSyncOptions
,CategorySyncOptions
,CustomerSyncOptions
,CustomObjectSyncOptions
,InventorySyncOptions
,ProductSyncOptions
,ProductTypeSyncOptions
,ShoppingListSyncOptions
,StateSyncOptions
,TaxCategorySyncOptions
,TypeSyncOptions
public class BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT extends com.commercetools.api.models.ResourceUpdateAction<ResourceUpdateActionT>> extends java.lang.Object
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseSyncOptions(com.commercetools.api.client.ProjectApiRoot ctpClient, QuadConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>,java.util.List<ResourceUpdateActionT>> errorCallback, TriConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>> warningCallback, int batchSize, TriFunction<java.util.List<ResourceUpdateActionT>,ResourceDraftT,ResourceT,java.util.List<ResourceUpdateActionT>> beforeUpdateCallback, java.util.function.Function<ResourceDraftT,ResourceDraftT> beforeCreateCallback, long cacheSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Optional<ResourceDraftT>
applyBeforeCreateCallback(ResourceDraftT newResourceDraft)
Given a new resource draft of typeV
this method applies thebeforeCreateCallback
function which is set tothis
instance of theBaseSyncOptions
and returns the result.java.util.List<ResourceUpdateActionT>
applyBeforeUpdateCallback(java.util.List<ResourceUpdateActionT> updateActions, ResourceDraftT newResourceDraft, ResourceT oldResource)
Given aList
ofResourceUpdateAction
, a new resource draft of typeV
and the old existing resource of the typeU
, this method applies thebeforeUpdateCallback
function which is set tothis
instance of theBaseSyncOptions
and returns the result.void
applyErrorCallback(SyncException syncException)
void
applyErrorCallback(SyncException exception, ResourceT oldResource, ResourceDraftT newResourceDraft, java.util.List<ResourceUpdateActionT> updateActions)
Given anerrorMessage
and anexception
, this method calls theerrorCallback
function which is set tothis
instance of theBaseSyncOptions
.void
applyErrorCallback(java.lang.String errorMessage)
void
applyWarningCallback(SyncException exception, ResourceT oldResource, ResourceDraftT newResourceDraft)
Given anexception
,oldResource
andnewResourceDraft
this method calls thewarningCallback
function which is set tothis
instance of theBaseSyncOptions
.int
getBatchSize()
Gets the batch size used in the sync process.java.util.function.Function<ResourceDraftT,ResourceDraftT>
getBeforeCreateCallback()
TriFunction<java.util.List<ResourceUpdateActionT>,ResourceDraftT,ResourceT,java.util.List<ResourceUpdateActionT>>
getBeforeUpdateCallback()
Returns thebeforeUpdateCallback
TriFunction
<List
<ResourceUpdateAction
<U
>>,V
,U
,List
<ResourceUpdateAction
<U
>>> function set tothis
BaseSyncOptions
.long
getCacheSize()
Gets the cache size used in the sync process.com.commercetools.api.client.ProjectApiRoot
getCtpClient()
Returns theProjectApiRoot
responsible for interaction with the target CTP project.QuadConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>,java.util.List<ResourceUpdateActionT>>
getErrorCallback()
Returns theerrorCallback
QuadConsumer
<SyncException
,Optional
<V
>,Optional
<U
>,List
<ResourceUpdateAction
<U
>>> function set tothis
BaseSyncOptions
.TriConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>>
getWarningCallback()
Returns thewarningCallback
TriConsumer
<SyncException
,Optional
<V
>,Optional
<U
>> function set tothis
BaseSyncOptions
.
-
-
-
Constructor Detail
-
BaseSyncOptions
protected BaseSyncOptions(@Nonnull com.commercetools.api.client.ProjectApiRoot ctpClient, @Nullable QuadConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>,java.util.List<ResourceUpdateActionT>> errorCallback, @Nullable TriConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>> warningCallback, int batchSize, @Nullable TriFunction<java.util.List<ResourceUpdateActionT>,ResourceDraftT,ResourceT,java.util.List<ResourceUpdateActionT>> beforeUpdateCallback, @Nullable java.util.function.Function<ResourceDraftT,ResourceDraftT> beforeCreateCallback, long cacheSize)
-
-
Method Detail
-
getCtpClient
public com.commercetools.api.client.ProjectApiRoot getCtpClient()
Returns theProjectApiRoot
responsible for interaction with the target CTP project.- Returns:
- the
ProjectApiRoot
responsible for interaction with the target CTP project.
-
getErrorCallback
@Nullable public QuadConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>,java.util.List<ResourceUpdateActionT>> getErrorCallback()
Returns theerrorCallback
QuadConsumer
<SyncException
,Optional
<V
>,Optional
<U
>,List
<ResourceUpdateAction
<U
>>> function set tothis
BaseSyncOptions
. It represents the callback that is called whenever an event occurs during the sync process that represents an error.- Returns:
- the
errorCallback
QuadConsumer
<SyncException
,Optional
<V
>,Optional
<U
>,List
<ResourceUpdateAction
<U
>> function set tothis
BaseSyncOptions
-
getWarningCallback
@Nullable public TriConsumer<SyncException,java.util.Optional<ResourceDraftT>,java.util.Optional<ResourceT>> getWarningCallback()
Returns thewarningCallback
TriConsumer
<SyncException
,Optional
<V
>,Optional
<U
>> function set tothis
BaseSyncOptions
. It represents the callback that is called whenever an event occurs during the sync process that represents a warning.- Returns:
- the
warningCallback
TriConsumer
<SyncException
,Optional
<V
>,Optional
<U
>> function set tothis
BaseSyncOptions
-
applyWarningCallback
public void applyWarningCallback(@Nonnull SyncException exception, @Nullable ResourceT oldResource, @Nullable ResourceDraftT newResourceDraft)
Given anexception
,oldResource
andnewResourceDraft
this method calls thewarningCallback
function which is set tothis
instance of theBaseSyncOptions
. If therewarningCallback
is null, this method does nothing.- Parameters:
exception
- the exception to supply to thewarningCallback
function.oldResource
- the old resource that is being compared to the new draft.newResourceDraft
- the new resource draft that is being compared to the old resource.
-
applyErrorCallback
public void applyErrorCallback(@Nonnull SyncException exception, @Nullable ResourceT oldResource, @Nullable ResourceDraftT newResourceDraft, @Nullable java.util.List<ResourceUpdateActionT> updateActions)
Given anerrorMessage
and anexception
, this method calls theerrorCallback
function which is set tothis
instance of theBaseSyncOptions
. If thereerrorCallback
is null, this method does nothing.- Parameters:
exception
-Throwable
instance to supply as first param to theerrorCallback
function.oldResource
- the old resource that is being compared to the new draft.newResourceDraft
- the new resource draft that is being compared to the old resource.updateActions
- the list of update actions.
-
applyErrorCallback
public void applyErrorCallback(@Nonnull SyncException syncException)
- Parameters:
syncException
-Throwable
instance to supply as first param to theerrorCallback
function.- See Also:
applyErrorCallback(SyncException exception, Object oldResource, Object newResource, List updateActions)
-
applyErrorCallback
public void applyErrorCallback(@Nonnull java.lang.String errorMessage)
- Parameters:
errorMessage
- the error message to supply as part of first param to theerrorCallback
function.- See Also:
applyErrorCallback(SyncException exception, Object oldResource, Object newResource, List updateActions)
-
getBatchSize
public int getBatchSize()
Gets the batch size used in the sync process. During the sync there is a need for fetching existing resources so that they can be compared with the new resource drafts. That's why the input is sliced into batches and then processed. It allows to reduce the query size for fetching all resources processed in one batch. E.g. value of 30 means that 30 entries from input list would be accumulated and one API call will be performed for fetching entries responding to them. Then comparison and sync are performed.This batch size is set to 30 by default.
- Returns:
- option that indicates capacity of batch of resources to process.
-
getCacheSize
public long getCacheSize()
Gets the cache size used in the sync process. To increase performance during the sync some resource keys / ids are cached which are required for resolving references. To keep the cache performant old entries are evicted when a certain size is reached.This cache size is set to 100.000 by default.
- Returns:
- option that indicates capacity of cache of resource keys
-
getBeforeUpdateCallback
@Nullable public TriFunction<java.util.List<ResourceUpdateActionT>,ResourceDraftT,ResourceT,java.util.List<ResourceUpdateActionT>> getBeforeUpdateCallback()
Returns thebeforeUpdateCallback
TriFunction
<List
<ResourceUpdateAction
<U
>>,V
,U
,List
<ResourceUpdateAction
<U
>>> function set tothis
BaseSyncOptions
. It represents a callback function which is applied (if set) on the generated list of update actions to produce a resultant list after the filter function has been applied.- Returns:
- the
beforeUpdateCallback
TriFunction
<List
<ResourceUpdateAction
<U
>>,V
,U
,List
<ResourceUpdateAction
<U
>>> function set tothis
BaseSyncOptions
.
-
getBeforeCreateCallback
@Nullable public java.util.function.Function<ResourceDraftT,ResourceDraftT> getBeforeCreateCallback()
Returns thebeforeCreateCallback
Function
<V
,Optional
<V
>> function set tothis
BaseSyncOptions
. It represents a callback function which is applied (if set) on the resource draft of typeV
before it is created to produce a resultant draft after the filter function has been applied.- Returns:
- the
beforeUpdateCallback
Function
<V
,Optional
<V
>> function set tothis
BaseSyncOptions
.
-
applyBeforeUpdateCallback
@Nonnull public java.util.List<ResourceUpdateActionT> applyBeforeUpdateCallback(@Nonnull java.util.List<ResourceUpdateActionT> updateActions, @Nonnull ResourceDraftT newResourceDraft, @Nonnull ResourceT oldResource)
Given aList
ofResourceUpdateAction
, a new resource draft of typeV
and the old existing resource of the typeU
, this method applies thebeforeUpdateCallback
function which is set tothis
instance of theBaseSyncOptions
and returns the result. If thebeforeUpdateCallback
is null orupdateActions
is empty, this method does nothing to the supplied list ofupdateActions
and returns the same list. If the result of the callback is null, an empty list is returned.- Parameters:
updateActions
- the list of update actions to apply thebeforeUpdateCallback
function on.newResourceDraft
- the new resource draft that is being compared to the old resource.oldResource
- the old resource that is being compared to the new draft.- Returns:
- a list of update actions after applying the
beforeUpdateCallback
function on. If thebeforeUpdateCallback
function is null orupdateActions
is empty, the supplied list ofupdateActions
is returned as is. If the return of the callback is null, an empty list is returned.
-
applyBeforeCreateCallback
@Nonnull public java.util.Optional<ResourceDraftT> applyBeforeCreateCallback(@Nonnull ResourceDraftT newResourceDraft)
Given a new resource draft of typeV
this method applies thebeforeCreateCallback
function which is set tothis
instance of theBaseSyncOptions
and returns the result.- If the
beforeCreateCallback
is null, this method does nothing to the supplied resource draft and returns it as is, wrapped in an optional. - If the return of
beforeCreateCallback
is null, an empty optional is returned. - Otherwise, the result of the
beforeCreateCallback
is returned in the optional.
- Parameters:
newResourceDraft
- the new resource draft that should be created.- Returns:
- an optional containing the resultant resource draft after applying the
beforeCreateCallback
function on. If thebeforeCreateCallback
function is null, the supplied resource draft is returned as is, wrapped in an optional.
- If the
-
-