12
12
import com .commercetools .api .models .ResourcePagedQueryResponse ;
13
13
14
14
public class QueryUtils {
15
+
16
+ public enum SortOrder {
17
+ ASCENDING ("asc" ), DESCENDING ("desc" );
18
+
19
+ private final String value ;
20
+
21
+ SortOrder (final String value ) {
22
+ this .value = value ;
23
+ }
24
+
25
+ public String getValue () {
26
+ return value ;
27
+ }
28
+ }
29
+
15
30
static int DEFAULT_PAGE_SIZE = 500 ;
16
31
/**
17
32
* <p>Queries all elements matching a query by using a limit based pagination with a combination of
@@ -26,6 +41,8 @@ public class QueryUtils {
26
41
* <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
27
42
* parallel.
28
43
*
44
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
45
+ *
29
46
* @param query query containing predicates and expansion paths
30
47
* @param pageMapper callback function that is called on every page queried
31
48
* @param <TElement> type of one query result element
@@ -41,6 +58,40 @@ public class QueryUtils {
41
58
return queryAll (query , pageMapper , DEFAULT_PAGE_SIZE );
42
59
}
43
60
61
+ /**
62
+ * <p>Queries all elements matching a query by using a limit based pagination with a combination of
63
+ * id sorting and a page size 500. More on the algorithm can be found here:
64
+ * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p>
65
+ *
66
+ * <p>The method takes a callback {@link java.util.function.Function} that returns a result of type {@code <S>} that
67
+ * is returned on every page of elements queried. Eventually, the method returns a {@link
68
+ * java.util.concurrent.CompletionStage} that contains a list of all the results of the callbacks returned from every
69
+ * page.
70
+ *
71
+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
72
+ * parallel.
73
+ *
74
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
75
+ *
76
+ * @param query query containing predicates and expansion paths
77
+ * @param pageMapper callback function that is called on every page queried
78
+ * @param sortField field the results should sort on
79
+ * @param sortOrder order of the results
80
+ * @param elementIdentifierFn function to retrieve the sort order field from an element
81
+ * @param <TElement> type of one query result element
82
+ * @param <TMethod> type of the query
83
+ * @param <TResult> type of the result
84
+ * @param <S> type of the returned result of the callback function on every page.
85
+ * @return a completion stage containing a list of mapped pages as a result.
86
+ */
87
+ @ Nonnull
88
+ public static <TMethod extends SimplePagedQueryResourceRequest <TMethod , TResult , ?>, TResult extends ResourcePagedQueryResponse <TElement >, TElement extends DomainResource <TElement >, S > CompletionStage <List <S >> queryAll (
89
+ @ Nonnull final SimplePagedQueryResourceRequest <TMethod , TResult , ?> query ,
90
+ @ Nonnull final Function <List <TElement >, S > pageMapper , final String sortField , final SortOrder sortOrder ,
91
+ final Function <TElement , String > elementIdentifierFn ) {
92
+ return queryAll (query , pageMapper , DEFAULT_PAGE_SIZE , sortField , sortOrder , elementIdentifierFn );
93
+ }
94
+
44
95
/**
45
96
* <p>Queries all elements matching a query by using a limit based pagination with a combination of
46
97
* id sorting and a page size 500. More on the algorithm can be found here:
@@ -52,6 +103,8 @@ public class QueryUtils {
52
103
* <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
53
104
* parallel.
54
105
*
106
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
107
+ *
55
108
* @param query query containing predicates and expansion paths
56
109
* @param pageConsumer consumer applied on every page queried
57
110
* @param <TElement> type of one query result element
@@ -67,6 +120,38 @@ public class QueryUtils {
67
120
return queryAll (query , pageConsumer , DEFAULT_PAGE_SIZE );
68
121
}
69
122
123
+ /**
124
+ * <p>Queries all elements matching a query by using a limit based pagination with a combination of
125
+ * id sorting and a page size 500. More on the algorithm can be found here:
126
+ * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p>
127
+ *
128
+ * <p>The method takes a consumer {@link Consumer} that is applied on every page of elements
129
+ * queried.
130
+ *
131
+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
132
+ * parallel.
133
+ *
134
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
135
+ *
136
+ * @param query query containing predicates and expansion paths
137
+ * @param pageConsumer consumer applied on every page queried
138
+ * @param sortField field the results should sort on
139
+ * @param sortOrder order of the results
140
+ * @param elementIdentifierFn function to retrieve the sort order field from an element
141
+ * @param <TElement> type of one query result element
142
+ * @param <TMethod> type of the query
143
+ * @param <TResult> type of the result
144
+ * @return a completion stage containing void as a result after the consumer was applied on all
145
+ * pages.
146
+ */
147
+ @ Nonnull
148
+ public static <TMethod extends SimplePagedQueryResourceRequest <TMethod , TResult , ?>, TResult extends ResourcePagedQueryResponse <TElement >, TElement extends DomainResource <TElement >> CompletionStage <Void > queryAll (
149
+ @ Nonnull final SimplePagedQueryResourceRequest <TMethod , TResult , ?> query ,
150
+ @ Nonnull final Consumer <List <TElement >> pageConsumer , final String sortField , final SortOrder sortOrder ,
151
+ final Function <TElement , String > elementIdentifierFn ) {
152
+ return queryAll (query , pageConsumer , DEFAULT_PAGE_SIZE , sortField , sortOrder , elementIdentifierFn );
153
+ }
154
+
70
155
/**
71
156
* <p>Queries all elements matching a query by using a limit based pagination with a combination of
72
157
* id sorting and the supplied {@code pageSize}. More on the algorithm can be found here:
@@ -80,6 +165,8 @@ public class QueryUtils {
80
165
* <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
81
166
* parallel.
82
167
*
168
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
169
+ *
83
170
* @param query query containing predicates and expansion paths
84
171
* @param pageMapper callback function that is called on every page queried
85
172
* @param <TElement> type of one query result element
@@ -97,6 +184,43 @@ public class QueryUtils {
97
184
return queryAll .run (pageMapper );
98
185
}
99
186
187
+ /**
188
+ * <p>Queries all elements matching a query by using a limit based pagination with a combination of
189
+ * id sorting and the supplied {@code pageSize}. More on the algorithm can be found here:
190
+ * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p>
191
+ *
192
+ * <p>The method takes a callback {@link Function} that returns a result of type {@code <S>} that
193
+ * is returned on every page of elements queried. Eventually, the method returns a {@link
194
+ * CompletionStage} that contains a list of all the results of the callbacks returned from every
195
+ * page.
196
+ *
197
+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
198
+ * parallel.
199
+ *
200
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
201
+ *
202
+ * @param query query containing predicates and expansion paths
203
+ * @param pageMapper callback function that is called on every page queried
204
+ * @param sortField field the results should sort on
205
+ * @param sortOrder order of the results
206
+ * @param elementIdentifierFn function to retrieve the sort order field from an element
207
+ * @param <TElement> type of one query result element
208
+ * @param <TMethod> type of the query
209
+ * @param <TResult> type of the result
210
+ * @param <S> type of the returned result of the callback function on every page.
211
+ * @param pageSize the page size.
212
+ * @return a completion stage containing a list of mapped pages as a result.
213
+ */
214
+ @ Nonnull
215
+ public static <TMethod extends SimplePagedQueryResourceRequest <TMethod , TResult , ?>, TResult extends ResourcePagedQueryResponse <TElement >, TElement extends DomainResource <TElement >, S > CompletionStage <List <S >> queryAll (
216
+ @ Nonnull final SimplePagedQueryResourceRequest <TMethod , TResult , ?> query ,
217
+ @ Nonnull final Function <List <TElement >, S > pageMapper , final int pageSize , final String sortField ,
218
+ final SortOrder sortOrder , final Function <TElement , String > elementIdentifierFn ) {
219
+ final QueryAll <TMethod , TResult , TElement , S > queryAll = QueryAll .of (query , pageSize , sortField , sortOrder ,
220
+ elementIdentifierFn );
221
+ return queryAll .run (pageMapper );
222
+ }
223
+
100
224
/**
101
225
* <p>Queries all elements matching a query by using a limit based pagination with a combination of
102
226
* id sorting and the supplied {@code pageSize}. More on the algorithm can be found here:
@@ -123,4 +247,38 @@ public class QueryUtils {
123
247
final QueryAll <TMethod , TResult , TElement , Void > queryAll = QueryAll .of (query , pageSize );
124
248
return queryAll .run (pageConsumer );
125
249
}
250
+
251
+ /**
252
+ * <p>Queries all elements matching a query by using a limit based pagination with a combination of
253
+ * id sorting and the supplied {@code pageSize}. More on the algorithm can be found here:
254
+ * <a href="https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements">Iterating all elements</a>.</p>
255
+ *
256
+ * <p>The method takes a {@link java.util.function.Consumer} that is applied on every page of the queried elements.
257
+ *
258
+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in
259
+ * parallel.
260
+ *
261
+ * <p>NOTE: Please be aware that using a non-unique field for sorting may return incomplete results</p>
262
+ *
263
+ * @param query query containing predicates and expansion paths
264
+ * @param pageConsumer consumer applied on every page queried
265
+ * @param sortField field the results should sort on
266
+ * @param sortOrder order of the results
267
+ * @param elementIdentifierFn function to retrieve the sort order field from an element
268
+ * @param <TElement> type of one query result element
269
+ * @param <TMethod> type of the query
270
+ * @param <TResult> type of the result
271
+ * @param pageSize the page size
272
+ * @return a completion stage containing void as a result after the consumer was applied on all
273
+ * pages.
274
+ */
275
+ @ Nonnull
276
+ public static <TMethod extends SimplePagedQueryResourceRequest <TMethod , TResult , ?>, TResult extends ResourcePagedQueryResponse <TElement >, TElement extends DomainResource <TElement >> CompletionStage <Void > queryAll (
277
+ @ Nonnull final SimplePagedQueryResourceRequest <TMethod , TResult , ?> query ,
278
+ @ Nonnull final Consumer <List <TElement >> pageConsumer , final int pageSize , final String sortField ,
279
+ final SortOrder sortOrder , final Function <TElement , String > elementIdentifierFn ) {
280
+ final QueryAll <TMethod , TResult , TElement , Void > queryAll = QueryAll .of (query , pageSize , sortField , sortOrder ,
281
+ elementIdentifierFn );
282
+ return queryAll .run (pageConsumer );
283
+ }
126
284
}
0 commit comments