org.springframework.data.jpa.datatables.SpecificationBuilder Maven / Gradle / Ivy
package org.springframework.data.jpa.datatables;
import jakarta.persistence.metamodel.Bindable.BindableType;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.NonNull;
import jakarta.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
public class SpecificationBuilder extends AbstractPredicateBuilder> {
public SpecificationBuilder(DataTablesInput input) {
super(input);
}
@Override
public Specification build() {
return new DataTablesSpecification<>();
}
private class DataTablesSpecification implements Specification {
@Override
public Predicate toPredicate(@NonNull Root root, @NonNull CriteriaQuery> query, @NonNull CriteriaBuilder criteriaBuilder) {
Predicates predicates = new Predicates();
initPredicatesRecursively(predicates, query, tree, root, root, criteriaBuilder);
if (input.getSearchPanes() != null) {
input.getSearchPanes().forEach((attribute, values) -> {
if (!values.isEmpty()) {
predicates.columns.add(root.get(attribute).in(values));
}
});
}
return predicates.toPredicate(criteriaBuilder);
}
private boolean isCountQuery(CriteriaQuery> query) {
return query.getResultType() == Long.class;
}
protected void initPredicatesRecursively(Predicates predicates, CriteriaQuery> query, Node node, From from, FetchParent fetch, CriteriaBuilder criteriaBuilder) {
if (node.isLeaf()) {
boolean hasColumnFilter = node.getData() != null;
if (hasColumnFilter) {
Filter columnFilter = node.getData();
predicates.columns.add(columnFilter.createPredicate(from, criteriaBuilder, node.getName()));
} else if (hasGlobalFilter) {
Filter globalFilter = tree.getData();
predicates.global.add(globalFilter.createPredicate(from, criteriaBuilder, node.getName()));
}
}
for (Node child : node.getChildren()) {
Path
© 2015 - 2025 Weber Informatics LLC | Privacy Policy