Skip to content

Commit eae000f

Browse files
Add an optional pre-filtering specification
1 parent 5c8a4d6 commit eae000f

File tree

7 files changed

+86
-6
lines changed

7 files changed

+86
-6
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ It overrides jQuery data serialization to allow Spring MVC to correctly map inpu
9090
The repositories now expose the following methods:
9191
* `DataTablesOutput<T> findAll(DataTablesInput input);`
9292
* `DataTablesOutput<T> findAll(DataTablesInput input, Specification<T> additionalSpecification);`
93+
* `DataTablesOutput<T> findAll(DataTablesInput input, Specification<T> additionalSpecification, Specification<T> preFilteringSpecification);`
9394

9495
Note: since version 2.0, QueryDSL is also supported:
9596
* replace `DataTablesRepositoryFactoryBean` with `QDataTablesRepositoryFactoryBean`
@@ -99,6 +100,7 @@ and your repositories will now expose:
99100

100101
* `DataTablesOutput<T> findAll(DataTablesInput input);`
101102
* `DataTablesOutput<T> findAll(DataTablesInput input, com.mysema.querydsl.Predicate additionalPredicate);`
103+
* `DataTablesOutput<T> findAll(DataTablesInput input, com.mysema.querydsl.Predicate additionalPredicate, com.mysema.querydsl.Predicate preFilteringPredicate);`
102104

103105
Your controllers should be able to handle the parameters sent by DataTables:
104106

@@ -124,6 +126,13 @@ public class UserRestController {
124126
parameter0.getSearch().setValue("");
125127
return userRepository.findAll(input, additionalSpecification);
126128
}
129+
130+
// or with an additional filter allowing to 'hide' data from the client (the filter will be applied on both the count and the data queries, and may impact the recordsTotal in the output)
131+
@JsonView(DataTablesOutput.View.class)
132+
@RequestMapping(value = "/data/users", method = RequestMethod.GET)
133+
public DataTablesOutput<User> getUsers(@Valid DataTablesInput input) {
134+
return userRepository.findAll(input, null, removeHiddenEntitiesSpecification);
135+
}
127136
}
128137
```
129138

src/main/java/org/springframework/data/jpa/datatables/qrepository/QDataTablesRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,17 @@ public interface QDataTablesRepository<T, ID extends Serializable>
3838
*/
3939
DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate);
4040

41+
/**
42+
* Returns the filtered list for the given {@link DataTablesInput}.
43+
*
44+
* @param input the {@link DataTablesInput} mapped from the Ajax request
45+
* @param additionalPredicate an additional {@link Predicate} to apply to the query (with an "AND"
46+
* clause)
47+
* @param preFilteringPredicate a pre-filtering {@link Predicate} to apply to the query (with an
48+
* "AND" clause)
49+
* @return a {@link DataTablesOutput}
50+
*/
51+
DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate,
52+
Predicate preFilteringPredicate);
53+
4154
}

src/main/java/org/springframework/data/jpa/datatables/qrepository/QDataTablesRepositoryImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,25 @@ public QDataTablesRepositoryImpl(JpaEntityInformation<T, ID> entityInformation,
4848

4949
@Override
5050
public DataTablesOutput<T> findAll(DataTablesInput input) {
51-
return findAll(input, null);
51+
return findAll(input, null, null);
5252
}
5353

5454
@Override
5555
public DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate) {
56+
return findAll(input, additionalPredicate, null);
57+
}
58+
59+
@Override
60+
public DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate,
61+
Predicate preFilteringPredicate) {
5662
DataTablesOutput<T> output = new DataTablesOutput<T>();
5763
output.setDraw(input.getDraw());
5864
try {
59-
output.setRecordsTotal(count());
65+
output
66+
.setRecordsTotal(preFilteringPredicate == null ? count() : count(preFilteringPredicate));
6067

6168
Page<T> data = findAll(new BooleanBuilder().and(getPredicate(this.builder, input))
62-
.and(additionalPredicate).getValue(), getPageable(input));
69+
.and(additionalPredicate).and(preFilteringPredicate).getValue(), getPageable(input));
6370

6471
output.setData(data.getContent());
6572
output.setRecordsFiltered(data.getTotalElements());

src/main/java/org/springframework/data/jpa/datatables/repository/DataTablesRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ public interface DataTablesRepository<T, ID extends Serializable>
3737
*/
3838
DataTablesOutput<T> findAll(DataTablesInput input, Specification<T> additionalSpecification);
3939

40+
/**
41+
* Returns the filtered list for the given {@link DataTablesInput}.
42+
*
43+
* @param input the {@link DataTablesInput} mapped from the Ajax request
44+
* @param additionalSpecification an additional {@link Specification} to apply to the query (with
45+
* an "AND" clause)
46+
* @param preFilteringSpecification a pre-filtering {@link Specification} to apply to the query
47+
* (with an "AND" clause)
48+
* @return a {@link DataTablesOutput}
49+
*/
50+
DataTablesOutput<T> findAll(DataTablesInput input, Specification<T> additionalSpecification,
51+
Specification<T> preFilteringSpecification);
52+
4053
}

src/main/java/org/springframework/data/jpa/datatables/repository/DataTablesRepositoryImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,27 @@ public DataTablesRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
3131

3232
@Override
3333
public DataTablesOutput<T> findAll(DataTablesInput input) {
34-
return findAll(input, null);
34+
return findAll(input, null, null);
3535
}
3636

3737
@Override
3838
public DataTablesOutput<T> findAll(DataTablesInput input,
3939
Specification<T> additionalSpecification) {
40+
return findAll(input, additionalSpecification, null);
41+
}
42+
43+
@Override
44+
public DataTablesOutput<T> findAll(DataTablesInput input,
45+
Specification<T> additionalSpecification, Specification<T> preFilteringSpecification) {
4046
DataTablesOutput<T> output = new DataTablesOutput<T>();
4147
output.setDraw(input.getDraw());
4248

4349
try {
44-
output.setRecordsTotal(count());
50+
output.setRecordsTotal(
51+
preFilteringSpecification == null ? count() : count(preFilteringSpecification));
4552

4653
Page<T> data = findAll(Specifications.where(getSpecification(getDomainClass(), input))
47-
.and(additionalSpecification), getPageable(input));
54+
.and(additionalSpecification).and(preFilteringSpecification), getPageable(input));
4855

4956
output.setData(data.getContent());
5057
output.setRecordsFiltered(data.getTotalElements());
@@ -56,4 +63,5 @@ public DataTablesOutput<T> findAll(DataTablesInput input,
5663

5764
return output;
5865
}
66+
5967
}

src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
1818
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
1919
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
20+
import org.springframework.data.jpa.datatables.specification.PreFilteringSpecification;
2021
import org.springframework.test.context.ContextConfiguration;
2122
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2223

@@ -97,6 +98,17 @@ public void testEscapeCharacter() {
9798
assertEquals(1, (long) output.getRecordsFiltered());
9899
}
99100

101+
@Test
102+
public void testWithPreFiltering() {
103+
DataTablesInput input = getBasicInput();
104+
105+
DataTablesOutput<Bill> output =
106+
billRepository.findAll(input, null, new PreFilteringSpecification<Bill>());
107+
assertNotNull(output);
108+
assertEquals(6, (long) output.getRecordsFiltered());
109+
assertEquals(6, (long) output.getRecordsTotal());
110+
}
111+
100112
/**
101113
*
102114
* @return basic input parameters
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.data.jpa.datatables.specification;
2+
3+
import javax.persistence.criteria.CriteriaBuilder;
4+
import javax.persistence.criteria.CriteriaQuery;
5+
import javax.persistence.criteria.Predicate;
6+
import javax.persistence.criteria.Root;
7+
8+
import org.springframework.data.jpa.domain.Specification;
9+
10+
public class PreFilteringSpecification<T> implements Specification<T> {
11+
12+
@Override
13+
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
14+
CriteriaBuilder criteriaBuilder) {
15+
return criteriaBuilder.le(root.get("id").as(Integer.class), 6);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)