'How to filter a list using querydsl with a specific permutation given by the user?
I have a method that is filtering Visits (visit have date, and animalType, and medical specialisation) by given parameters.I am using queryDSL to filter list. This is my meth
private List<Visit> customQuery(CustomSearchCommand customSearchCommand){
QVisit visit = QVisit.visit;
JPAQuery<Visit> visits = new JPAQueryFactory(entityManager).selectFrom(visit)
.where(visit.veterinarian.medicalSpecialization.eq(customSearchCommand.getMedicalSpecialization()))
.where(visit.date.after(customSearchCommand.getDateFrom()))
.where(visit.date.before(customSearchCommand.getDateTo()))
.where(visit.veterinarian.animalType.eq(customSearchCommand.getAnimalType()));
return visits.fetch();
}
CustomSearchCommand is an object that provides parameters for filtering. This is class:
public class CustomSearchCommand {
@Size(min = 3, max = 20)
private String medicalSpecialization;
private AnimalType animalType;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateFrom;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateTo;
public String getMedicalSpecialization() {
return medicalSpecialization;
}
public AnimalType getAnimalType() {
return animalType;
}
public LocalDateTime getDateFrom() {
return dateFrom;
}
public LocalDateTime getDateTo() {
return dateTo;
}
}
My question is, How can I change the method of filtering visits so that I can filter visits when the user provides, for example, only the date from and animal type or, for example, only the date from and medical specialization. The point is that the CustomSearch command object may have, for example, the medical specialisation field as null, and then it is to filter the visits by the rest of the parameters.
What is yours advice? How can I fix this? Thank you for your help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
