use of de.symeda.sormas.api.clinicalcourse.ClinicalVisitCriteria in project SORMAS-Project by hzi-braunschweig.
the class CaseFacadeEjb method mergeCase.
private void mergeCase(CaseDataDto leadCaseData, CaseDataDto otherCaseData, boolean cloning) {
// 1 Merge Dtos
// 1.1 Case
copyDtoValues(leadCaseData, otherCaseData, cloning);
save(leadCaseData, !cloning, true, true, false);
// 1.2 Person - Only merge when the persons have different UUIDs
if (!cloning && !DataHelper.equal(leadCaseData.getPerson().getUuid(), otherCaseData.getPerson().getUuid())) {
PersonDto leadPerson = personFacade.getPersonByUuid(leadCaseData.getPerson().getUuid());
PersonDto otherPerson = personFacade.getPersonByUuid(otherCaseData.getPerson().getUuid());
personFacade.mergePerson(leadPerson, otherPerson);
} else {
assert (DataHelper.equal(leadCaseData.getPerson().getUuid(), otherCaseData.getPerson().getUuid()));
}
// 2 Change CaseReference
Case leadCase = service.getByUuid(leadCaseData.getUuid());
Case otherCase = service.getByUuid(otherCaseData.getUuid());
// 2.1 Contacts
List<Contact> contacts = contactService.findBy(new ContactCriteria().caze(otherCase.toReference()), null);
for (Contact contact : contacts) {
if (cloning) {
ContactDto newContact = ContactDto.build(leadCase.toReference(), leadCase.getDisease(), leadCase.getDiseaseDetails(), leadCase.getDiseaseVariant());
newContact.setPerson(new PersonReferenceDto(contact.getPerson().getUuid()));
DtoHelper.copyDtoValues(newContact, contactFacade.toDto(contact), cloning);
contactFacade.save(newContact, false, false);
} else {
// simply move existing entities to the merge target
contact.setCaze(leadCase);
contactService.ensurePersisted(contact);
}
}
// 2.2 Samples
List<Sample> samples = sampleService.findBy(new SampleCriteria().caze(otherCase.toReference()), null);
for (Sample sample : samples) {
if (cloning) {
SampleDto newSample = SampleDto.build(sample.getReportingUser().toReference(), leadCase.toReference());
DtoHelper.copyDtoValues(newSample, SampleFacadeEjb.toDto(sample), cloning);
sampleFacade.saveSample(newSample, false, true, true);
// 2.2.1 Pathogen Tests
for (PathogenTest pathogenTest : sample.getPathogenTests()) {
PathogenTestDto newPathogenTest = PathogenTestDto.build(newSample.toReference(), pathogenTest.getLabUser().toReference());
DtoHelper.copyDtoValues(newPathogenTest, PathogenTestFacadeEjbLocal.toDto(pathogenTest), cloning);
sampleTestFacade.savePathogenTest(newPathogenTest);
}
for (AdditionalTest additionalTest : sample.getAdditionalTests()) {
AdditionalTestDto newAdditionalTest = AdditionalTestDto.build(newSample.toReference());
DtoHelper.copyDtoValues(newAdditionalTest, AdditionalTestFacadeEjbLocal.toDto(additionalTest), cloning);
additionalTestFacade.saveAdditionalTest(newAdditionalTest);
}
} else {
// simply move existing entities to the merge target
sample.setAssociatedCase(leadCase);
sampleService.ensurePersisted(sample);
}
}
// 2.3 Tasks
if (!cloning) {
// simply move existing entities to the merge target
List<Task> tasks = taskService.findBy(new TaskCriteria().caze(new CaseReferenceDto(otherCase.getUuid())), true);
for (Task task : tasks) {
task.setCaze(leadCase);
taskService.ensurePersisted(task);
}
}
// 3 Change Therapy Reference
// 3.1 Treatments
List<Treatment> treatments = treatmentService.findBy(new TreatmentCriteria().therapy(new TherapyReferenceDto(otherCase.getTherapy().getUuid())));
TherapyReferenceDto leadCaseTherapyReference = new TherapyReferenceDto(leadCase.getTherapy().getUuid());
for (Treatment treatment : treatments) {
if (cloning) {
TreatmentDto newTreatment = TreatmentDto.build(leadCaseTherapyReference);
DtoHelper.copyDtoValues(newTreatment, TreatmentFacadeEjb.toDto(treatment), cloning);
treatmentFacade.saveTreatment(newTreatment);
} else {
// simply move existing entities to the merge target
treatment.setTherapy(leadCase.getTherapy());
treatmentService.ensurePersisted(treatment);
}
}
// 3.2 Prescriptions
List<Prescription> prescriptions = prescriptionService.findBy(new PrescriptionCriteria().therapy(new TherapyReferenceDto(otherCase.getTherapy().getUuid())));
for (Prescription prescription : prescriptions) {
if (cloning) {
PrescriptionDto newPrescription = PrescriptionDto.buildPrescription(leadCaseTherapyReference);
DtoHelper.copyDtoValues(newPrescription, PrescriptionFacadeEjb.toDto(prescription), cloning);
prescriptionFacade.savePrescription(newPrescription);
} else {
// simply move existing entities to the merge target
prescription.setTherapy(leadCase.getTherapy());
prescriptionService.ensurePersisted(prescription);
}
}
// 4 Change Clinical Course Reference
// 4.1 Clinical Visits
List<ClinicalVisit> clinicalVisits = clinicalVisitService.findBy(new ClinicalVisitCriteria().clinicalCourse(new ClinicalCourseReferenceDto(otherCase.getClinicalCourse().getUuid())));
for (ClinicalVisit clinicalVisit : clinicalVisits) {
if (cloning) {
ClinicalVisitDto newClinicalVisit = ClinicalVisitDto.build(leadCaseData.getClinicalCourse().toReference(), leadCase.getDisease());
DtoHelper.copyDtoValues(newClinicalVisit, ClinicalVisitFacadeEjb.toDto(clinicalVisit), cloning);
clinicalVisitFacade.saveClinicalVisit(newClinicalVisit, leadCase.getUuid(), false);
} else {
// simply move existing entities to the merge target
clinicalVisit.setClinicalCourse(leadCase.getClinicalCourse());
clinicalVisitService.ensurePersisted(clinicalVisit);
}
}
// (set the person and the disease of the visit, saveVisit does the rest)
for (VisitDto otherVisit : otherCase.getVisits().stream().map(VisitFacadeEjb::toDto).collect(Collectors.toList())) {
otherVisit.setPerson(leadCaseData.getPerson());
otherVisit.setDisease(leadCaseData.getDisease());
visitFacade.saveVisit(otherVisit);
}
// 6 Documents
List<Document> documents = documentService.getRelatedToEntity(DocumentRelatedEntityType.CASE, otherCase.getUuid());
for (Document document : documents) {
document.setRelatedEntityUuid(leadCaseData.getUuid());
documentService.ensurePersisted(document);
}
// 7 Persist Event links through eventparticipants
Set<EventParticipant> eventParticipants = otherCase.getEventParticipants();
for (EventParticipant eventParticipant : eventParticipants) {
eventParticipant.setResultingCase(leadCase);
eventParticipantService.ensurePersisted(eventParticipant);
}
otherCase.getEventParticipants().clear();
// 8 Exposures - Make sure there are no two probable infection environments
// if there are more than 2 exposures marked as probable infection environment, find the one that originates from the otherCase and set it to false
// the one originating from the otherCase should always be found at the higher index
List<Exposure> probableExposuresList = leadCase.getEpiData().getExposures().stream().filter(Exposure::isProbableInfectionEnvironment).collect(Collectors.toList());
while (probableExposuresList.size() >= 2) {
// should never be > 2, but still make sure to set all but one exposures to false
probableExposuresList.get(probableExposuresList.size() - 1).setProbableInfectionEnvironment(false);
exposureService.ensurePersisted(probableExposuresList.get(probableExposuresList.size() - 1));
probableExposuresList.remove(probableExposuresList.size() - 1);
}
// 9 Reports
List<SurveillanceReport> surveillanceReports = surveillanceReportService.getByCaseUuids(Collections.singletonList(otherCase.getUuid()));
surveillanceReports.forEach(surveillanceReport -> {
SurveillanceReportDto surveillanceReportDto = SurveillanceReportFacadeEjb.toDto(surveillanceReport);
surveillanceReportDto.setCaze(leadCase.toReference());
surveillanceReportFacade.saveSurveillanceReport(surveillanceReportDto);
});
// 10 Activity as case
final EpiData otherEpiData = otherCase.getEpiData();
if (otherEpiData != null && YesNoUnknown.YES == otherEpiData.getActivityAsCaseDetailsKnown() && CollectionUtils.isNotEmpty(otherEpiData.getActivitiesAsCase())) {
final EpiData leadEpiData = leadCase.getEpiData();
leadEpiData.setActivityAsCaseDetailsKnown(YesNoUnknown.YES);
epiDataService.ensurePersisted(leadEpiData);
}
// Travel entries reference
List<TravelEntry> travelEntries = travelEntryService.getAllByResultingCase(otherCase);
travelEntries.forEach(t -> {
t.setResultingCase(leadCase);
travelEntryService.ensurePersisted(t);
});
}
use of de.symeda.sormas.api.clinicalcourse.ClinicalVisitCriteria in project SORMAS-Project by hzi-braunschweig.
the class CaseService method delete.
@Override
public void delete(Case caze) {
// Mark all contacts associated with this case as deleted and remove this case
// from any contacts where it is set as the resulting case
List<Contact> contacts = contactService.findBy(new ContactCriteria().caze(caze.toReference()), null);
for (Contact contact : contacts) {
contactService.delete(contact);
}
contacts = contactService.getAllByResultingCase(caze);
for (Contact contact : contacts) {
contact.setResultingCase(null);
externalJournalService.handleExternalJournalPersonUpdateAsync(contact.getPerson().toReference());
contactService.ensurePersisted(contact);
}
caze.getSamples().stream().filter(sample -> sample.getAssociatedContact() == null && sample.getAssociatedEventParticipant() == null).forEach(sample -> sampleService.delete(sample));
// Delete all tasks associated with this case
List<Task> tasks = taskService.findBy(new TaskCriteria().caze(new CaseReferenceDto(caze.getUuid())), true);
for (Task task : tasks) {
taskService.delete(task);
}
// Delete all prescriptions/treatments/clinical visits
if (caze.getTherapy() != null) {
TherapyReferenceDto therapy = new TherapyReferenceDto(caze.getTherapy().getUuid());
treatmentService.findBy(new TreatmentCriteria().therapy(therapy)).stream().forEach(t -> treatmentService.delete(t));
prescriptionService.findBy(new PrescriptionCriteria().therapy(therapy)).stream().forEach(p -> prescriptionService.delete(p));
}
if (caze.getClinicalCourse() != null) {
ClinicalCourseReferenceDto clinicalCourse = new ClinicalCourseReferenceDto(caze.getClinicalCourse().getUuid());
clinicalVisitService.findBy(new ClinicalVisitCriteria().clinicalCourse(clinicalCourse)).stream().forEach(c -> clinicalVisitService.delete(c));
}
// Remove all events linked to case by removing the case_id from event participant
caze.getEventParticipants().stream().forEach(eventParticipant -> eventParticipant.setResultingCase(null));
// Unlink TravelEntries where this case is set as the resulting case
List<TravelEntry> travelEntries = travelEntryService.getAllByResultingCase(caze);
for (TravelEntry travelEntry : travelEntries) {
travelEntry.setResultingCase(null);
travelEntryService.ensurePersisted(travelEntry);
}
// Unlink Immunizations where this case is set as the related case
immunizationService.unlinkRelatedCase(caze);
// Mark the case as deleted
super.delete(caze);
}
Aggregations