use of de.symeda.sormas.api.therapy.TreatmentCriteria 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);
t.setPerson(leadCase.getPerson());
travelEntryService.ensurePersisted(t);
});
}
use of de.symeda.sormas.api.therapy.TreatmentCriteria in project SORMAS-Project by hzi-braunschweig.
the class TherapyView method createTreatmentsHeader.
private VerticalLayout createTreatmentsHeader() {
VerticalLayout treatmentsHeader = new VerticalLayout();
treatmentsHeader.setMargin(false);
treatmentsHeader.setSpacing(false);
treatmentsHeader.setWidth(100, Unit.PERCENTAGE);
HorizontalLayout headlineRow = new HorizontalLayout();
headlineRow.setMargin(false);
headlineRow.setSpacing(true);
headlineRow.setWidth(100, Unit.PERCENTAGE);
{
Label treatmentsLabel = new Label(I18nProperties.getString(Strings.headingTreatments));
CssStyles.style(treatmentsLabel, CssStyles.H3);
headlineRow.addComponent(treatmentsLabel);
headlineRow.setExpandRatio(treatmentsLabel, 1);
// Bulk operations
if (UserProvider.getCurrent().hasUserRight(UserRight.PERFORM_BULK_OPERATIONS)) {
MenuBar bulkOperationsDropdown = MenuBarHelper.createDropDown(Captions.bulkActions, new MenuBarHelper.MenuBarItem(I18nProperties.getCaption(Captions.bulkDelete), VaadinIcons.TRASH, selectedItem -> {
ControllerProvider.getTherapyController().deleteAllSelectedTreatments(treatmentGrid.getSelectedRows(), new Runnable() {
public void run() {
reloadTreatmentGrid();
}
});
}));
headlineRow.addComponent(bulkOperationsDropdown);
headlineRow.setComponentAlignment(bulkOperationsDropdown, Alignment.MIDDLE_RIGHT);
}
Button newTreatmentButton = ButtonHelper.createButton(Captions.treatmentNewTreatment, e -> {
ControllerProvider.getTherapyController().openTreatmentCreateForm(treatmentCriteria.getTherapy(), this::reloadTreatmentGrid);
});
headlineRow.addComponent(newTreatmentButton);
headlineRow.setComponentAlignment(newTreatmentButton, Alignment.MIDDLE_RIGHT);
}
treatmentsHeader.addComponent(headlineRow);
HorizontalLayout filterRow = new HorizontalLayout();
filterRow.setMargin(false);
filterRow.setSpacing(true);
{
treatmentTypeFilter = ComboBoxHelper.createComboBoxV7();
treatmentTypeFilter.setWidth(140, Unit.PIXELS);
treatmentTypeFilter.setInputPrompt(I18nProperties.getPrefixCaption(TreatmentDto.I18N_PREFIX, TreatmentDto.TREATMENT_TYPE));
treatmentTypeFilter.addItems((Object[]) TreatmentType.values());
treatmentTypeFilter.addValueChangeListener(e -> {
treatmentCriteria.treatmentType(((TreatmentType) e.getProperty().getValue()));
navigateTo(treatmentCriteria);
});
filterRow.addComponent(treatmentTypeFilter);
treatmentTextFilter = new TextField();
treatmentTextFilter.setWidth(300, Unit.PIXELS);
treatmentTextFilter.setNullRepresentation("");
treatmentTextFilter.setInputPrompt(I18nProperties.getString(Strings.promptTreatmentTextFilter));
treatmentTextFilter.addTextChangeListener(e -> {
treatmentCriteria.textFilter(e.getText());
reloadTreatmentGrid();
});
filterRow.addComponent(treatmentTextFilter);
}
treatmentsHeader.addComponent(filterRow);
return treatmentsHeader;
}
use of de.symeda.sormas.api.therapy.TreatmentCriteria in project SORMAS-Project by hzi-braunschweig.
the class CaseService method deletePermanent.
@Override
public void deletePermanent(Case caze) {
// Delete all tasks associated with this case
Optional.ofNullable(caze.getTasks()).ifPresent(tl -> tl.forEach(t -> taskService.deletePermanent(t)));
// Delete all samples that are only associated with this case
caze.getSamples().stream().filter(sample -> sample.getAssociatedContact() == null && sample.getAssociatedEventParticipant() == null).forEach(sample -> sampleService.deletePermanent(sample));
caze.getVisits().stream().forEach(visit -> {
if (visit.getContacts() == null || visit.getContacts().isEmpty()) {
visitService.deletePermanent(visit);
} else {
visit.setCaze(null);
visitService.ensurePersisted(visit);
}
});
// Delete surveillance reports related to this case
surveillanceReportService.getByCaseUuids(Collections.singletonList(caze.getUuid())).forEach(s -> surveillanceReportService.deletePermanent(s));
// Delete documents related to this case
documentService.getRelatedToEntity(DocumentRelatedEntityType.CASE, caze.getUuid()).forEach(d -> documentService.markAsDeleted(d));
// Delete clinical management data
if (caze.getTherapy() != null) {
TherapyReferenceDto therapy = new TherapyReferenceDto(caze.getTherapy().getUuid());
List<Treatment> treatments = treatmentService.findBy(new TreatmentCriteria().therapy(therapy));
treatments.forEach(t -> treatmentService.deletePermanent(t));
prescriptionService.findBy(new PrescriptionCriteria().therapy(therapy)).forEach(p -> prescriptionService.deletePermanent(p));
}
if (caze.getClinicalCourse() != null) {
ClinicalCourseReferenceDto clinicalCourse = new ClinicalCourseReferenceDto(caze.getClinicalCourse().getUuid());
List<ClinicalVisit> cvs = clinicalVisitService.findBy(new ClinicalVisitCriteria().clinicalCourse(clinicalCourse));
cvs.forEach(c -> clinicalVisitService.deletePermanent(c));
}
// Remove the case from any S2S share info referencing it
sormasToSormasShareInfoService.getByAssociatedEntity(SormasToSormasShareInfo.CAZE, caze.getUuid()).forEach(s -> {
s.setCaze(null);
if (sormasToSormasShareInfoFacade.hasAnyEntityReference(s)) {
sormasToSormasShareInfoService.ensurePersisted(s);
} else {
sormasToSormasShareInfoService.deletePermanent(s);
}
});
// Remove the case from any external share info referencing it
externalShareInfoService.getShareInfoByCase(caze.getUuid()).forEach(e -> {
externalShareInfoService.deletePermanent(e);
});
// Remove the case from all cases in which it has been set as a duplicate
getCasesSetAsDuplicate(caze.getId()).forEach(c -> {
c.setDuplicateOf(null);
ensurePersisted(c);
});
caseFacade.deleteCaseInExternalSurveillanceTool(caze);
deleteCaseLinks(caze);
super.deletePermanent(caze);
}
use of de.symeda.sormas.api.therapy.TreatmentCriteria 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