Package com.commercetools.sync.commons
Class BaseSync<ResourceT extends com.commercetools.api.models.common.BaseResource,ResourceDraftT,ResourceUpdateActionT extends com.commercetools.api.models.ResourceUpdateAction<ResourceUpdateActionT>,SyncStatisticsT extends BaseSyncStatistics<SyncStatisticsT>,SyncOptionsT extends BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT>>
- java.lang.Object
-
- com.commercetools.sync.commons.BaseSync<ResourceT,ResourceDraftT,ResourceUpdateActionT,SyncStatisticsT,SyncOptionsT>
-
- Direct Known Subclasses:
CartDiscountSync,CategorySync,CustomerSync,CustomObjectSync,InventorySync,ProductSync,ProductTypeSync,ShoppingListSync,StateSync,TaxCategorySync,TypeSync
public abstract class BaseSync<ResourceT extends com.commercetools.api.models.common.BaseResource,ResourceDraftT,ResourceUpdateActionT extends com.commercetools.api.models.ResourceUpdateAction<ResourceUpdateActionT>,SyncStatisticsT extends BaseSyncStatistics<SyncStatisticsT>,SyncOptionsT extends BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT>> extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description protected SyncStatisticsTstatisticsprotected SyncOptionsTsyncOptions
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseSync(SyncStatisticsT statistics, SyncOptionsT syncOptions)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static <ResultT> ResultTexecuteSupplierIfConcurrentModificationException(java.lang.Throwable exception, java.util.function.Supplier<ResultT> onConcurrentModificationSupplier, java.util.function.Supplier<ResultT> onOtherExceptionSupplier)This method checks if the suppliedexceptionis an instance ofConcurrentModificationException.SyncStatisticsTgetStatistics()Returns an instance of type U which is a subclass ofBaseSyncStatisticscontaining all the stats of the sync process; which includes a report message, the total number of update, created, failed, processed resources and the processing time of the sync in different time units and in a human readable format.SyncOptionsTgetSyncOptions()protected voidhandleError(java.lang.String errorMessage, java.lang.Throwable exception, ResourceT oldResource, ResourceDraftT newResourceDraft, java.util.List<ResourceUpdateActionT> updateActions, int failedTimes)This method calls the optional error callback specified in thesyncOptionsand updates thestatisticsinstance by incrementing the total number of failed resources S to sync.protected abstract java.util.concurrent.CompletionStage<SyncStatisticsT>process(java.util.List<ResourceDraftT> resourceDrafts)Given a list of resource (e.g.protected abstract java.util.concurrent.CompletionStage<SyncStatisticsT>processBatch(java.util.List<ResourceDraftT> batch)java.util.concurrent.CompletionStage<SyncStatisticsT>sync(java.util.List<ResourceDraftT> resourceDrafts)Given a list of resource (e.g.protected java.util.concurrent.CompletionStage<SyncStatisticsT>syncBatches(java.util.List<java.util.List<ResourceDraftT>> batches, java.util.concurrent.CompletionStage<SyncStatisticsT> result)Given a list of resource (e.g.
-
-
-
Field Detail
-
statistics
protected final SyncStatisticsT extends BaseSyncStatistics<SyncStatisticsT> statistics
-
syncOptions
protected final SyncOptionsT extends BaseSyncOptions<ResourceT,ResourceDraftT,ResourceUpdateActionT> syncOptions
-
-
Constructor Detail
-
BaseSync
protected BaseSync(@Nonnull SyncStatisticsT statistics, @Nonnull SyncOptionsT syncOptions)
-
-
Method Detail
-
process
protected abstract java.util.concurrent.CompletionStage<SyncStatisticsT> process(@Nonnull java.util.List<ResourceDraftT> resourceDrafts)
Given a list of resource (e.g. categories, products, etc..) drafts. This method compares each new resource in this list with it's corresponding old resource in a given CTP project, and in turn it either issues update actions on the existing resource if it exists or create it if it doesn't.- Parameters:
resourceDrafts- the list of new resources as drafts.- Returns:
- an instance of
CompletionStage<SyncStatisticsT> which contains as a result an instance ofSyncStatisticsTwhich is a subclass ofBaseSyncStatisticsrepresenting thestatisticsinstance attribute ofthisBaseSync.
-
sync
public java.util.concurrent.CompletionStage<SyncStatisticsT> sync(@Nonnull java.util.List<ResourceDraftT> resourceDrafts)
Given a list of resource (e.g. categories, products, etc..) drafts. This method compares each new resource in this list with it's corresponding old resource in a given CTP project, and in turn it either issues update actions on the existing resource if it exists or create it if it doesn't.The time before and after the actual sync process starts is recorded in the
BaseSyncStatisticscontainer so that the total processing time is computed in the statistics.- Parameters:
resourceDrafts- the list of new resources as drafts.- Returns:
- an instance of
CompletionStage<SyncStatisticsT> which contains as a result an instance ofSyncStatisticsTwhich is a subclass ofBaseSyncStatisticsrepresenting thestatisticsinstance attribute ofthisBaseSync.
-
getStatistics
@Nonnull public SyncStatisticsT getStatistics()
Returns an instance of type U which is a subclass ofBaseSyncStatisticscontaining all the stats of the sync process; which includes a report message, the total number of update, created, failed, processed resources and the processing time of the sync in different time units and in a human readable format.- Returns:
- a statistics object for the sync process.
-
getSyncOptions
public SyncOptionsT getSyncOptions()
-
syncBatches
protected java.util.concurrent.CompletionStage<SyncStatisticsT> syncBatches(@Nonnull java.util.List<java.util.List<ResourceDraftT>> batches, @Nonnull java.util.concurrent.CompletionStage<SyncStatisticsT> result)
Given a list of resource (e.g. categories, products, etc.. batches represented by aList<List> of resources, this method recursively callsprocessBatch(List)on each batch, then removes it, until there are no more batches, in other words, all batches have been synced.- Parameters:
batches- the batches of resources to sync.result- in the first call of this recursive method, this result is normally a completed future, it used from within the method to recursively sync each batch once the previous batch has finished syncing.- Returns:
- an instance of
CompletionStage<SyncStatisticsT> which contains as a result an instance ofBaseSyncStatisticsrepresenting thestatisticsof the sync process executed on the given list of batches.
-
processBatch
protected abstract java.util.concurrent.CompletionStage<SyncStatisticsT> processBatch(@Nonnull java.util.List<ResourceDraftT> batch)
-
executeSupplierIfConcurrentModificationException
protected static <ResultT> ResultT executeSupplierIfConcurrentModificationException(@Nonnull java.lang.Throwable exception, @Nonnull java.util.function.Supplier<ResultT> onConcurrentModificationSupplier, @Nonnull java.util.function.Supplier<ResultT> onOtherExceptionSupplier)This method checks if the suppliedexceptionis an instance ofConcurrentModificationException. If it is, then it executes the suppliedonConcurrentModificationSupplierSupplier. Otherwise, if it is not an instance of aConcurrentModificationExceptionthen it executes the otheronOtherExceptionSupplierSupplier. Regardless, which supplier is executed the results of either is the result of this method.- Type Parameters:
ResultT- the type of the result of the suppliers and this method.- Parameters:
exception- the commercetools exception to check if isConcurrentModificationException.onConcurrentModificationSupplier- the supplier to execute if theexceptionis aConcurrentModificationException.onOtherExceptionSupplier- the supplier to execute if theexceptionis not aConcurrentModificationException.- Returns:
- the result of the executed supplier.
-
handleError
protected void handleError(@Nonnull java.lang.String errorMessage, @Nullable java.lang.Throwable exception, ResourceT oldResource, ResourceDraftT newResourceDraft, java.util.List<ResourceUpdateActionT> updateActions, int failedTimes)This method calls the optional error callback specified in thesyncOptionsand updates thestatisticsinstance by incrementing the total number of failed resources S to sync.- Parameters:
errorMessage- The error message describing the reason(s) of failure.exception- The exception that called caused the failure, if any.oldResource- the commercetools resource which could be updated.newResourceDraft- the commercetools resource draft where we get the new data.updateActions- the update actions to update the resource with.failedTimes- The number of times that the failed cart discount statistic counter is incremented.
-
-