I need to obtain all the ProductType that have been defined in my commercetools project because I have to use the localized value of the "name" to perform a search in a file system. Basically I need to use the JVM SDK to extract the list of ProductTypes and traverse it.
Can someone give me some clue how to achieve it?
Thanks in advance.
Yep it is pretty feasible using the jvm sdk, here is a code snippet of how to do it
package io.sphere.sdk.deletemeplese;
import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.queries.ProductTypeQuery;
import io.sphere.sdk.queries.PagedQueryResult;
import io.sphere.sdk.test.IntegrationTest;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
public class SomeIntegrationTest extends IntegrationTest {
@Test
public void test(){
final int PAGE_SIZE = 500;
final long totalProductTypes = client().executeBlocking(ProductTypeQuery.of().withLimit(0)).getTotal();
List<ProductType> allProductTypes = IntStream.range(0,(int)(totalProductTypes/PAGE_SIZE) +1)
.mapToObj(i->i)
.map(i -> ProductTypeQuery.of().withLimit(500).withOffset(i*PAGE_SIZE))
.map(client()::executeBlocking)
.map(PagedQueryResult::getResults)
.flatMap(List::stream)
.collect(Collectors.toList());
assertThat(allProductTypes).hasSize((int)totalProductTypes);
}
}
I hope this answers your question.