I'm looking for a way to migrate certain functionality from CommerceTools Java SDK-v1 to SDK-v2.
This functionality allows me to extract the entire product database (around 30k products) into a single list of products (List<Product>
).
This is the code we currently use with SDK v1:
final QueryPredicate qPred = ProductQuery.of().productType().isInIds(ids);
final ProductQuery seedQuery = ProductQuery.of()
.withSort(m -> m.id().sort().asc())
.withLimit(ProjectConstants.PAGE_SIZE)
.withPredicates(qPred)
.withFetchTotal(false);
final CompletionStage<List<Product>> resultStage = findNext(seedQuery, seedQuery, new LinkedList<>(), connCT);
final List<Product> actualProducts = resultStage.toCompletableFuture().join();
final List<Product> javaSortedActual = actualProducts.stream().sorted(Comparator.comparing(p -> p.getId())).collect(toList());
return javaSortedActual;
As you can see, ProductQuery
is used to create the seedQuery that is then used with CompletionStage.findNext
which is the command that retrieves all the product pages one by one and enters them into the complete list.
The problem is that with the SDK v2 the ProductQuery
method no longer exists and I no longer know how I should recover all the products from our database in a single list.
Does anyone know how I can get the same functionality with SDK v2?
Thanks in advance.
You can use the QueryUtils class with it's methods. They provide similar functionality.
public void queryAll() {
ProjectApiRoot apiRoot = createProjectClient();
List<String> ids = QueryUtils.queryAll(apiRoot.productProjections().get(), (productProjections) -> {
return productProjections.stream().map(ProductProjection::getId).collect(Collectors.toList());
}, 100)
.thenApply(lists -> lists.stream().flatMap(List::stream).collect(Collectors.toList()))
.toCompletableFuture()
.join();
}