Search in sources :

Example 1 with ImportSimilarityResultOption

use of de.symeda.sormas.ui.importer.ImportSimilarityResultOption in project SORMAS-Project by hzi-braunschweig.

the class ContactImporter method importDataFromCsvLine.

@Override
protected ImportLineResult importDataFromCsvLine(String[] values, String[] entityClasses, String[] entityProperties, String[][] entityPropertyPaths, boolean firstLine) throws IOException, InterruptedException {
    // Check whether the new line has the same length as the header line
    if (values.length > entityProperties.length) {
        writeImportError(values, I18nProperties.getValidationError(Validations.importLineTooLong));
        return ImportLineResult.ERROR;
    }
    // regenerate the UUID to prevent overwrite in case of export and import of the same entities
    int uuidIndex = ArrayUtils.indexOf(entityProperties, ContactDto.UUID);
    if (uuidIndex >= 0) {
        values[uuidIndex] = DataHelper.createUuid();
    }
    final PersonDto newPersonTemp = PersonDto.buildImportEntity();
    final ContactDto newContactTemp = caze != null ? ContactDto.build(caze) : ContactDto.build();
    newContactTemp.setReportingUser(currentUser.toReference());
    final List<VaccinationDto> vaccinations = new ArrayList<>();
    ImportRelatedObjectsMapper.Builder relatedObjectsMapperBuilder = new ImportRelatedObjectsMapper.Builder();
    if (FacadeProvider.getFeatureConfigurationFacade().isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
        relatedObjectsMapperBuilder.addMapper(VaccinationDto.class, vaccinations, () -> VaccinationDto.build(currentUser.toReference()), this::insertColumnEntryIntoRelatedObject);
    }
    ImportRelatedObjectsMapper relatedMapper = relatedObjectsMapperBuilder.build();
    boolean contactHasImportError = insertRowIntoData(values, entityClasses, entityPropertyPaths, true, importColumnInformation -> {
        try {
            if (!relatedMapper.map(importColumnInformation)) {
                // If the cell entry is not empty, try to insert it into the current contact or person object
                if (!StringUtils.isEmpty(importColumnInformation.getValue())) {
                    insertColumnEntryIntoData(newContactTemp, newPersonTemp, importColumnInformation.getValue(), importColumnInformation.getEntityPropertyPath());
                }
            }
        } catch (ImportErrorException | InvalidColumnException e) {
            return e;
        }
        return null;
    });
    // try to assign the contact to an existing case
    if (caze == null && newContactTemp.getCaseIdExternalSystem() != null) {
        CaseDataDto existingCase = FacadeProvider.getCaseFacade().getCaseDataByUuid(newContactTemp.getCaseIdExternalSystem().trim());
        if (existingCase != null) {
            newContactTemp.assignCase(existingCase);
            newContactTemp.setCaseIdExternalSystem(null);
        }
    }
    // If the row does not have any import errors, call the backend validation of all associated entities
    if (!contactHasImportError) {
        try {
            FacadeProvider.getPersonFacade().validate(newPersonTemp);
            FacadeProvider.getContactFacade().validate(newContactTemp);
        } catch (ValidationRuntimeException e) {
            contactHasImportError = true;
            writeImportError(values, e.getMessage());
        }
    }
    PersonDto newPerson = newPersonTemp;
    // Sanitize non-HOME address
    PersonHelper.sanitizeNonHomeAddress(newPerson);
    // if there are any, display a window to resolve the conflict to the user
    if (!contactHasImportError) {
        try {
            ContactImportConsumer consumer = new ContactImportConsumer();
            ImportSimilarityResultOption resultOption = null;
            ContactImportLock personSelectLock = new ContactImportLock();
            // We need to pause the current thread to prevent the import from continuing until the user has acted
            synchronized (personSelectLock) {
                // Call the logic that allows the user to handle the similarity; once this has been done, the LOCK should be notified
                // to allow the importer to resume
                handlePersonSimilarity(newPerson, result -> consumer.onImportResult(result, personSelectLock), (person, similarityResultOption) -> new ContactImportSimilarityResult(person, null, similarityResultOption), Strings.infoSelectOrCreatePersonForImport, currentUI);
                try {
                    if (!personSelectLock.wasNotified) {
                        personSelectLock.wait();
                    }
                } catch (InterruptedException e) {
                    logger.error("InterruptedException when trying to perform LOCK.wait() in contact import: " + e.getMessage());
                    throw e;
                }
                if (consumer.result != null) {
                    resultOption = consumer.result.getResultOption();
                }
                // If the user picked an existing person, override the contact person with it
                if (ImportSimilarityResultOption.PICK.equals(resultOption)) {
                    newPerson = FacadeProvider.getPersonFacade().getPersonByUuid(consumer.result.getMatchingPerson().getUuid());
                }
            }
            // or an existing person was picked, save the contact and person to the database
            if (ImportSimilarityResultOption.SKIP.equals(resultOption)) {
                return ImportLineResult.SKIPPED;
            } else {
                boolean skipPersonValidation = ImportSimilarityResultOption.PICK.equals(resultOption);
                final PersonDto savedPerson = FacadeProvider.getPersonFacade().savePerson(newPerson, skipPersonValidation);
                newContactTemp.setPerson(savedPerson.toReference());
                ContactDto newContact = newContactTemp;
                final ContactImportLock contactSelectLock = new ContactImportLock();
                synchronized (contactSelectLock) {
                    handleContactSimilarity(newContactTemp, savedPerson, result -> consumer.onImportResult(result, contactSelectLock));
                    try {
                        if (!contactSelectLock.wasNotified) {
                            contactSelectLock.wait();
                        }
                    } catch (InterruptedException e) {
                        logger.error("InterruptedException when trying to perform LOCK.wait() in contact import: " + e.getMessage());
                        throw e;
                    }
                    if (consumer.result != null) {
                        resultOption = consumer.result.getResultOption();
                    }
                    if (ImportSimilarityResultOption.PICK.equals(resultOption)) {
                        newContact = FacadeProvider.getContactFacade().getByUuid(consumer.result.getMatchingContact().getUuid());
                    }
                }
                // Workaround: Reset the change date to avoid OutdatedEntityExceptions
                newContact.setChangeDate(new Date());
                FacadeProvider.getContactFacade().save(newContact, true, false);
                for (VaccinationDto vaccination : vaccinations) {
                    FacadeProvider.getVaccinationFacade().createWithImmunization(vaccination, newContact.getRegion(), newContact.getDistrict(), newContact.getPerson(), newContact.getDisease());
                }
                consumer.result = null;
                return ImportLineResult.SUCCESS;
            }
        } catch (ValidationRuntimeException e) {
            writeImportError(values, e.getMessage());
            return ImportLineResult.ERROR;
        }
    } else {
        return ImportLineResult.ERROR;
    }
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) VaccinationDto(de.symeda.sormas.api.vaccination.VaccinationDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) Date(java.util.Date) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ContactDto(de.symeda.sormas.api.contact.ContactDto) SimilarContactDto(de.symeda.sormas.api.contact.SimilarContactDto) ContactImportSimilarityResult(de.symeda.sormas.ui.importer.ContactImportSimilarityResult) ImportSimilarityResultOption(de.symeda.sormas.ui.importer.ImportSimilarityResultOption) ImportRelatedObjectsMapper(de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper)

Example 2 with ImportSimilarityResultOption

use of de.symeda.sormas.ui.importer.ImportSimilarityResultOption in project SORMAS-Project by hzi-braunschweig.

the class ContactImporterTest method testImportCaseContacts.

@Test
public void testImportCaseContacts() throws IOException, InvalidColumnException, InterruptedException, CsvValidationException, URISyntaxException {
    ContactFacadeEjb contactFacade = getBean(ContactFacadeEjbLocal.class);
    RDCF rdcf = creator.createRDCF("Abia", "Umuahia North", "Urban Ward 2", "Anelechi Hospital");
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    PersonDto casePerson = creator.createPerson("John", "Smith");
    CaseDataDto caze = creator.createCase(user.toReference(), casePerson.toReference(), Disease.CORONAVIRUS, CaseClassification.CONFIRMED, InvestigationStatus.PENDING, new Date(), rdcf);
    // Successful import of 5 case contacts
    File csvFile = new File(getClass().getClassLoader().getResource("sormas_case_contact_import_test_success.csv").toURI());
    ContactImporter contactImporter = new ContactImporterExtension(csvFile, user, caze);
    ImportResultStatus importResult = contactImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(5, contactFacade.count(null));
    // Person Similarity: pick
    List<SimilarPersonDto> persons = FacadeProvider.getPersonFacade().getSimilarPersonDtos(new PersonSimilarityCriteria());
    csvFile = new File(getClass().getClassLoader().getResource("sormas_case_contact_import_test_similarities.csv").toURI());
    contactImporter = new ContactImporterExtension(csvFile, user, caze) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            List<SimilarPersonDto> entries = new ArrayList<>();
            for (SimilarPersonDto person : persons) {
                if (PersonHelper.areNamesSimilar(newPerson.getFirstName(), newPerson.getLastName(), person.getFirstName(), person.getLastName(), null)) {
                    entries.add(person);
                }
            }
            resultConsumer.accept((T) new ContactImportSimilarityResult(entries.get(0), null, ImportSimilarityResultOption.PICK));
        }
    };
    importResult = contactImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(6, contactFacade.count(null));
    assertEquals(6, getPersonFacade().getAllUuids().size());
    // Person Similarity: skip
    csvFile = new File(getClass().getClassLoader().getResource("sormas_case_contact_import_test_similarities.csv").toURI());
    contactImporter = new ContactImporterExtension(csvFile, user, caze) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            resultConsumer.accept((T) new ContactImportSimilarityResult(null, null, ImportSimilarityResultOption.SKIP));
        }
    };
    importResult = contactImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(6, contactFacade.count(null));
    assertEquals(6, getPersonFacade().getAllUuids().size());
    // Person Similarity: create
    csvFile = new File(getClass().getClassLoader().getResource("sormas_case_contact_import_test_similarities.csv").toURI());
    contactImporter = new ContactImporterExtension(csvFile, user, caze);
    importResult = contactImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(7, contactFacade.count(null));
    assertEquals(7, getPersonFacade().getAllUuids().size());
    // Test import contacts from a commented CSV file
    // Successful import of 5 case contacts
    csvFile = new File(getClass().getClassLoader().getResource("sormas_case_contact_import_test_comment_success.csv").toURI());
    contactImporter = new ContactImporterExtension(csvFile, user, caze);
    importResult = contactImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(12, contactFacade.count(null));
}
Also used : CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) SimilarPersonDto(de.symeda.sormas.api.person.SimilarPersonDto) UserDto(de.symeda.sormas.api.user.UserDto) ContactFacadeEjb(de.symeda.sormas.backend.contact.ContactFacadeEjb) Date(java.util.Date) PersonSimilarityCriteria(de.symeda.sormas.api.person.PersonSimilarityCriteria) SimilarPersonDto(de.symeda.sormas.api.person.SimilarPersonDto) RDCF(de.symeda.sormas.ui.TestDataCreator.RDCF) UI(com.vaadin.ui.UI) ContactImportSimilarityResult(de.symeda.sormas.ui.importer.ContactImportSimilarityResult) ImportSimilarityResultOption(de.symeda.sormas.ui.importer.ImportSimilarityResultOption) ImportResultStatus(de.symeda.sormas.ui.importer.ImportResultStatus) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) AbstractBeanTest(de.symeda.sormas.ui.AbstractBeanTest) Test(org.junit.Test)

Example 3 with ImportSimilarityResultOption

use of de.symeda.sormas.ui.importer.ImportSimilarityResultOption in project SORMAS-Project by hzi-braunschweig.

the class CaseImporterTest method testImportAllCases.

@Test
public void testImportAllCases() throws IOException, InvalidColumnException, InterruptedException, CsvValidationException, URISyntaxException {
    TestDataCreator creator = new TestDataCreator();
    TestDataCreator.RDCF rdcf = creator.createRDCF("Abia", "Umuahia North", "Urban Ward 2", "Anelechi Hospital");
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    // Successful import of 5 cases
    File csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_success.csv").toURI());
    CaseImporterExtension caseImporter = new CaseImporterExtension(csvFile, true, user);
    ImportResultStatus importResult = caseImporter.runImport();
    assertEquals(caseImporter.stringBuilder.toString(), ImportResultStatus.COMPLETED, importResult);
    assertEquals(5, getCaseFacade().count(null));
    // Failed import of 5 cases because of errors
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_errors.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user);
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importResult);
    assertEquals(5, getCaseFacade().count(null));
    // Similarity: skip
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            resultConsumer.accept((T) new CaseImportSimilarityResult(null, null, ImportSimilarityResultOption.SKIP));
        }
    };
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(5, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-5", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Similarity: pick
    List<SimilarPersonDto> persons = FacadeProvider.getPersonFacade().getSimilarPersonDtos(new PersonSimilarityCriteria());
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            List<SimilarPersonDto> entries = new ArrayList<>();
            for (SimilarPersonDto person : persons) {
                if (PersonHelper.areNamesSimilar(newPerson.getFirstName(), newPerson.getLastName(), person.getFirstName(), person.getLastName(), null)) {
                    entries.add(person);
                }
            }
            resultConsumer.accept((T) new CaseImportSimilarityResult(entries.get(0), null, ImportSimilarityResultOption.PICK));
        }

        @Override
        protected void handleCaseSimilarity(CaseImportSimilarityInput input, Consumer<CaseImportSimilarityResult> resultConsumer) {
            resultConsumer.accept(new CaseImportSimilarityResult(null, input.getSimilarCases().get(0), ImportSimilarityResultOption.PICK));
        }
    };
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(5, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-5", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Similarity: cancel
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            resultConsumer.accept((T) new CaseImportSimilarityResult(null, null, ImportSimilarityResultOption.CANCEL));
        }
    };
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.CANCELED, importResult);
    assertEquals(5, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-5", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Similarity: override
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            List<SimilarPersonDto> entries = new ArrayList<>();
            for (SimilarPersonDto person : persons) {
                if (PersonHelper.areNamesSimilar(newPerson.getFirstName(), newPerson.getLastName(), person.getFirstName(), person.getLastName(), null)) {
                    entries.add(person);
                }
            }
            resultConsumer.accept((T) new CaseImportSimilarityResult(entries.get(0), null, ImportSimilarityResultOption.PICK));
        }

        @Override
        protected void handleCaseSimilarity(CaseImportSimilarityInput input, Consumer<CaseImportSimilarityResult> resultConsumer) {
            resultConsumer.accept(new CaseImportSimilarityResult(null, input.getSimilarCases().get(0), ImportSimilarityResultOption.OVERRIDE));
        }
    };
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(5, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-10", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Similarity: create -> fail because of duplicate epid number
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user) {

        @Override
        protected <T extends PersonImportSimilarityResult> void handlePersonSimilarity(PersonDto newPerson, Consumer<T> resultConsumer, BiFunction<SimilarPersonDto, ImportSimilarityResultOption, T> createSimilarityResult, String infoText, UI currentUI) {
            List<SimilarPersonDto> entries = new ArrayList<>();
            for (SimilarPersonDto person : persons) {
                if (PersonHelper.areNamesSimilar(newPerson.getFirstName(), newPerson.getLastName(), person.getFirstName(), person.getLastName(), null)) {
                    entries.add(person);
                }
            }
            resultConsumer.accept((T) new CaseImportSimilarityResult(entries.get(0), null, ImportSimilarityResultOption.PICK));
        }

        @Override
        protected void handleCaseSimilarity(CaseImportSimilarityInput input, Consumer<CaseImportSimilarityResult> resultConsumer) {
            resultConsumer.accept(new CaseImportSimilarityResult(null, null, ImportSimilarityResultOption.CREATE));
        }
    };
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED_WITH_ERRORS, importResult);
    assertEquals(5, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-10", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Change epid number of the case in database to pass creation test
    CaseDataDto caze = getCaseFacade().getAllActiveCasesAfter(null).get(4);
    caze.setEpidNumber("ABC-DEF-GHI-19-99");
    getCaseFacade().save(caze);
    assertEquals("ABC-DEF-GHI-19-99", getCaseFacade().getAllActiveCasesAfter(null).get(4).getEpidNumber());
    // Similarity: create -> pass
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_similarities.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user);
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(6, getCaseFacade().count(null));
    assertEquals("ABC-DEF-GHI-19-10", getCaseFacade().getAllActiveCasesAfter(null).get(5).getEpidNumber());
    // Successful import of a case with different infrastructure combinations
    creator.createRDCF("R1", "D1", "C1", "F1");
    creator.createRDCF("R2", "D2", "C2", "F2");
    creator.createRDCF("R3", "D3", "C3", "F3");
    csvFile = new File(getClass().getClassLoader().getResource("sormas_case_import_test_different_infrastructure.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user);
    importResult = caseImporter.runImport();
    assertEquals(ImportResultStatus.COMPLETED, importResult);
    assertEquals(7, getCaseFacade().count(null));
    // Successful import of 5 cases from a commented CSV file
    csvFile = new File(getClass().getClassLoader().getResource("sormas_import_test_comment_success.csv").toURI());
    caseImporter = new CaseImporterExtension(csvFile, true, user);
    importResult = caseImporter.runImport();
    assertEquals(caseImporter.stringBuilder.toString(), ImportResultStatus.COMPLETED, importResult);
    assertEquals(12, getCaseFacade().count(null));
}
Also used : CaseImportSimilarityResult(de.symeda.sormas.ui.importer.CaseImportSimilarityResult) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) SimilarPersonDto(de.symeda.sormas.api.person.SimilarPersonDto) UserDto(de.symeda.sormas.api.user.UserDto) PersonSimilarityCriteria(de.symeda.sormas.api.person.PersonSimilarityCriteria) SimilarPersonDto(de.symeda.sormas.api.person.SimilarPersonDto) CaseImportSimilarityInput(de.symeda.sormas.ui.importer.CaseImportSimilarityInput) UI(com.vaadin.ui.UI) TestDataCreator(de.symeda.sormas.ui.TestDataCreator) ImportSimilarityResultOption(de.symeda.sormas.ui.importer.ImportSimilarityResultOption) ImportResultStatus(de.symeda.sormas.ui.importer.ImportResultStatus) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) AbstractBeanTest(de.symeda.sormas.ui.AbstractBeanTest) Test(org.junit.Test)

Example 4 with ImportSimilarityResultOption

use of de.symeda.sormas.ui.importer.ImportSimilarityResultOption in project SORMAS-Project by hzi-braunschweig.

the class EventParticipantImporter method importDataFromCsvLine.

@Override
protected ImportLineResult importDataFromCsvLine(String[] values, String[] entityClasses, String[] entityProperties, String[][] entityPropertyPaths, boolean firstLine) throws IOException, InterruptedException {
    // Check whether the new line has the same length as the header line
    if (values.length > entityProperties.length) {
        writeImportError(values, I18nProperties.getValidationError(Validations.importLineTooLong));
        return ImportLineResult.ERROR;
    }
    // regenerate the UUID to prevent overwrite in case of export and import of the same entities
    int uuidIndex = ArrayUtils.indexOf(entityProperties, EventParticipantDto.UUID);
    if (uuidIndex >= 0) {
        values[uuidIndex] = DataHelper.createUuid();
    }
    int personUuidIndex = ArrayUtils.indexOf(entityProperties, String.join(".", EventParticipantDto.PERSON, PersonDto.UUID));
    if (personUuidIndex >= 0) {
        values[personUuidIndex] = DataHelper.createUuid();
    }
    final PersonDto newPersonTemp = PersonDto.buildImportEntity();
    final EventParticipantDto newEventParticipantTemp = EventParticipantDto.build(event.toReference(), currentUser.toReference());
    newEventParticipantTemp.setPerson(newPersonTemp);
    final List<VaccinationDto> vaccinations = new ArrayList<>();
    ImportRelatedObjectsMapper.Builder relatedObjectsMapperBuilder = new ImportRelatedObjectsMapper.Builder();
    if (FacadeProvider.getFeatureConfigurationFacade().isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED) && event.getDisease() != null) {
        relatedObjectsMapperBuilder.addMapper(VaccinationDto.class, vaccinations, () -> VaccinationDto.build(currentUser.toReference()), this::insertColumnEntryIntoRelatedObject);
    }
    ImportRelatedObjectsMapper relatedMapper = relatedObjectsMapperBuilder.build();
    boolean eventParticipantHasImportError = insertRowIntoData(values, entityClasses, entityPropertyPaths, true, importColumnInformation -> {
        try {
            if (!relatedMapper.map(importColumnInformation)) {
                // If the cell entry is not empty, try to insert it into the current contact or person object
                if (!StringUtils.isEmpty(importColumnInformation.getValue())) {
                    insertColumnEntryIntoData(newEventParticipantTemp, newPersonTemp, importColumnInformation.getValue(), importColumnInformation.getEntityPropertyPath());
                }
            }
        } catch (ImportErrorException | InvalidColumnException e) {
            return e;
        }
        return null;
    });
    // If the row does not have any import errors, call the backend validation of all associated entities
    if (!eventParticipantHasImportError) {
        try {
            personFacade.validate(newPersonTemp);
            eventParticipantFacade.validate(newEventParticipantTemp);
        } catch (ValidationRuntimeException e) {
            eventParticipantHasImportError = true;
            writeImportError(values, e.getMessage());
        }
    }
    PersonDto newPerson = newPersonTemp;
    // Sanitize non-HOME address
    PersonHelper.sanitizeNonHomeAddress(newPerson);
    // if there are any, display a window to resolve the conflict to the user
    if (!eventParticipantHasImportError) {
        EventParticipantDto newEventParticipant = newEventParticipantTemp;
        try {
            EventParticipantImportConsumer consumer = new EventParticipantImportConsumer();
            ImportSimilarityResultOption resultOption = null;
            EventParticipantImportLock personSelectLock = new EventParticipantImportLock();
            // We need to pause the current thread to prevent the import from continuing until the user has acted
            synchronized (personSelectLock) {
                // Call the logic that allows the user to handle the similarity; once this has been done, the LOCK should be notified
                // to allow the importer to resume
                handlePersonSimilarity(newPerson, result -> consumer.onImportResult(result, personSelectLock), (person, similarityResultOption) -> new PersonImportSimilarityResult(person, similarityResultOption), Strings.infoSelectOrCreatePersonForImport, currentUI);
                try {
                    if (!personSelectLock.wasNotified) {
                        personSelectLock.wait();
                    }
                } catch (InterruptedException e) {
                    logger.error("InterruptedException when trying to perform LOCK.wait() in eventparticipant import: " + e.getMessage());
                    throw e;
                }
                if (consumer.result != null) {
                    resultOption = consumer.result.getResultOption();
                }
                // If the user picked an existing person, override the eventparticipant person with it
                if (ImportSimilarityResultOption.PICK.equals(resultOption)) {
                    newPerson = personFacade.getPersonByUuid(consumer.result.getMatchingPerson().getUuid());
                    // get first eventparticipant for event and person
                    EventParticipantCriteria eventParticipantCriteria = new EventParticipantCriteria().withPerson(newPerson.toReference()).withEvent(event.toReference());
                    EventParticipantDto pickedEventParticipant = eventParticipantFacade.getFirst(eventParticipantCriteria);
                    if (pickedEventParticipant != null) {
                        // re-apply import on pickedEventParticipant
                        insertRowIntoData(values, entityClasses, entityPropertyPaths, true, importColumnInformation -> {
                            // If the cell entry is not empty, try to insert it into the current contact or person object
                            if (!StringUtils.isEmpty(importColumnInformation.getValue())) {
                                try {
                                    insertColumnEntryIntoData(pickedEventParticipant, newPersonTemp, importColumnInformation.getValue(), importColumnInformation.getEntityPropertyPath());
                                } catch (ImportErrorException | InvalidColumnException e) {
                                    return e;
                                }
                            }
                            return null;
                        });
                        newEventParticipant = pickedEventParticipant;
                    }
                }
            }
            // or an existing person was picked, save the eventparticipant and person to the database
            if (ImportSimilarityResultOption.SKIP.equals(resultOption)) {
                return ImportLineResult.SKIPPED;
            } else {
                // Workaround: Reset the change date to avoid OutdatedEntityExceptions
                newPerson.setChangeDate(new Date());
                boolean skipPersonValidation = ImportSimilarityResultOption.PICK.equals(resultOption);
                final PersonDto savedPerson = personFacade.savePerson(newPerson, skipPersonValidation);
                newEventParticipant.setPerson(savedPerson);
                newEventParticipant.setChangeDate(new Date());
                eventParticipantFacade.saveEventParticipant(newEventParticipant);
                for (VaccinationDto vaccination : vaccinations) {
                    FacadeProvider.getVaccinationFacade().createWithImmunization(vaccination, newEventParticipant.getRegion(), newEventParticipant.getDistrict(), newEventParticipant.getPerson().toReference(), event.getDisease());
                }
                consumer.result = null;
                return ImportLineResult.SUCCESS;
            }
        } catch (ValidationRuntimeException e) {
            writeImportError(values, e.getMessage());
            return ImportLineResult.ERROR;
        }
    } else {
        return ImportLineResult.ERROR;
    }
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) VaccinationDto(de.symeda.sormas.api.vaccination.VaccinationDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) Date(java.util.Date) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ImportSimilarityResultOption(de.symeda.sormas.ui.importer.ImportSimilarityResultOption) EventParticipantCriteria(de.symeda.sormas.api.event.EventParticipantCriteria) ImportRelatedObjectsMapper(de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper) PersonImportSimilarityResult(de.symeda.sormas.ui.importer.PersonImportSimilarityResult)

Aggregations

PersonDto (de.symeda.sormas.api.person.PersonDto)4 ImportSimilarityResultOption (de.symeda.sormas.ui.importer.ImportSimilarityResultOption)4 ArrayList (java.util.ArrayList)4 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)3 Date (java.util.Date)3 UI (com.vaadin.ui.UI)2 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)2 ImportRelatedObjectsMapper (de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper)2 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)2 PersonSimilarityCriteria (de.symeda.sormas.api.person.PersonSimilarityCriteria)2 SimilarPersonDto (de.symeda.sormas.api.person.SimilarPersonDto)2 UserDto (de.symeda.sormas.api.user.UserDto)2 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)2 VaccinationDto (de.symeda.sormas.api.vaccination.VaccinationDto)2 AbstractBeanTest (de.symeda.sormas.ui.AbstractBeanTest)2 ContactImportSimilarityResult (de.symeda.sormas.ui.importer.ContactImportSimilarityResult)2 ImportResultStatus (de.symeda.sormas.ui.importer.ImportResultStatus)2 File (java.io.File)2 List (java.util.List)2 Test (org.junit.Test)2