001 002package com.commercetools.api.client; 003 004import java.util.List; 005import java.util.concurrent.CompletionStage; 006import java.util.function.Consumer; 007import java.util.function.Function; 008 009import javax.annotation.Nonnull; 010 011import com.commercetools.api.models.DomainResource; 012import com.commercetools.api.models.PagedQueryResourceRequest; 013import com.commercetools.api.models.ResourcePagedQueryResponse; 014 015public class QueryUtils { 016 static int DEFAULT_PAGE_SIZE = 500; 017 /** 018 * <p>Queries all elements matching a query by using a limit based pagination with a combination of 019 * id sorting and a page size 500. More on the algorithm can be found here: 020 * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p> 021 * 022 * <p>The method takes a callback {@link java.util.function.Function} that returns a result of type {@code <S>} that 023 * is returned on every page of elements queried. Eventually, the method returns a {@link 024 * java.util.concurrent.CompletionStage} that contains a list of all the results of the callbacks returned from every 025 * page. 026 * 027 * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in 028 * parallel. 029 * 030 * @param query query containing predicates and expansion paths 031 * @param pageMapper callback function that is called on every page queried 032 * @param <TElement> type of one query result element 033 * @param <TMethod> type of the query 034 * @param <TResult> type of the result 035 * @param <S> type of the returned result of the callback function on every page. 036 * @return a completion stage containing a list of mapped pages as a result. 037 */ 038 @Nonnull 039 public static <TMethod extends PagedQueryResourceRequest<TMethod, TResult>, TResult extends ResourcePagedQueryResponse<TElement>, TElement extends DomainResource<TElement>, S> CompletionStage<List<S>> queryAll( 040 @Nonnull final PagedQueryResourceRequest<TMethod, TResult> query, 041 @Nonnull final Function<List<TElement>, S> pageMapper) { 042 return queryAll(query, pageMapper, DEFAULT_PAGE_SIZE); 043 } 044 045 /** 046 * <p>Queries all elements matching a query by using a limit based pagination with a combination of 047 * id sorting and a page size 500. More on the algorithm can be found here: 048 * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p> 049 * 050 * <p>The method takes a consumer {@link Consumer} that is applied on every page of elements 051 * queried. 052 * 053 * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in 054 * parallel. 055 * 056 * @param query query containing predicates and expansion paths 057 * @param pageConsumer consumer applied on every page queried 058 * @param <TElement> type of one query result element 059 * @param <TMethod> type of the query 060 * @param <TResult> type of the result 061 * @return a completion stage containing void as a result after the consumer was applied on all 062 * pages. 063 */ 064 @Nonnull 065 public static <TMethod extends PagedQueryResourceRequest<TMethod, TResult>, TResult extends ResourcePagedQueryResponse<TElement>, TElement extends DomainResource<TElement>> CompletionStage<Void> queryAll( 066 @Nonnull final PagedQueryResourceRequest<TMethod, TResult> query, 067 @Nonnull final Consumer<List<TElement>> pageConsumer) { 068 return queryAll(query, pageConsumer, DEFAULT_PAGE_SIZE); 069 } 070 071 /** 072 * <p>Queries all elements matching a query by using a limit based pagination with a combination of 073 * id sorting and the supplied {@code pageSize}. More on the algorithm can be found here: 074 * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p> 075 * 076 * <p>The method takes a callback {@link Function} that returns a result of type {@code <S>} that 077 * is returned on every page of elements queried. Eventually, the method returns a {@link 078 * CompletionStage} that contains a list of all the results of the callbacks returned from every 079 * page. 080 * 081 * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in 082 * parallel. 083 * 084 * @param query query containing predicates and expansion paths 085 * @param pageMapper callback function that is called on every page queried 086 * @param <TElement> type of one query result element 087 * @param <TMethod> type of the query 088 * @param <TResult> type of the result 089 * @param <S> type of the returned result of the callback function on every page. 090 * @param pageSize the page size. 091 * @return a completion stage containing a list of mapped pages as a result. 092 */ 093 @Nonnull 094 public static <TMethod extends PagedQueryResourceRequest<TMethod, TResult>, TResult extends ResourcePagedQueryResponse<TElement>, TElement extends DomainResource<TElement>, S> CompletionStage<List<S>> queryAll( 095 @Nonnull final PagedQueryResourceRequest<TMethod, TResult> query, 096 @Nonnull final Function<List<TElement>, S> pageMapper, final int pageSize) { 097 final QueryAll<TMethod, TResult, TElement, S> queryAll = QueryAll.of(query, pageSize); 098 return queryAll.run(pageMapper); 099 } 100 101 /** 102 * <p>Queries all elements matching a query by using a limit based pagination with a combination of 103 * id sorting and the supplied {@code pageSize}. More on the algorithm can be found here: 104 * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p> 105 * 106 * <p>The method takes a {@link java.util.function.Consumer} that is applied on every page of the queried elements. 107 * 108 * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in 109 * parallel. 110 * 111 * @param query query containing predicates and expansion paths 112 * @param pageConsumer consumer applied on every page queried 113 * @param <TElement> type of one query result element 114 * @param <TMethod> type of the query 115 * @param <TResult> type of the result 116 * @param pageSize the page size 117 * @return a completion stage containing void as a result after the consumer was applied on all 118 * pages. 119 */ 120 @Nonnull 121 public static <TMethod extends PagedQueryResourceRequest<TMethod, TResult>, TResult extends ResourcePagedQueryResponse<TElement>, TElement extends DomainResource<TElement>> CompletionStage<Void> queryAll( 122 @Nonnull final PagedQueryResourceRequest<TMethod, TResult> query, 123 @Nonnull final Consumer<List<TElement>> pageConsumer, final int pageSize) { 124 final QueryAll<TMethod, TResult, TElement, Void> queryAll = QueryAll.of(query, pageSize); 125 return queryAll.run(pageConsumer); 126 } 127}