use of de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper 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;
}
}
use of de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper in project SORMAS-Project by hzi-braunschweig.
the class CaseImportFacadeEjb method buildEntities.
private ImportLineResultDto<CaseImportEntities> buildEntities(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, CaseImportEntities entities) {
final UserReferenceDto currentUserRef = userService.getCurrentUser().toReference();
final List<SampleDto> samples = entities.getSamples();
final List<PathogenTestDto> pathogenTests = entities.getPathogenTests();
final List<VaccinationDto> vaccinations = entities.getVaccinations();
ImportRelatedObjectsMapper.Builder relatedObjectsMapperBuilder = new ImportRelatedObjectsMapper.Builder().addMapper(SampleDto.class, samples, () -> SampleDto.build(currentUserRef, new CaseReferenceDto(entities.getCaze().getUuid())), this::insertColumnEntryIntoRelatedObject).addMapper(PathogenTestDto.class, pathogenTests, () -> {
if (samples.isEmpty()) {
return null;
}
SampleDto referenceSample = samples.get(samples.size() - 1);
return PathogenTestDto.build(new SampleReferenceDto(referenceSample.getUuid()), currentUserRef);
}, this::insertColumnEntryIntoRelatedObject);
if (featureConfigurationFacade.isPropertyValueTrue(FeatureType.IMMUNIZATION_MANAGEMENT, FeatureTypeProperty.REDUCED)) {
relatedObjectsMapperBuilder.addMapper(VaccinationDto.class, vaccinations, () -> VaccinationDto.build(currentUserRef), this::insertColumnEntryIntoRelatedObject);
}
ImportRelatedObjectsMapper relatedMapper = relatedObjectsMapperBuilder.build();
final ImportLineResultDto<CaseImportEntities> result = insertRowIntoData(values, entityClasses, entityPropertyPaths, ignoreEmptyEntries, (cellData) -> {
try {
CaseDataDto caze = entities.getCaze();
if (!relatedMapper.map(cellData)) {
if (StringUtils.isNotEmpty(cellData.getValue())) {
// If the cell entry is not empty, try to insert it into the current case or its person
insertColumnEntryIntoData(caze, entities.getPerson(), cellData.getValue(), cellData.getEntityPropertyPath());
}
}
} catch (ImportErrorException | InvalidColumnException e) {
return e;
}
return null;
});
// Sanitize non-HOME address
PersonHelper.sanitizeNonHomeAddress(entities.getPerson());
return result;
}
use of de.symeda.sormas.api.importexport.ImportRelatedObjectsMapper 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;
}
}
Aggregations