22
22
import org .springframework .batch .item .data .MongoItemReader ;
23
23
import org .springframework .data .domain .Sort ;
24
24
import org .springframework .data .mongodb .core .MongoOperations ;
25
+ import org .springframework .data .mongodb .core .query .Query ;
25
26
import org .springframework .util .Assert ;
27
+ import org .springframework .util .StringUtils ;
26
28
27
29
/**
28
30
* A builder implementation for the {@link MongoItemReader}
34
36
public class MongoItemReaderBuilder <T > {
35
37
private MongoOperations template ;
36
38
37
- private String query ;
39
+ private String jsonQuery ;
38
40
39
41
private Class <? extends T > targetType ;
40
42
@@ -58,6 +60,8 @@ public class MongoItemReaderBuilder<T> {
58
60
59
61
private int currentItemCount ;
60
62
63
+ private Query query ;
64
+
61
65
/**
62
66
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
63
67
* should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
@@ -129,16 +133,16 @@ public MongoItemReaderBuilder<T> template(MongoOperations template) {
129
133
}
130
134
131
135
/**
132
- * A JSON formatted MongoDB query . Parameterization of the provided query is allowed
136
+ * A JSON formatted MongoDB jsonQuery . Parameterization of the provided jsonQuery is allowed
133
137
* via ?<index> placeholders where the <index> indicates the index of the
134
138
* parameterValue to substitute.
135
139
*
136
- * @param query JSON formatted Mongo query
140
+ * @param query JSON formatted Mongo jsonQuery
137
141
* @return The current instance of the builder
138
142
* @see MongoItemReader#setQuery(String)
139
143
*/
140
- public MongoItemReaderBuilder <T > query (String query ) {
141
- this .query = query ;
144
+ public MongoItemReaderBuilder <T > jsonQuery (String query ) {
145
+ this .jsonQuery = query ;
142
146
143
147
return this ;
144
148
}
@@ -237,6 +241,20 @@ public MongoItemReaderBuilder<T> pageSize(int pageSize) {
237
241
return this ;
238
242
}
239
243
244
+ /**
245
+ * Provide a Spring Data Mongo {@link Query}. This will take precidence over a JSON
246
+ * configured query.
247
+ *
248
+ * @param query Query to execute
249
+ * @return this instance for method chaining
250
+ * @see MongoItemReader#setQuery(Query)
251
+ */
252
+ public MongoItemReaderBuilder <T > query (Query query ) {
253
+ this .query = query ;
254
+
255
+ return this ;
256
+ }
257
+
240
258
/**
241
259
* Validates and builds a {@link MongoItemReader}.
242
260
*
@@ -248,18 +266,25 @@ public MongoItemReader<T> build() {
248
266
Assert .hasText (this .name , "A name is required when saveState is set to true" );
249
267
}
250
268
Assert .notNull (this .targetType , "targetType is required." );
251
- Assert .notNull (this .query , "query is required." );
252
- Assert .notNull (this .sorts , "sorts map is required." );
269
+ Assert .state (StringUtils .hasText (this .jsonQuery ) || this .query != null , "A query is required" );
270
+
271
+ if (StringUtils .hasText (this .jsonQuery )) {
272
+ Assert .notNull (this .sorts , "sorts map is required." );
273
+ }
274
+ else {
275
+ Assert .state (this .query .getLimit () != 0 , "PageSize in Query object was ignored." );
276
+ }
253
277
254
278
MongoItemReader <T > reader = new MongoItemReader <>();
255
279
reader .setTemplate (this .template );
256
280
reader .setTargetType (this .targetType );
257
- reader .setQuery (this .query );
281
+ reader .setQuery (this .jsonQuery );
258
282
reader .setSort (this .sorts );
259
283
reader .setHint (this .hint );
260
284
reader .setFields (this .fields );
261
285
reader .setCollection (this .collection );
262
286
reader .setParameterValues (this .parameterValues );
287
+ reader .setQuery (this .query );
263
288
264
289
reader .setPageSize (this .pageSize );
265
290
reader .setName (this .name );
0 commit comments