Skip to content

Commit 1c0e648

Browse files
Update the paging calculation, to correctly handle requests from the Scroller plugin (darrachequesne#24)
Fix darrachequesne#23
1 parent 73db1d6 commit 1c0e648

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public <R> DataTablesOutput<R> findAll(DataTablesInput input, Predicate addition
7373
Predicate preFilteringPredicate, Converter<T, R> converter) {
7474
DataTablesOutput<R> output = new DataTablesOutput<R>();
7575
output.setDraw(input.getDraw());
76+
if (input.getLength() == 0) {
77+
return output;
78+
}
79+
7680
try {
7781
long recordsTotal = preFilteringPredicate == null ? count() : count(preFilteringPredicate);
7882
if (recordsTotal == 0) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public <R> DataTablesOutput<R> findAll(DataTablesInput input,
5858
Converter<T, R> converter) {
5959
DataTablesOutput<R> output = new DataTablesOutput<R>();
6060
output.setDraw(input.getDraw());
61+
if (input.getLength() == 0) {
62+
return output;
63+
}
6164

6265
try {
6366
long recordsTotal =

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import org.springframework.data.domain.PageRequest;
76
import org.springframework.data.domain.Pageable;
87
import org.springframework.data.domain.Sort;
98
import org.springframework.data.domain.Sort.Direction;
@@ -42,7 +41,7 @@ public static Pageable getPageable(DataTablesInput input) {
4241
input.setStart(0);
4342
input.setLength(Integer.MAX_VALUE);
4443
}
45-
return new PageRequest(input.getStart() / input.getLength(), input.getLength(), sort);
44+
return new DataTablesPageRequest(input.getStart(), input.getLength(), sort);
4645
}
4746

4847
public static boolean isBoolean(String filterValue) {
@@ -55,4 +54,57 @@ public static String getLikeFilterValue(String filterValue) {
5554
+ "%";
5655
}
5756

57+
private static class DataTablesPageRequest implements Pageable {
58+
59+
private final int offset;
60+
private final int pageSize;
61+
private final Sort sort;
62+
63+
public DataTablesPageRequest(int offset, int pageSize, Sort sort) {
64+
this.offset = offset;
65+
this.pageSize = pageSize;
66+
this.sort = sort;
67+
}
68+
69+
@Override
70+
public int getOffset() {
71+
return offset;
72+
}
73+
74+
@Override
75+
public int getPageSize() {
76+
return pageSize;
77+
}
78+
79+
@Override
80+
public Sort getSort() {
81+
return sort;
82+
}
83+
84+
@Override
85+
public Pageable next() {
86+
throw new UnsupportedOperationException();
87+
}
88+
89+
@Override
90+
public Pageable previousOrFirst() {
91+
throw new UnsupportedOperationException();
92+
}
93+
94+
@Override
95+
public Pageable first() {
96+
throw new UnsupportedOperationException();
97+
}
98+
99+
@Override
100+
public boolean hasPrevious() {
101+
throw new UnsupportedOperationException();
102+
}
103+
104+
@Override
105+
public int getPageNumber() {
106+
throw new UnsupportedOperationException();
107+
}
108+
}
109+
58110
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ public void testWithZeroLength() {
241241
input.setLength(0);
242242
DataTablesOutput<User> output = userRepository.findAll(input);
243243
assertNotNull(output);
244-
assertNotNull(output.getError());
245-
assertEquals(output.getError(), "java.lang.ArithmeticException: / by zero");
244+
assertEquals(0, output.getData().size());
245+
assertEquals(0, output.getRecordsFiltered());
246246
}
247247

248248
@Test
@@ -278,6 +278,27 @@ public UserDto convert(User user) {
278278
assertEquals("BLOCKED", user.getStatus());
279279
}
280280

281+
@Test
282+
public void testWithFancyPaging() {
283+
DataTablesInput input = getBasicInput();
284+
285+
input.setLength(5);
286+
input.setStart(7);
287+
DataTablesOutput<User> output = userRepository.findAll(input);
288+
assertNotNull(output);
289+
assertNull(output.getError());
290+
assertEquals(5, output.getData().size());
291+
assertEquals(8, (int) output.getData().get(0).getId());
292+
assertEquals(8 + 5 - 1, (int) output.getData().get(4).getId());
293+
294+
input.setLength(7);
295+
input.setStart(22);
296+
output = userRepository.findAll(input);
297+
assertNotNull(output);
298+
assertEquals(2, output.getData().size());
299+
assertEquals(23, (int) output.getData().get(0).getId());
300+
}
301+
281302
/**
282303
*
283304
* @return basic input parameters

0 commit comments

Comments
 (0)