Skip to content

Commit b2f91ae

Browse files
committed
Added builder method for new Query option on the MongoItemReader
1 parent 6e0df99 commit b2f91ae

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import org.springframework.batch.item.data.MongoItemReader;
2323
import org.springframework.data.domain.Sort;
2424
import org.springframework.data.mongodb.core.MongoOperations;
25+
import org.springframework.data.mongodb.core.query.Query;
2526
import org.springframework.util.Assert;
27+
import org.springframework.util.StringUtils;
2628

2729
/**
2830
* A builder implementation for the {@link MongoItemReader}
@@ -34,7 +36,7 @@
3436
public class MongoItemReaderBuilder<T> {
3537
private MongoOperations template;
3638

37-
private String query;
39+
private String jsonQuery;
3840

3941
private Class<? extends T> targetType;
4042

@@ -58,6 +60,8 @@ public class MongoItemReaderBuilder<T> {
5860

5961
private int currentItemCount;
6062

63+
private Query query;
64+
6165
/**
6266
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
6367
* should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
@@ -129,16 +133,16 @@ public MongoItemReaderBuilder<T> template(MongoOperations template) {
129133
}
130134

131135
/**
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
133137
* via ?&lt;index&gt; placeholders where the &lt;index&gt; indicates the index of the
134138
* parameterValue to substitute.
135139
*
136-
* @param query JSON formatted Mongo query
140+
* @param query JSON formatted Mongo jsonQuery
137141
* @return The current instance of the builder
138142
* @see MongoItemReader#setQuery(String)
139143
*/
140-
public MongoItemReaderBuilder<T> query(String query) {
141-
this.query = query;
144+
public MongoItemReaderBuilder<T> jsonQuery(String query) {
145+
this.jsonQuery = query;
142146

143147
return this;
144148
}
@@ -237,6 +241,20 @@ public MongoItemReaderBuilder<T> pageSize(int pageSize) {
237241
return this;
238242
}
239243

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+
240258
/**
241259
* Validates and builds a {@link MongoItemReader}.
242260
*
@@ -248,18 +266,25 @@ public MongoItemReader<T> build() {
248266
Assert.hasText(this.name, "A name is required when saveState is set to true");
249267
}
250268
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+
}
253277

254278
MongoItemReader<T> reader = new MongoItemReader<>();
255279
reader.setTemplate(this.template);
256280
reader.setTargetType(this.targetType);
257-
reader.setQuery(this.query);
281+
reader.setQuery(this.jsonQuery);
258282
reader.setSort(this.sorts);
259283
reader.setHint(this.hint);
260284
reader.setFields(this.fields);
261285
reader.setCollection(this.collection);
262286
reader.setParameterValues(this.parameterValues);
287+
reader.setQuery(this.query);
263288

264289
reader.setPageSize(this.pageSize);
265290
reader.setName(this.name);

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilderTests.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testHint() throws Exception {
106106
public void testCollection() throws Exception {
107107
MongoItemReader<String> reader = getBasicBuilder()
108108
.parameterValues(Collections.singletonList("foo"))
109-
.query("{ name : ?0 }")
109+
.jsonQuery("{ name : ?0 }")
110110
.collection("collection")
111111
.build();
112112

@@ -126,7 +126,7 @@ public void testCollection() throws Exception {
126126
@Test
127127
public void testNullTemplate() {
128128
validateExceptionMessage(new MongoItemReaderBuilder<String>().targetType(String.class)
129-
.query("{ }")
129+
.jsonQuery("{ }")
130130
.sorts(this.sortOptions)
131131
.name("mongoReaderTest")
132132
.pageSize(50), "template is required.");
@@ -135,7 +135,7 @@ public void testNullTemplate() {
135135
@Test
136136
public void testNullTargetType() {
137137
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
138-
.query("{ }")
138+
.jsonQuery("{ }")
139139
.sorts(this.sortOptions)
140140
.name("mongoReaderTest")
141141
.pageSize(50), "targetType is required.");
@@ -147,14 +147,14 @@ public void testNullQuery() {
147147
.targetType(String.class)
148148
.sorts(this.sortOptions)
149149
.name("mongoReaderTest")
150-
.pageSize(50), "query is required.");
150+
.pageSize(50), "A query is required");
151151
}
152152

153153
@Test
154154
public void testNullSorts() {
155155
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
156156
.targetType(String.class)
157-
.query("{ }")
157+
.jsonQuery("{ }")
158158
.name("mongoReaderTest")
159159
.pageSize(50), "sorts map is required.");
160160
}
@@ -163,26 +163,30 @@ public void testNullSorts() {
163163
public void testNullName() {
164164
validateExceptionMessage(new MongoItemReaderBuilder<String>().template(this.template)
165165
.targetType(String.class)
166-
.query("{ }")
166+
.jsonQuery("{ }")
167167
.sorts(this.sortOptions)
168168
.pageSize(50), "A name is required when saveState is set to true");
169169
}
170170

171171
private void validateExceptionMessage(MongoItemReaderBuilder<String> builder, String message) {
172172
try {
173173
builder.build();
174-
fail("IllegalArgumentException should have been thrown");
174+
fail("Exception should have been thrown");
175175
}
176176
catch (IllegalArgumentException iae) {
177177
assertEquals("IllegalArgumentException message did not match the expected result.", message,
178178
iae.getMessage());
179179
}
180+
catch (IllegalStateException ise) {
181+
assertEquals("IllegalStateException message did not match the expected result.", message,
182+
ise.getMessage());
183+
}
180184
}
181185

182186
private MongoItemReaderBuilder<String> getBasicBuilder() {
183187
return new MongoItemReaderBuilder<String>().template(this.template)
184188
.targetType(String.class)
185-
.query("{ }")
189+
.jsonQuery("{ }")
186190
.sorts(this.sortOptions)
187191
.name("mongoReaderTest")
188192
.pageSize(50);

0 commit comments

Comments
 (0)