SORMAS-Project
SORMAS-Project copied to clipboard
DateHelper: No sneaky conversion from `null` to `now`
Problem Description
In #6700 (PR #9942) it was found that many methods of DateHelper (getStartOf/getEndOf/add/substract) relied on joda-time to convert null to now.
Proposed Change
- Let all
getStartOf/getEndOf/add/substractpassnullthrough. - Handle
nullvalues properly where tests fail.
Acceptance Criteria
- [ ] Conversion from
nulltonowis removed.
Implementation Details
DateHelper.handleNull marks where it was needed to convert null to now.
Additional Information
Just don't allow or pass through null does not work. At least 4 places in tests uncovered that logic relies on current null to now conversion.
NullPointerExceptions happening when null is not allowed
// CaseService
public void updateFollowUpDetails(Case caze, boolean followUpStatusChangedByUser) {
//...
// NPE happens here
if (DateHelper.getStartOfDay(currentFollowUpUntil).before(DateHelper.getStartOfDay(untilDate))) {
caze.setOverwriteFollowUpUntil(false);
}
// ContactService
public void updateFollowUpDetails(Contact contact, boolean followUpStatusChangedByUser) {
//...
// NPE happens here
if (DateHelper.getStartOfDay(currentFollowUpUntil).before(DateHelper.getStartOfDay(untilDate))) {
contact.setOverwriteFollowUpUntil(false);
}
// VisitService
public Predicate buildRelevantVisitsFilter(Person person, Disease disease, Date startDate, Date endDate, CriteriaBuilder cb, Root<Visit> from) {
// NPE happens here
startDate = DateHelper.getStartOfDay(startDate);
endDate = DateHelper.getEndOfDay(endDate);
Predicate filter = cb.and(cb.equal(from.get(Visit.PERSON), person), cb.equal(from.get(Visit.DISEASE), disease));
filter = CriteriaBuilderHelper.and(
cb,
filter,
cb.greaterThanOrEqualTo(from.get(Visit.VISIT_DATE_TIME), DateHelper.subtractDays(startDate, FollowUpLogic.ALLOWED_DATE_OFFSET)),
cb.lessThanOrEqualTo(from.get(Visit.VISIT_DATE_TIME), DateHelper.addDays(endDate, FollowUpLogic.ALLOWED_DATE_OFFSET)));
# CaseFacadeEjb.getIndexList: H2Function.timestamp_subtract_days fails with null Date
Caused by: java.lang.NullPointerException
at de.symeda.sormas.api.utils.DateHelper.subtractDays(DateHelper.java:549)
at de.symeda.sormas.backend.H2Function.timestamp_subtract_days(H2Function.java:49)