2

我需要获取已在我的 commercetools 项目中定义的所有 ProductType,因为我必须使用“名称”的本地化值在文件系统中执行搜索。基本上我需要使用 JVM SDK 来提取 ProductTypes 列表并遍历它。

有人可以给我一些线索如何实现它吗?

提前致谢。

4

2 回答 2

2

是的,使用 jvm sdk 是非常可行的,这里是如何做到这一点的代码片段

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);

    }

}

我希望这回答了你的问题。

于 2017-11-28T13:19:32.327 回答
1

您可以使用类QueryExecutionUtils

CompletionStage<List<ProductType>> allProductTypesStage = 
       QueryExecutionUtils.queryAll(client, ProductTypeQuery.of());
于 2017-11-29T07:40:47.560 回答