Module used for importing/syncing InventoryEntries into a commercetools project. It also provides utilities for generating update actions based on the comparison of a InventoryEntry against a InventoryEntryDraft.
-
The sync expects a list of
InventoryEntryDraft
s that have theirsku
fields set, otherwise the sync will trigger anerrorCallback
function set by the user (more on it can be found down below in the options explanations). -
Every inventory entry may have a reference to a supply
Channel
and a reference to theType
of its custom fields. These references are matched by theirkey
s. Therefore, in order for the sync to resolve the actual ids of those references, theirkey
s has to be supplied in the following way:-
Provide the
key
value on theid
field of the reference. This means that callinggetId()
on the reference would return itskey
.Note: When syncing from a source commercetools project, you can use this util which this library provides:
replaceInventoriesReferenceIdsWithKeys
that replaces the references id fields with keys, in order to make them ready for reference resolution by the sync:java // Puts the keys in the reference id fields to prepare for reference resolution final List<InventoryEntryDraft> inventoryEntryDrafts = replaceInventoriesReferenceIdsWithKeys(inventoryEntries);
-
-
Create a
sphereClient
as described here. -
After the
sphereClient
is setup, aInventorySyncOptions
should be be built as follows:
// instantiating a InventorySyncOptions
final InventorySyncOptions inventorySyncOptions = InventorySyncOptionsBuilder.of(sphereClient).build();
More information about Sync Options.
After all the aforementioned points in the previous section have been fulfilled, to run the sync:
// instantiating an inventory sync
final InventorySync inventorySync = new InventorySync(inventorySyncOptions);
// execute the sync on your list of inventories
CompletionStage<InventorySyncStatistics> syncStatisticsStage = inventorySync.sync(inventoryEntryDrafts);
The result of the completing the syncStatisticsStage
in the previous code snippet contains a InventorySyncStatistics
which contains all the stats of the sync process; which includes a report message, the total number of updated, created,
failed, processed inventories and the processing time of the sync in different time units and in a
human readable format.
final InventorySyncStatistics stats = syncStatisticsStage.toCompletebleFuture().join();
stats.getReportMessage();
/*"Summary: 25 inventory entries were processed in total (9 created, 5 updated, 2 failed to sync)."*/
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 known by the sync which batch is going to be the last one supplied.
More examples of how to use the sync here.
Make sure to read the Important Usage Tips for optimal performance.
A utility method provided by the library to compare an InventoryEntry with a new InventoryEntryDraft and results in a list of InventoryEntry update actions.
List<UpdateAction<InventoryEntry>> updateActions = InventorySyncUtils.buildActions(oldEntry, newEntry, inventorySyncOptions);
Examples of its usage can be found in the tests here.
Utility methods provided by the library to compare the specific fields of an InventoryEntry and a new InventoryEntryDraft, and in turn builds
the update action. One example is the buildChangeQuantityAction
which compares quantities:
Optional<UpdateAction<InventoryEntry>> updateAction = buildChangeQuantityAction(oldEntry, newEntry);
- The library will sync all field types of custom fields, except
ReferenceType
. #87.