org.springframework.data.mongodb.datatables.DataTablesRepositoryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-mongodb-datatables Show documentation
Show all versions of spring-data-mongodb-datatables Show documentation
Spring Data MongoDB extension to work with the great jQuery plug-in DataTables (http://datatables.net/)
package org.springframework.data.mongodb.datatables;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import static java.util.stream.Collectors.toList;
import static org.springframework.data.mongodb.core.query.Query.query;
final class DataTablesRepositoryImpl extends SimpleMongoRepository
implements DataTablesRepository {
private final MongoEntityInformation metadata;
private final MongoOperations mongoOperations;
/**
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
*
* @param metadata must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
*/
public DataTablesRepositoryImpl(MongoEntityInformation metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
this.metadata = metadata;
this.mongoOperations = mongoOperations;
}
@Override
public DataTablesOutput findAll(DataTablesInput input) {
return findAll(input, null, null, null);
}
@Override
public DataTablesOutput findAll(DataTablesInput input, Criteria additionalCriteria) {
return findAll(input, additionalCriteria, null, null);
}
@Override
public DataTablesOutput findAll(DataTablesInput input, Criteria additionalCriteria, Criteria preFilteringCriteria) {
return findAll(input, additionalCriteria, preFilteringCriteria, null);
}
@Override
public DataTablesOutput findAll(DataTablesInput input, Function converter) {
return findAll(input, null, null, converter);
}
@Override
public DataTablesOutput findAll(DataTablesInput input, Criteria additionalCriteria, Criteria preFilteringCriteria, Function converter) {
DataTablesOutput output = new DataTablesOutput<>();
output.setDraw(input.getDraw());
if (input.getLength() == 0) {
return output;
}
try {
long recordsTotal = count(preFilteringCriteria);
output.setRecordsTotal(recordsTotal);
if (recordsTotal == 0) {
return output;
}
Query query = new DataTablesCriteria(input, additionalCriteria, preFilteringCriteria).toQuery();
long recordsFiltered = mongoOperations.count(query, metadata.getCollectionName());
output.setRecordsFiltered(recordsFiltered);
if (recordsFiltered == 0) {
return output;
}
List data = mongoOperations.find(query, metadata.getJavaType(), metadata.getCollectionName());
output.setData(converter == null ? (List) data : data.stream().map(converter).collect(toList()));
} catch (Exception e) {
output.setError(e.toString());
}
return output;
}
private long count(Criteria preFilteringCriteria) {
if (preFilteringCriteria == null) {
return count();
} else {
return mongoOperations.count(query(preFilteringCriteria), metadata.getCollectionName());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy