Remove deprecated usage of com.mongodb.util.JSON in MongoItemReader

This commit is contained in:
Mahmoud Ben Hassine
2020-01-07 14:51:37 +01:00
parent 20061007c0
commit e0f78d1504
2 changed files with 16 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,13 +20,12 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mongodb.util.JSON;
import org.bson.Document;
import org.bson.codecs.DecoderContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.InitializingBean;
@@ -36,6 +35,8 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
import org.springframework.data.mongodb.util.json.ParameterBindingJsonReader;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -77,12 +78,12 @@ import org.springframework.util.StringUtils;
*
* @author Michael Minella
* @author Takaaki Iida
* @author Mahmoud Ben Hassine
*/
public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class);
private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
private MongoOperations template;
private Query query;
private String queryString;
@@ -91,7 +92,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
private String hint;
private String fields;
private String collection;
private List<Object> parameterValues;
private List<Object> parameterValues = new ArrayList<>();
public MongoItemReader() {
super();
@@ -145,6 +146,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
* @param parameterValues values
*/
public void setParameterValues(List<Object> parameterValues) {
Assert.notNull(parameterValues, "Parameter values must not be null");
this.parameterValues = parameterValues;
}
@@ -245,23 +247,11 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
}
}
// Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
private String replacePlaceholders(String input, List<Object> values) {
Matcher matcher = PLACEHOLDER.matcher(input);
String result = input;
while (matcher.find()) {
String group = matcher.group();
int index = Integer.parseInt(matcher.group(1));
result = result.replace(group, getParameterWithIndex(values, index));
}
return result;
}
// Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
private String getParameterWithIndex(List<Object> values, int index) {
return JSON.serialize(values.get(index));
ParameterBindingJsonReader reader = new ParameterBindingJsonReader(input, values.toArray());
DecoderContext decoderContext = DecoderContext.builder().build();
Document document = new ParameterBindingDocumentCodec().decode(reader, decoderContext);
return document.toJson();
}
private Sort convertToSort(Map<String, Sort.Direction> sorts) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 the original author or authors.
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.batch.item.data.builder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -51,7 +52,7 @@ public class MongoItemReaderBuilder<T> {
private String collection;
private List<Object> parameterValues;
private List<Object> parameterValues = new ArrayList<>();
protected int pageSize = 10;