11/*
2- * Copyright 2012-2017 the original author or authors.
2+ * Copyright 2012-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2020import java .util .Iterator ;
2121import java .util .List ;
2222import java .util .Map ;
23- import java .util .regex .Matcher ;
24- import java .util .regex .Pattern ;
25-
26- import com .mongodb .util .JSON ;
2723
24+ import org .bson .Document ;
25+ import org .bson .codecs .DecoderContext ;
2826import org .slf4j .Logger ;
2927import org .slf4j .LoggerFactory ;
28+
3029import org .springframework .batch .item .ExecutionContext ;
3130import org .springframework .batch .item .ItemReader ;
3231import org .springframework .beans .factory .InitializingBean ;
3635import org .springframework .data .mongodb .core .MongoOperations ;
3736import org .springframework .data .mongodb .core .query .BasicQuery ;
3837import org .springframework .data .mongodb .core .query .Query ;
38+ import org .springframework .data .mongodb .util .json .ParameterBindingDocumentCodec ;
39+ import org .springframework .data .mongodb .util .json .ParameterBindingJsonReader ;
3940import org .springframework .util .Assert ;
4041import org .springframework .util .ClassUtils ;
4142import org .springframework .util .StringUtils ;
7778 *
7879 * @author Michael Minella
7980 * @author Takaaki Iida
81+ * @author Mahmoud Ben Hassine
8082 */
8183public class MongoItemReader <T > extends AbstractPaginatedDataItemReader <T > implements InitializingBean {
8284
8385 private static final Logger log = LoggerFactory .getLogger (MongoItemReader .class );
8486
85- private static final Pattern PLACEHOLDER = Pattern .compile ("\\ ?(\\ d+)" );
8687 private MongoOperations template ;
8788 private Query query ;
8889 private String queryString ;
@@ -91,7 +92,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
9192 private String hint ;
9293 private String fields ;
9394 private String collection ;
94- private List <Object > parameterValues ;
95+ private List <Object > parameterValues = new ArrayList <>() ;
9596
9697 public MongoItemReader () {
9798 super ();
@@ -145,6 +146,7 @@ public void setTargetType(Class<? extends T> type) {
145146 * @param parameterValues values
146147 */
147148 public void setParameterValues (List <Object > parameterValues ) {
149+ Assert .notNull (parameterValues , "Parameter values must not be null" );
148150 this .parameterValues = parameterValues ;
149151 }
150152
@@ -245,23 +247,11 @@ public void afterPropertiesSet() throws Exception {
245247 }
246248 }
247249
248- // Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
249250 private String replacePlaceholders (String input , List <Object > values ) {
250- Matcher matcher = PLACEHOLDER .matcher (input );
251- String result = input ;
252-
253- while (matcher .find ()) {
254- String group = matcher .group ();
255- int index = Integer .parseInt (matcher .group (1 ));
256- result = result .replace (group , getParameterWithIndex (values , index ));
257- }
258-
259- return result ;
260- }
261-
262- // Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
263- private String getParameterWithIndex (List <Object > values , int index ) {
264- return JSON .serialize (values .get (index ));
251+ ParameterBindingJsonReader reader = new ParameterBindingJsonReader (input , values .toArray ());
252+ DecoderContext decoderContext = DecoderContext .builder ().build ();
253+ Document document = new ParameterBindingDocumentCodec ().decode (reader , decoderContext );
254+ return document .toJson ();
265255 }
266256
267257 private Sort convertToSort (Map <String , Sort .Direction > sorts ) {
0 commit comments