fix(search): use in-memory sort for SENDER to include documents with null sender
INNER JOIN from Sort.by("sender.lastName") was excluding docs without a sender.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -291,11 +291,15 @@ public class DocumentService {
|
||||
.and(hasTagPartial(tagQ))
|
||||
.and(hasStatus(status));
|
||||
|
||||
Sort springSort = resolveSort(sort, dir);
|
||||
if (sort == DocumentSort.RECEIVER) {
|
||||
List<Document> results = documentRepository.findAll(spec);
|
||||
return sortByFirstReceiver(results, dir);
|
||||
}
|
||||
if (sort == DocumentSort.SENDER) {
|
||||
List<Document> results = documentRepository.findAll(spec);
|
||||
return sortBySender(results, dir);
|
||||
}
|
||||
Sort springSort = resolveSort(sort, dir);
|
||||
return documentRepository.findAll(spec, springSort);
|
||||
}
|
||||
|
||||
@@ -312,6 +316,22 @@ public class DocumentService {
|
||||
};
|
||||
}
|
||||
|
||||
private List<Document> sortBySender(List<Document> documents, String dir) {
|
||||
boolean ascending = "ASC".equalsIgnoreCase(dir);
|
||||
Comparator<String> nullSafeComparator = (a, b) -> {
|
||||
if (a.isEmpty() && b.isEmpty()) return 0;
|
||||
if (a.isEmpty()) return ascending ? 1 : -1;
|
||||
if (b.isEmpty()) return ascending ? -1 : 1;
|
||||
return ascending ? a.compareTo(b) : b.compareTo(a);
|
||||
};
|
||||
return documents.stream()
|
||||
.sorted(Comparator.comparing(doc -> {
|
||||
Person s = doc.getSender();
|
||||
return s != null ? s.getLastName() + " " + s.getFirstName() : "";
|
||||
}, nullSafeComparator))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private List<Document> sortByFirstReceiver(List<Document> documents, String dir) {
|
||||
boolean ascending = "ASC".equalsIgnoreCase(dir);
|
||||
Comparator<String> nullSafeComparator = (a, b) -> {
|
||||
|
||||
Reference in New Issue
Block a user