Skip to content

Commit c723eee

Browse files
Add support for additional converter (darrachequesne#21)
1 parent 6c03d0a commit c723eee

File tree

6 files changed

+145
-10
lines changed

6 files changed

+145
-10
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.Serializable;
44

5+
import org.springframework.core.convert.converter.Converter;
56
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
67
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
78
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
@@ -51,4 +52,27 @@ public interface QDataTablesRepository<T, ID extends Serializable>
5152
DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate,
5253
Predicate preFilteringPredicate);
5354

55+
/**
56+
* Returns the filtered list for the given {@link DataTablesInput}.
57+
*
58+
* @param input the {@link DataTablesInput} mapped from the Ajax request
59+
* @param converter the {@link Converter} to apply to the results of the query
60+
* @return a {@link DataTablesOutput}
61+
*/
62+
<R> DataTablesOutput<R> findAll(DataTablesInput input, Converter<T, R> converter);
63+
64+
/**
65+
* Returns the filtered list for the given {@link DataTablesInput}.
66+
*
67+
* @param input the {@link DataTablesInput} mapped from the Ajax request
68+
* @param additionalPredicate an additional {@link Predicate} to apply to the query (with an "AND"
69+
* clause)
70+
* @param preFilteringPredicate a pre-filtering {@link Predicate} to apply to the query (with an
71+
* "AND" clause)
72+
* @param converter the {@link Converter} to apply to the results of the query
73+
* @return a {@link DataTablesOutput}
74+
*/
75+
<R> DataTablesOutput<R> findAll(DataTablesInput input, Predicate additionalPredicate,
76+
Predicate preFilteringPredicate, Converter<T, R> converter);
77+
5478
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.getPredicate;
55

66
import java.io.Serializable;
7+
import java.util.List;
78

89
import javax.persistence.EntityManager;
910

11+
import org.springframework.core.convert.converter.Converter;
1012
import org.springframework.data.domain.Page;
1113
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
1214
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
@@ -48,18 +50,29 @@ public QDataTablesRepositoryImpl(JpaEntityInformation<T, ID> entityInformation,
4850

4951
@Override
5052
public DataTablesOutput<T> findAll(DataTablesInput input) {
51-
return findAll(input, null, null);
53+
return findAll(input, null, null, null);
5254
}
5355

5456
@Override
5557
public DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate) {
56-
return findAll(input, additionalPredicate, null);
58+
return findAll(input, additionalPredicate, null, null);
5759
}
5860

5961
@Override
6062
public DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPredicate,
6163
Predicate preFilteringPredicate) {
62-
DataTablesOutput<T> output = new DataTablesOutput<T>();
64+
return findAll(input, additionalPredicate, preFilteringPredicate, null);
65+
}
66+
67+
@Override
68+
public <R> DataTablesOutput<R> findAll(DataTablesInput input, Converter<T, R> converter) {
69+
return findAll(input, null, null, converter);
70+
}
71+
72+
@Override
73+
public <R> DataTablesOutput<R> findAll(DataTablesInput input, Predicate additionalPredicate,
74+
Predicate preFilteringPredicate, Converter<T, R> converter) {
75+
DataTablesOutput<R> output = new DataTablesOutput<R>();
6376
output.setDraw(input.getDraw());
6477
try {
6578
long recordsTotal = preFilteringPredicate == null ? count() : count(preFilteringPredicate);
@@ -71,12 +84,14 @@ public DataTablesOutput<T> findAll(DataTablesInput input, Predicate additionalPr
7184
Page<T> data = findAll(new BooleanBuilder().and(getPredicate(this.builder, input))
7285
.and(additionalPredicate).and(preFilteringPredicate).getValue(), getPageable(input));
7386

74-
output.setData(data.getContent());
87+
@SuppressWarnings("unchecked")
88+
List<R> content =
89+
converter == null ? (List<R>) data.getContent() : data.map(converter).getContent();
90+
output.setData(content);
7591
output.setRecordsFiltered(data.getTotalElements());
7692

7793
} catch (Exception e) {
7894
output.setError(e.toString());
79-
output.setRecordsFiltered(0L);
8095
}
8196

8297
return output;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.Serializable;
44

5+
import org.springframework.core.convert.converter.Converter;
56
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
67
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
78
import org.springframework.data.jpa.domain.Specification;
@@ -50,4 +51,27 @@ public interface DataTablesRepository<T, ID extends Serializable>
5051
DataTablesOutput<T> findAll(DataTablesInput input, Specification<T> additionalSpecification,
5152
Specification<T> preFilteringSpecification);
5253

54+
/**
55+
* Returns the filtered list for the given {@link DataTablesInput}.
56+
*
57+
* @param input the {@link DataTablesInput} mapped from the Ajax request
58+
* @param converter the {@link Converter} to apply to the results of the query
59+
* @return a {@link DataTablesOutput}
60+
*/
61+
<R> DataTablesOutput<R> findAll(DataTablesInput input, Converter<T, R> converter);
62+
63+
/**
64+
* Returns the filtered list for the given {@link DataTablesInput}.
65+
*
66+
* @param input the {@link DataTablesInput} mapped from the Ajax request
67+
* @param additionalSpecification an additional {@link Specification} to apply to the query (with
68+
* an "AND" clause)
69+
* @param preFilteringSpecification a pre-filtering {@link Specification} to apply to the query
70+
* (with an "AND" clause)
71+
* @param converter the {@link Converter} to apply to the results of the query
72+
* @return a {@link DataTablesOutput}
73+
*/
74+
<R> DataTablesOutput<R> findAll(DataTablesInput input, Specification<T> additionalSpecification,
75+
Specification<T> preFilteringSpecification, Converter<T, R> converter);
76+
5377
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.getSpecification;
55

66
import java.io.Serializable;
7+
import java.util.List;
78

89
import javax.persistence.EntityManager;
910

11+
import org.springframework.core.convert.converter.Converter;
1012
import org.springframework.data.domain.Page;
1113
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
1214
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
@@ -31,19 +33,31 @@ public DataTablesRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
3133

3234
@Override
3335
public DataTablesOutput<T> findAll(DataTablesInput input) {
34-
return findAll(input, null, null);
36+
return findAll(input, null, null, null);
3537
}
3638

3739
@Override
3840
public DataTablesOutput<T> findAll(DataTablesInput input,
3941
Specification<T> additionalSpecification) {
40-
return findAll(input, additionalSpecification, null);
42+
return findAll(input, additionalSpecification, null, null);
4143
}
4244

4345
@Override
4446
public DataTablesOutput<T> findAll(DataTablesInput input,
4547
Specification<T> additionalSpecification, Specification<T> preFilteringSpecification) {
46-
DataTablesOutput<T> output = new DataTablesOutput<T>();
48+
return findAll(input, additionalSpecification, preFilteringSpecification, null);
49+
}
50+
51+
@Override
52+
public <R> DataTablesOutput<R> findAll(DataTablesInput input, Converter<T, R> converter) {
53+
return findAll(input, null, null, converter);
54+
}
55+
56+
@Override
57+
public <R> DataTablesOutput<R> findAll(DataTablesInput input,
58+
Specification<T> additionalSpecification, Specification<T> preFilteringSpecification,
59+
Converter<T, R> converter) {
60+
DataTablesOutput<R> output = new DataTablesOutput<R>();
4761
output.setDraw(input.getDraw());
4862

4963
try {
@@ -57,12 +71,14 @@ public DataTablesOutput<T> findAll(DataTablesInput input,
5771
Page<T> data = findAll(Specifications.where(getSpecification(getDomainClass(), input))
5872
.and(additionalSpecification).and(preFilteringSpecification), getPageable(input));
5973

60-
output.setData(data.getContent());
74+
@SuppressWarnings("unchecked")
75+
List<R> content =
76+
converter == null ? (List<R>) data.getContent() : data.map(converter).getContent();
77+
output.setData(content);
6178
output.setRecordsFiltered(data.getTotalElements());
6279

6380
} catch (Exception e) {
6481
output.setError(e.toString());
65-
output.setRecordsFiltered(0L);
6682
}
6783

6884
return output;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.springframework.data.jpa.datatables.model;
2+
3+
public class UserDto {
4+
5+
private final Integer id;
6+
private final String username;
7+
private final String role;
8+
private final String status;
9+
10+
public UserDto(Integer id, String username, String role, String status) {
11+
this.id = id;
12+
this.username = username;
13+
this.role = role;
14+
this.status = status;
15+
}
16+
17+
public Integer getId() {
18+
return id;
19+
}
20+
21+
public String getUsername() {
22+
return username;
23+
}
24+
25+
public String getRole() {
26+
return role;
27+
}
28+
29+
public String getStatus() {
30+
return status;
31+
}
32+
33+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import org.junit.Test;
1010
import org.junit.runner.RunWith;
1111
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.core.convert.converter.Converter;
1213
import org.springframework.data.jpa.datatables.Config;
1314
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
1415
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
1516
import org.springframework.data.jpa.datatables.model.User;
17+
import org.springframework.data.jpa.datatables.model.UserDto;
1618
import org.springframework.data.jpa.datatables.model.UserRepository;
1719
import org.springframework.data.jpa.datatables.specification.TestSpecification;
1820
import org.springframework.test.context.ContextConfiguration;
@@ -255,6 +257,27 @@ public void testWithNegativeLength() {
255257
assertEquals(24, output.getRecordsTotal());
256258
}
257259

260+
@Test
261+
public void testWithConverter() {
262+
DataTablesInput input = getBasicInput();
263+
Converter<User, UserDto> userConverter = new Converter<User, UserDto>() {
264+
@Override
265+
public UserDto convert(User user) {
266+
return new UserDto(user.getId(), user.getUsername(), user.getRole().toString(),
267+
user.getStatus().toString());
268+
}
269+
};
270+
271+
input.getColumn("id").setSearchValue("24");
272+
DataTablesOutput<UserDto> output = userRepository.findAll(input, userConverter);
273+
assertNotNull(output);
274+
UserDto user = output.getData().get(0);
275+
assertEquals(24, (int) user.getId());
276+
assertEquals("john23", user.getUsername());
277+
assertEquals("USER", user.getRole());
278+
assertEquals("BLOCKED", user.getStatus());
279+
}
280+
258281
/**
259282
*
260283
* @return basic input parameters

0 commit comments

Comments
 (0)