Skip to content

Commit

Permalink
fix: properly compute search panes with related entities
Browse files Browse the repository at this point in the history
Related: #159
  • Loading branch information
darrachequesne committed Apr 3, 2024
1 parent be286ee commit 495cfbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.query.sqm.tree.domain.AbstractSqmFrom;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.from.SqmAttributeJoin;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.datatables.SpecificationBuilder;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
Expand Down Expand Up @@ -109,8 +107,10 @@ private SearchPanes computeSearchPanes(DataTablesInput input, Specification<T> s
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> query = criteriaBuilder.createQuery(Object[].class);
Root<T> root = query.from(getDomainClass());
query.multiselect(root.get(attribute), criteriaBuilder.count(root));
query.groupBy(root.get(attribute));
Path<?> path = getPath(root, attribute);

query.multiselect(path, criteriaBuilder.count(root));
query.groupBy(path);
query.where(specification.toPredicate(root, query, criteriaBuilder));

List<SearchPanes.Item> items = new ArrayList<>();
Expand All @@ -127,4 +127,13 @@ private SearchPanes computeSearchPanes(DataTablesInput input, Specification<T> s
return new SearchPanes(options);
}

private Path<?> getPath(Root<T> root, String attribute) {
String[] parts = attribute.split("\\.");
Path<?> path = root;
for (String part : parts) {
path = path.get(part);
}
return path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,16 @@ void withSearchPanes() {
Map<String, Set<String>> searchPanes = new HashMap<>();
searchPanes.put("position", new HashSet<>(asList("Software Engineer", "Integration Specialist")));
searchPanes.put("age", emptySet());
searchPanes.put("office.city", emptySet());

input.setSearchPanes(searchPanes);

DataTablesOutput<Employee> output = getOutput(input);
assertThat(output.getRecordsFiltered()).isEqualTo(3);
assertThat(output.getSearchPanes()).isNotNull();

assertThat(output.getSearchPanes().getOptions().size()).isEqualTo(3);

assertThat(output.getSearchPanes().getOptions().get("position")).containsOnly(
new SearchPanes.Item("Software Engineer", "Software Engineer", 2, 2),
new SearchPanes.Item("Integration Specialist", "Integration Specialist", 1, 1)
Expand All @@ -334,6 +337,11 @@ void withSearchPanes() {
new SearchPanes.Item("41", "41", 1, 1),
new SearchPanes.Item("61", "61", 1, 1)
);
assertThat(output.getSearchPanes().getOptions().get("office.city")).containsOnly(
new SearchPanes.Item("London", "London", 1, 1),
new SearchPanes.Item("New York", "New York", 1, 1),
new SearchPanes.Item("San Francisco", "San Francisco", 1, 1)
);
}

@Test
Expand Down

0 comments on commit 495cfbc

Please sign in to comment.