-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13215 from SORMAS-Foundation/feature-13190_add_ra…
…nge_filter_for_birthdate #13190 - Add date range filter for birthdate (from-to) to Persons, Ca…
- Loading branch information
Showing
17 changed files
with
431 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...as-backend/src/main/java/de/symeda/sormas/backend/util/BirthdateRangeFilterPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package de.symeda.sormas.backend.util; | ||
|
||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.From; | ||
import javax.persistence.criteria.Predicate; | ||
|
||
import de.symeda.sormas.backend.common.CriteriaBuilderHelper; | ||
import de.symeda.sormas.backend.person.Person; | ||
|
||
public class BirthdateRangeFilterPredicate { | ||
|
||
public static Predicate createBirthdateRangeFilter( | ||
Date birthdateFrom, | ||
Date birthdateTo, | ||
boolean includePartialMatch, | ||
CriteriaBuilder cb, | ||
From<?, Person> personFrom, | ||
Predicate filter) { | ||
if (birthdateFrom != null) { | ||
Calendar calendarBirthdateFrom = Calendar.getInstance(); | ||
calendarBirthdateFrom.setTime(birthdateFrom); | ||
int birthdateFromCriteriaYear = calendarBirthdateFrom.get(Calendar.YEAR); | ||
int birthdateFromCriteriaMonth = calendarBirthdateFrom.get(Calendar.MONTH) + 1; | ||
int birthdateFromCriteriaDay = calendarBirthdateFrom.get(Calendar.DAY_OF_MONTH); | ||
|
||
Predicate yearPredicate = cb.greaterThan(personFrom.get(Person.BIRTHDATE_YYYY), birthdateFromCriteriaYear); | ||
|
||
Predicate monthPredicate = cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateFromCriteriaYear); | ||
monthPredicate = cb.and(monthPredicate, cb.greaterThan(personFrom.get(Person.BIRTHDATE_MM), birthdateFromCriteriaMonth)); | ||
|
||
Predicate dayPredicate = cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateFromCriteriaYear); | ||
dayPredicate = cb.and(dayPredicate, cb.equal(personFrom.get(Person.BIRTHDATE_MM), birthdateFromCriteriaMonth)); | ||
dayPredicate = cb.and(dayPredicate, cb.greaterThanOrEqualTo(personFrom.get(Person.BIRTHDATE_DD), birthdateFromCriteriaDay)); | ||
|
||
if (includePartialMatch) { | ||
Predicate sameYearPartialMatchPredicate = cb | ||
.and(cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateFromCriteriaYear), cb.isNull(personFrom.get(Person.BIRTHDATE_MM))); | ||
Predicate sameYearMonthPartialMatchPredicate = cb.and( | ||
cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateFromCriteriaYear), | ||
cb.equal(personFrom.get(Person.BIRTHDATE_MM), birthdateFromCriteriaMonth), | ||
cb.isNull(personFrom.get(Person.BIRTHDATE_DD))); | ||
filter = CriteriaBuilderHelper.and( | ||
cb, | ||
filter, | ||
cb.or(yearPredicate, monthPredicate, dayPredicate, sameYearPartialMatchPredicate, sameYearMonthPartialMatchPredicate)); | ||
} else { | ||
filter = CriteriaBuilderHelper.and(cb, filter, cb.or(yearPredicate, monthPredicate, dayPredicate)); | ||
} | ||
} | ||
|
||
if (birthdateTo != null) { | ||
Calendar calendarBirthdateTo = Calendar.getInstance(); | ||
calendarBirthdateTo.setTime(birthdateTo); | ||
int birthdateToCriteriaYear = calendarBirthdateTo.get(Calendar.YEAR); | ||
int birthdateToCriteriaMonth = calendarBirthdateTo.get(Calendar.MONTH) + 1; | ||
int birthdateToCriteriaDay = calendarBirthdateTo.get(Calendar.DAY_OF_MONTH); | ||
|
||
Predicate yearPredicate = cb.lessThan(personFrom.get(Person.BIRTHDATE_YYYY), birthdateToCriteriaYear); | ||
|
||
Predicate monthPredicate = cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateToCriteriaYear); | ||
monthPredicate = cb.and(monthPredicate, cb.lessThan(personFrom.get(Person.BIRTHDATE_MM), birthdateToCriteriaMonth)); | ||
|
||
Predicate dayPredicate = cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateToCriteriaYear); | ||
dayPredicate = cb.and(dayPredicate, cb.equal(personFrom.get(Person.BIRTHDATE_MM), birthdateToCriteriaMonth)); | ||
dayPredicate = cb.and(dayPredicate, cb.lessThanOrEqualTo(personFrom.get(Person.BIRTHDATE_DD), birthdateToCriteriaDay)); | ||
|
||
if (includePartialMatch) { | ||
Predicate sameYearPartialMatchPredicate = | ||
cb.and(cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateToCriteriaYear), cb.isNull(personFrom.get(Person.BIRTHDATE_MM))); | ||
Predicate sameYearMonthPartialMatchPredicate = cb.and( | ||
cb.equal(personFrom.get(Person.BIRTHDATE_YYYY), birthdateToCriteriaYear), | ||
cb.equal(personFrom.get(Person.BIRTHDATE_MM), birthdateToCriteriaMonth), | ||
cb.isNull(personFrom.get(Person.BIRTHDATE_DD))); | ||
filter = CriteriaBuilderHelper.and( | ||
cb, | ||
filter, | ||
cb.or(yearPredicate, monthPredicate, dayPredicate, sameYearPartialMatchPredicate, sameYearMonthPartialMatchPredicate)); | ||
} else { | ||
filter = CriteriaBuilderHelper.and(cb, filter, cb.or(yearPredicate, monthPredicate, dayPredicate)); | ||
} | ||
} | ||
return filter; | ||
} | ||
} |
Oops, something went wrong.