Skip to content

Commit

Permalink
Refactor vaccinations in case / contact / event participant export SO…
Browse files Browse the repository at this point in the history
…RMAS-Foundation#9193 (SORMAS-Foundation#9843)

* Refactor vaccinations in case / contact / event participant export SORMAS-Foundation#9193

* review findings SORMAS-Foundation#9193

* review findings SORMAS-Foundation#9193
  • Loading branch information
syntakker authored Jul 22, 2022
1 parent 9332459 commit 4e098f9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class EventParticipantExportDto implements Serializable {
private long personAddressId;

private String eventUuid;
private Date eventReportDateTime;

private final EventStatus eventStatus;
private final EventInvestigationStatus eventInvestigationStatus;
Expand Down Expand Up @@ -177,7 +178,7 @@ public class EventParticipantExportDto implements Serializable {
private long contactCount;

//@formatter:off
public EventParticipantExportDto(long id, long personId, String personUuid, String eventParticipantUuid, String nationalHealthId, long personAddressId, boolean isInJurisdiction, String eventUuid,
public EventParticipantExportDto(long id, long personId, String personUuid, String eventParticipantUuid, String nationalHealthId, long personAddressId, boolean isInJurisdiction, String eventUuid, Date eventReportDateTime,

EventStatus eventStatus, EventInvestigationStatus eventInvestigationStatus, Disease eventDisease, TypeOfPlace typeOfPlace, Date eventStartDate, Date eventEndDate, String eventTitle, String eventDesc,
String eventRegion, String eventDistrict, String eventCommunity, String eventCity, String eventStreet, String eventHouseNumber,
Expand All @@ -197,6 +198,7 @@ public EventParticipantExportDto(long id, long personId, String personUuid, Stri
this.nationalHealthId = nationalHealthId;
this.personAddressId = personAddressId;
this.eventUuid = eventUuid;
this.eventReportDateTime = eventReportDateTime;

this.eventStatus = eventStatus;
this.eventInvestigationStatus = eventInvestigationStatus;
Expand Down Expand Up @@ -978,4 +980,8 @@ public void setVaccineUniiCode(String vaccineUniiCode) {
public void setVaccineAtcCode(String vaccineAtcCode) {
this.vaccineAtcCode = vaccineAtcCode;
}

public Date getEventReportDateTime() {
return eventReportDateTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -887,16 +887,13 @@ public List<CaseExportDto> getExportList(

List<Long> resultCaseIds = resultList.stream().map(CaseExportDto::getId).collect(Collectors.toList());
if (!resultList.isEmpty()) {
Map<Long, Symptoms> symptoms = null;
if (ExportHelper.shouldExportFields(exportConfiguration, CaseDataDto.SYMPTOMS)) {
List<Symptoms> symptomsList = null;
CriteriaQuery<Symptoms> symptomsCq = cb.createQuery(Symptoms.class);
Root<Symptoms> symptomsRoot = symptomsCq.from(Symptoms.class);
Expression<String> symptomsIdsExpr = symptomsRoot.get(Symptoms.ID);
symptomsCq.where(symptomsIdsExpr.in(resultList.stream().map(CaseExportDto::getSymptomsId).collect(Collectors.toList())));
symptomsList = em.createQuery(symptomsCq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList();
symptoms = symptomsList.stream().collect(Collectors.toMap(Symptoms::getId, Function.identity()));
}
List<Symptoms> symptomsList = null;
CriteriaQuery<Symptoms> symptomsCq = cb.createQuery(Symptoms.class);
Root<Symptoms> symptomsRoot = symptomsCq.from(Symptoms.class);
Expression<String> symptomsIdsExpr = symptomsRoot.get(Symptoms.ID);
symptomsCq.where(symptomsIdsExpr.in(resultList.stream().map(CaseExportDto::getSymptomsId).collect(Collectors.toList())));
symptomsList = em.createQuery(symptomsCq).setHint(ModelConstants.HINT_HIBERNATE_READ_ONLY, true).getResultList();
Map<Long, Symptoms> symptoms = symptomsList.stream().collect(Collectors.toMap(Symptoms::getId, Function.identity()));

Map<Long, HealthConditions> healthConditions = null;
if (exportType == null || exportType == CaseExportType.CASE_MANAGEMENT) {
Expand Down Expand Up @@ -1050,7 +1047,7 @@ public List<CaseExportDto> getExportList(
if (exportConfiguration == null || exportConfiguration.getProperties().contains(CaseExportDto.COUNTRY)) {
exportDto.setCountry(configFacade.getEpidPrefix());
}
if (symptoms != null) {
if (ExportHelper.shouldExportFields(exportConfiguration, CaseDataDto.SYMPTOMS)) {
Optional.ofNullable(symptoms.get(exportDto.getSymptomsId()))
.ifPresent(symptom -> exportDto.setSymptoms(SymptomsFacadeEjb.toDto(symptom)));
}
Expand Down Expand Up @@ -1144,10 +1141,12 @@ public List<CaseExportDto> getExportList(
filteredImmunizations.sort(Comparator.comparing(i -> ImmunizationEntityHelper.getDateForComparison(i, false)));
Immunization mostRecentImmunization = filteredImmunizations.get(filteredImmunizations.size() - 1);
Integer numberOfDoses = mostRecentImmunization.getNumberOfDoses();
Date onsetDate = Optional.ofNullable(symptoms.get(exportDto.getSymptomsId())).map(Symptoms::getOnsetDate).orElse(null);

List<Vaccination> relevantSortedVaccinations = getRelevantSortedVaccinations(
exportDto.getUuid(),
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()));
List<Vaccination> relevantSortedVaccinations = vaccinationService.getRelevantSortedVaccinations(
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()),
onsetDate,
exportDto.getReportDate());
Vaccination firstVaccination = null;
Vaccination lastVaccination = null;

Expand Down Expand Up @@ -1290,15 +1289,6 @@ private Map<Long, UserReference> getCaseUsersForExport(List<CaseExportDto> resul
return caseUsers;
}

private List<Vaccination> getRelevantSortedVaccinations(String caseUuid, List<Vaccination> vaccinations) {
Case caze = caseService.getByUuid(caseUuid);

return vaccinations.stream()
.filter(v -> vaccinationService.isVaccinationRelevant(caze, v))
.sorted(Comparator.comparing(ImmunizationEntityHelper::getVaccinationDateForComparison))
.collect(Collectors.toList());
}

private String getNumberOfDosesFromVaccinations(Vaccination vaccination) {
return vaccination != null ? vaccination.getVaccineDose() : "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,10 @@ public List<ContactExportDto> getExportList(
Immunization mostRecentImmunization = filteredImmunizations.get(filteredImmunizations.size() - 1);
Integer numberOfDoses = mostRecentImmunization.getNumberOfDoses();

List<Vaccination> relevantSortedVaccinations = getRelevantSortedVaccinations(
exportContact.getUuid(),
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()));
List<Vaccination> relevantSortedVaccinations = vaccinationService.getRelevantSortedVaccinations(
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()),
exportContact.getLastContactDate(),
exportContact.getReportDate());
Vaccination firstVaccination = null;
Vaccination lastVaccination = null;

Expand Down Expand Up @@ -2262,15 +2263,6 @@ private User getRandomDistrictContactResponsible(District district) {
return userService.getRandomDistrictUser(district, UserRight.CONTACT_RESPONSIBLE);
}

private List<Vaccination> getRelevantSortedVaccinations(String caseUuid, List<Vaccination> vaccinations) {
Contact contact = contactService.getByUuid(caseUuid);

return vaccinations.stream()
.filter(v -> vaccinationService.isVaccinationRelevant(contact, v))
.sorted(Comparator.comparing(ImmunizationEntityHelper::getVaccinationDateForComparison))
.collect(Collectors.toList());
}

private String getNumberOfDosesFromVaccinations(Vaccination vaccination) {
return vaccination != null ? vaccination.getVaccineDose() : "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,6 @@ private void archiveAllArchivableEventParticipants(int daysAfterEventParticipant
}
}

private List<Vaccination> getRelevantSortedVaccinations(String eventUuid, List<Vaccination> vaccinations) {
Event event = eventService.getByUuid(eventUuid);

return vaccinations.stream()
.filter(v -> vaccinationService.isVaccinationRelevant(event, v))
.sorted(Comparator.comparing(ImmunizationEntityHelper::getVaccinationDateForComparison))
.collect(Collectors.toList());
}

private String getNumberOfDosesFromVaccinations(Vaccination vaccination) {
return vaccination != null ? vaccination.getVaccineDose() : "";
}
Expand Down Expand Up @@ -659,6 +650,7 @@ public List<EventParticipantExportDto> getExportList(
JurisdictionHelper.booleanSelector(cb, service.inJurisdictionOrOwned(eventParticipantQueryContext)),

event.get(Event.UUID),
event.get(Event.REPORT_DATE_TIME),

event.get(Event.EVENT_STATUS),
event.get(Event.EVENT_INVESTIGATION_STATUS),
Expand Down Expand Up @@ -806,9 +798,11 @@ public List<EventParticipantExportDto> getExportList(
Immunization mostRecentImmunization = filteredImmunizations.get(filteredImmunizations.size() - 1);
Integer numberOfDoses = mostRecentImmunization.getNumberOfDoses();

List<Vaccination> relevantSortedVaccinations = getRelevantSortedVaccinations(
exportDto.getEventUuid(),
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()));
List<Vaccination> relevantSortedVaccinations = vaccinationService.getRelevantSortedVaccinations(
filteredImmunizations.stream().flatMap(i -> i.getVaccinations().stream()).collect(Collectors.toList()),
exportDto.getEventStartDate(),
exportDto.getEventEndDate(),
exportDto.getEventReportDateTime());
Vaccination firstVaccination = null;
Vaccination lastVaccination = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static de.symeda.sormas.backend.ExtendedPostgreSQL94Dialect.TIMESTAMP_SUBTRACT_DAYS;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -54,6 +55,7 @@
import de.symeda.sormas.backend.common.CriteriaBuilderHelper;
import de.symeda.sormas.backend.contact.Contact;
import de.symeda.sormas.backend.event.Event;
import de.symeda.sormas.backend.immunization.ImmunizationEntityHelper;
import de.symeda.sormas.backend.immunization.entity.Immunization;
import de.symeda.sormas.backend.person.Person;
import de.symeda.sormas.backend.symptoms.Symptoms;
Expand Down Expand Up @@ -223,25 +225,31 @@ public boolean isVaccinationRelevant(Contact contact, Vaccination vaccination) {
* @return true when the vaccination is relevant, false otherwise
*/
public boolean isVaccinationRelevant(Event event, Vaccination vaccination) {
if (event.getStartDate() != null) {
return isVaccinationRelevant(vaccination, event.getStartDate(), null);

} else {
return isVaccinationRelevant(vaccination, event.getEndDate(), event.getReportDateTime());
}

return isVaccinationRelevant(vaccination, event.getStartDate(), event.getEndDate(), event.getReportDateTime());
}

/*
* HEADS UP! If you make changes here, you most probably also need to update the database queries in
* ContactService.updateVaccinationStatuses(...) and
* EventParticipantService.updateVaccinationStatuses(...).
*/
private boolean isVaccinationRelevant(Vaccination vaccination, Date primaryDate, Date fallbackDate) {
private boolean isVaccinationRelevant(Vaccination vaccination, Date... relevanceFilterDates) {

Date relevantVaccineDate = getRelevantVaccineDate(vaccination);
return primaryDate != null
? DateHelper.getEndOfDay(relevantVaccineDate).before(primaryDate)
: DateHelper.getEndOfDay(relevantVaccineDate).before(fallbackDate);
for (Date comparisonDate : relevanceFilterDates) {
if (comparisonDate != null) {
return DateHelper.getEndOfDay(relevantVaccineDate).before(comparisonDate);
}
}
return false;
}

public List<Vaccination> getRelevantSortedVaccinations(List<Vaccination> vaccinations, Date... relevanceFilterDates) {

return vaccinations.stream()
.filter(v -> isVaccinationRelevant(v, relevanceFilterDates))
.sorted(Comparator.comparing(ImmunizationEntityHelper::getVaccinationDateForComparison))
.collect(Collectors.toList());
}

public Predicate getRelevantVaccinationPredicate(
Expand Down Expand Up @@ -271,7 +279,7 @@ public Predicate getRelevantVaccinationPredicate(

/**
* HEADS UP! When this method gets changed, most probably the database logic in
* {@link de.symeda.sormas.backend.vaccination.VaccinationService#isVaccinationRelevant(Vaccination, Date, Date)}
* {@link de.symeda.sormas.backend.vaccination.VaccinationService#isVaccinationRelevant(Vaccination, Date...)}
* will also need an update.
*/
private Predicate getRelevantVaccinationPredicate(
Expand Down

0 comments on commit 4e098f9

Please sign in to comment.