Utility which provides API for building CTP category update actions and category synchronisation.
-
The sync expects a list of non-null
CategoryDraftobjects that have theirkeyfields set to match the categories from the source to the target. Also the target project is expected to have thekeyfields set, otherwise they won't be matched. -
Every category may have a reference to a
parent categoryand a reference to theTypeof its custom fields. Categories and Types are matched by theirkeyTherefore, in order for the sync to resolve the actual ids of those references, thekeyof theType/parentCategoryhas to be supplied in one of two ways:- Provide the
keyvalue on theidfield of the reference. This means that callinggetId()on the reference would return itskey. Note that the library will check that thiskeyis not provided inUUIDformat by default. However, if you want to provide thekeyinUUIDformat, you can set it through the sync options. - Provide the reference expanded. This means that calling
getObj()on the reference should not returnnull, but return theTypeobject, from which the itskeycan be directly accessible.
Note: This library provides you with a utility method
replaceCategoriesReferenceIdsWithKeysthat replaces the references id fields with keys, in order to make them ready for reference resolution by the sync:// Puts the keys in the reference id fields to prepare for reference resolution final List<CategoryDraft> categoryDrafts = replaceCategoriesReferenceIdsWithKeys(categories);
Example of its usage can be found here.
- Provide the
-
It is an important responsibility of the user of the library to instantiate a
sphereClientthat has the following properties:- Limits the amount of concurrent requests done to CTP. This can be done by decorating the
sphereClientwith QueueSphereClientDecorator - Retries on 5xx errors with a retry strategy. This can be achieved by decorating the
sphereClientwith the RetrySphereClientDecorator
You can use the same client instantiating used in the integration tests for this library found here.
- Limits the amount of concurrent requests done to CTP. This can be done by decorating the
-
After the
sphereClientis setup, aCategorySyncOptionsshould be be built as follows:
// instantiating a CategorySyncOptions
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(sphereClient).build();The options can be used to provide additional optional configuration for the sync as well:
-
errorCallBacka callback that is called whenever an event occurs during the sync process that represents an error. Currently, these events. -
warningCallBacka callback that is called whenever an event occurs during the sync process that represents a warning. Currently, these events. -
beforeUpdateCallbacka filter function which can be applied on a generated list of update actions. It allows the user to intercept category update and modify (add/remove) update actions just before they are send to CTP API. -
beforeCreateCallbacka filter function which can be applied on a category draft before a request to create it on CTP is issued. It allows the user to intercept category create requests modify the draft before the create request is sent to CTP API. -
allowUuida flag, if set totrue, enables the user to use keys with UUID format for references. By default, it is set tofalse.
Example of options usage, that sets the error and warning callbacks to output the message to the log error and warning streams, would look as follows:
final Logger logger = LoggerFactory.getLogger(MySync.class);
final CategorySyncOptions categorySyncOptions = CategorySyncOptionsBuilder.of(sphereClient)
.errorCallBack(logger::error)
.warningCallBack(logger::warn)
.build();After all the aforementioned points in the previous section have been fulfilled, to run the sync:
// instantiating a category sync
final CategorySync categorySync = new CategorySync(categorySyncOptions);
// execute the sync on your list of categories
CompletionStage<CategorySyncStatistics> syncStatisticsStage = categorySync.sync(categoryDrafts);The result of the completing the syncStatisticsStage in the previous code snippet contains a CategorySyncStatistics
which contains all the stats of the sync process; which includes a report message, the total number of updated, created,
failed, processed categories and the processing time of the last sync batch in different time units and in a
human readable format.
final CategorySyncStatistics stats = syncStatisticsStage.toCompletebleFuture().join();
stats.getReportMessage();
/*"Summary: 2000 categories were processed in total (1000 created, 995 updated, 5 failed to sync and 0 categories with a missing parent)."*/Note The statistics object contains the processing time of the last batch only. This is due to two reasons:
- The sync processing time should not take into account the time between supplying batches to the sync.
- It is not not known by the sync which batch is going to be the last one supplied.
More examples of how to use the sync
A utility method provided by the library to compare a Category with a new CategoryDraft and results in a list of category update actions.
List<UpdateAction<Category>> updateActions = CategorySyncUtils.buildActions(category, categoryDraft, categorySyncOptions);Examples of its usage can be found in the tests here.
Utility methods provided by the library to compare the specific fields of a Category and a new CategoryDraft, and in turn builds
the update action. One example is the buildChangeNameUpdateAction which compares names:
Optional<UpdateAction<Category>> updateAction = buildChangeNameUpdateAction(oldCategory, categoryDraft);More examples of those utils for different fields can be found here.