Search in sources :

Example 6 with InvalidColumnException

use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.

the class TravelEntryImportFacadeEjb method insertRowIntoData.

private ImportLineResultDto<TravelEntryImportEntities> insertRowIntoData(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, Function<ImportCellData, Exception> insertCallback) {
    String importError = null;
    List<String> invalidColumns = new ArrayList<>();
    for (int i = 0; i < values.length; i++) {
        String value = StringUtils.trimToNull(values[i]);
        if (ignoreEmptyEntries && StringUtils.isBlank(value)) {
            continue;
        }
        String[] entityPropertyPath = entityPropertyPaths[i];
        // Error description column is ignored
        if (entityPropertyPath[0].equals(ERROR_COLUMN_NAME)) {
            continue;
        }
        if (!(ignoreEmptyEntries && StringUtils.isEmpty(value))) {
            Exception exception = insertCallback.apply(new ImportCellData(value, entityClasses != null ? entityClasses[i] : null, entityPropertyPath));
            if (exception != null) {
                if (exception instanceof ImportErrorException) {
                    importError = exception.getMessage();
                    StringBuilder additionalInfo = new StringBuilder();
                    for (String s : entityPropertyPath) {
                        additionalInfo.append(" ").append(s);
                    }
                    importError += additionalInfo;
                    importError += "value:" + value;
                    break;
                } else if (exception instanceof InvalidColumnException) {
                    invalidColumns.add(((InvalidColumnException) exception).getColumnName());
                }
            }
        }
    }
    if (invalidColumns.size() > 0) {
        LOGGER.warn("Unhandled columns [{}]", String.join(", ", invalidColumns));
    }
    return importError != null ? ImportLineResultDto.errorResult(importError) : ImportLineResultDto.successResult();
}
Also used : ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ArrayList(java.util.ArrayList) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ParseException(java.text.ParseException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException)

Example 7 with InvalidColumnException

use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.

the class TravelEntryImportFacadeEjb method buildEntities.

private ImportLineResultDto<TravelEntryImportEntities> buildEntities(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, TravelEntryImportEntities entities) {
    ImportLineResultDto<TravelEntryImportEntities> importResult = insertRowIntoData(values, entityClasses, entityPropertyPaths, ignoreEmptyEntries, cellData -> {
        try {
            TravelEntryDto travelEntry = entities.getTravelEntry();
            if (StringUtils.isNotEmpty(cellData.getValue())) {
                // If the cell entry is not empty, try to insert it into the current travel entry or its person
                insertColumnEntryIntoData(travelEntry, entities.getPerson(), cellData.getValue(), cellData.getEntityPropertyPath());
            }
        } catch (ImportErrorException | InvalidColumnException e) {
            return e;
        }
        return null;
    });
    if (!importResult.isError()) {
        TravelEntryDto travelEntry = entities.getTravelEntry();
        if (travelEntry.getPointOfEntry() == null && configFacade.isConfiguredCountry(CountryHelper.COUNTRY_CODE_GERMANY)) {
            travelEntry.setPointOfEntry(pointOfEntryFacade.getByUuid(PointOfEntryDto.OTHER_POE_UUID).toReference());
            travelEntry.setPointOfEntryDetails(I18nProperties.getString(Strings.messageTravelEntryPOEFilledBySystem));
        }
    }
    return importResult;
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) TravelEntryDto(de.symeda.sormas.api.travelentry.TravelEntryDto) TravelEntryImportEntities(de.symeda.sormas.api.travelentry.travelentryimport.TravelEntryImportEntities)

Example 8 with InvalidColumnException

use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.

the class PopulationDataImporter method insertCellValueIntoData.

/**
 * Inserts the entry of a single cell into the population data object. Checks whether the entity property accords to one of the patterns
 * defined in this class
 * and sets the according sex and age group to the population data object.
 */
private void insertCellValueIntoData(PopulationDataDto populationData, String value, String[] entityPropertyPaths) throws InvalidColumnException, ImportErrorException {
    String entityProperty = buildEntityProperty(entityPropertyPaths);
    if (entityPropertyPaths.length != 1) {
        throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, buildEntityProperty(entityPropertyPaths)));
    }
    String entityPropertyPath = entityPropertyPaths[0];
    try {
        if (entityPropertyPath.equalsIgnoreCase("TOTAL")) {
            insertPopulationIntoPopulationData(populationData, value);
        } else if (entityPropertyPath.matches(TOTAL_HEADER_PATTERN)) {
            try {
                populationData.setSex(Sex.valueOf(entityPropertyPaths[0].substring(0, entityPropertyPaths[0].indexOf("_"))));
            } catch (IllegalArgumentException e) {
                throw new InvalidColumnException(entityProperty);
            }
            insertPopulationIntoPopulationData(populationData, value);
        } else if (entityPropertyPath.matches(HEADER_PATTERN)) {
            // Sex
            String sexString = entityPropertyPath.substring(0, entityPropertyPaths[0].indexOf("_"));
            if (!sexString.equals("TOTAL")) {
                try {
                    populationData.setSex(Sex.valueOf(sexString));
                } catch (IllegalArgumentException e) {
                    throw new InvalidColumnException(entityProperty);
                }
            }
            // Age group
            String ageGroupString = entityPropertyPath.substring(entityPropertyPath.indexOf("_") + 1, entityPropertyPaths[0].length());
            try {
                populationData.setAgeGroup(AgeGroup.valueOf(ageGroupString));
            } catch (IllegalArgumentException e) {
                throw new InvalidColumnException(entityProperty);
            }
            insertPopulationIntoPopulationData(populationData, value);
        } else {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, entityPropertyPath));
        }
    } catch (IllegalArgumentException e) {
        throw new ImportErrorException(value, entityProperty);
    } catch (ImportErrorException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Unexpected error when trying to import population data: " + e.getMessage());
        throw new ImportErrorException(I18nProperties.getValidationError(Validations.importUnexpectedError));
    }
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IOException(java.io.IOException)

Example 9 with InvalidColumnException

use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.

the class EventImportFacadeEjb method insertRowIntoData.

protected ImportLineResultDto<EventImportEntities> insertRowIntoData(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, Function<ImportCellData, Exception> insertCallback) {
    String importError = null;
    List<String> invalidColumns = new ArrayList<>();
    for (int i = 0; i < values.length; i++) {
        String value = StringUtils.trimToNull(values[i]);
        if (ignoreEmptyEntries && (value == null || value.isEmpty())) {
            continue;
        }
        String[] entityPropertyPath = entityPropertyPaths[i];
        // Error description column is ignored
        if (entityPropertyPath[0].equals(ERROR_COLUMN_NAME)) {
            continue;
        }
        if (!(ignoreEmptyEntries && StringUtils.isEmpty(value))) {
            Exception exception = insertCallback.apply(new ImportCellData(value, entityClasses != null ? entityClasses[i] : null, entityPropertyPath));
            if (exception != null) {
                if (exception instanceof ImportErrorException) {
                    importError = exception.getMessage();
                    break;
                } else if (exception instanceof InvalidColumnException) {
                    invalidColumns.add(((InvalidColumnException) exception).getColumnName());
                }
            }
        }
    }
    if (invalidColumns.size() > 0) {
        LOGGER.warn("Unhandled columns [{}]", String.join(", ", invalidColumns));
    }
    return importError != null ? ImportLineResultDto.errorResult(importError) : ImportLineResultDto.successResult();
}
Also used : ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ArrayList(java.util.ArrayList) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ParseException(java.text.ParseException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException)

Example 10 with InvalidColumnException

use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.

the class EventImportFacadeEjb method buildEntities.

private ImportLineResultDto<EventImportEntities> buildEntities(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, EventImportEntities entities) {
    final UserReferenceDto currentUserRef = userService.getCurrentUser().toReference();
    final List<EventParticipantDto> eventParticipants = entities.getEventParticipants();
    final List<EventGroupReferenceDto> eventGroupReferences = entities.getEventGroupReferences();
    final MutableBoolean currentEventParticipantHasEntries = new MutableBoolean(false);
    final Mutable<String> firstEventParticipantColumnName = new MutableObject<>(null);
    final ImportLineResultDto<EventImportEntities> result = insertRowIntoData(values, entityClasses, entityPropertyPaths, ignoreEmptyEntries, (cellData) -> {
        try {
            // participant if they don't have any entries
            if (String.join(".", cellData.getEntityPropertyPath()).equals(firstEventParticipantColumnName.getValue())) {
                if (eventParticipants.size() > 0 && currentEventParticipantHasEntries.isFalse()) {
                    eventParticipants.remove(eventParticipants.size() - 1);
                    currentEventParticipantHasEntries.setTrue();
                }
            }
            EventDto event = entities.getEvent();
            if (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(EventParticipantDto.class)) || DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(PersonDto.class)) || (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(LocationDto.class)) && eventParticipants.size() > 0)) {
                // to the list if the first column of a new participant has been reached and insert the entry of the cell into the participant
                if (firstEventParticipantColumnName.getValue() == null) {
                    firstEventParticipantColumnName.setValue(String.join(".", cellData.getEntityPropertyPath()));
                }
                if (String.join(".", cellData.getEntityPropertyPath()).equals(firstEventParticipantColumnName.getValue())) {
                    currentEventParticipantHasEntries.setFalse();
                    EventParticipantDto eventParticipantDto = EventParticipantDto.build(new EventReferenceDto(event.getUuid()), currentUserRef);
                    eventParticipantDto.setPerson(PersonDto.buildImportEntity());
                    eventParticipants.add(eventParticipantDto);
                }
                if (!StringUtils.isEmpty(cellData.getValue())) {
                    currentEventParticipantHasEntries.setTrue();
                    insertColumnEntryIntoEventParticipantData(eventParticipants.get(eventParticipants.size() - 1), cellData.getValue(), cellData.getEntityPropertyPath());
                }
            } else if (DataHelper.equal(cellData.getEntityClass(), DataHelper.getHumanClassName(EventGroupReferenceDto.class))) {
                eventGroupReferences.add(new EventGroupReferenceDto(cellData.getValue()));
            } else if (!StringUtils.isEmpty(cellData.getValue())) {
                // If the cell entry is not empty, try to insert it into the current event
                insertColumnEntryIntoData(event, cellData.getValue(), cellData.getEntityPropertyPath());
            }
        } catch (ImportErrorException | InvalidColumnException e) {
            return e;
        }
        return null;
    });
    // Remove the eventParticipant if empty
    if (eventParticipants.size() > 0 && currentEventParticipantHasEntries.isFalse()) {
        eventParticipants.remove(eventParticipants.size() - 1);
    }
    return result;
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) PersonDto(de.symeda.sormas.api.person.PersonDto) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) EventGroupReferenceDto(de.symeda.sormas.api.event.EventGroupReferenceDto) EventDto(de.symeda.sormas.api.event.EventDto) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) UserReferenceDto(de.symeda.sormas.api.user.UserReferenceDto) EventReferenceDto(de.symeda.sormas.api.event.EventReferenceDto) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) EventImportEntities(de.symeda.sormas.api.event.eventimport.EventImportEntities) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)27 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)26 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)20 IntrospectionException (java.beans.IntrospectionException)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 ArrayList (java.util.ArrayList)15 PropertyDescriptor (java.beans.PropertyDescriptor)11 IOException (java.io.IOException)11 ParseException (java.text.ParseException)11 List (java.util.List)11 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)9 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)9 CsvValidationException (com.opencsv.exceptions.CsvValidationException)8 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)6 ImportCellData (de.symeda.sormas.api.importexport.ImportCellData)5 PersonDto (de.symeda.sormas.api.person.PersonDto)5 EventParticipantDto (de.symeda.sormas.api.event.EventParticipantDto)4 DataHelper (de.symeda.sormas.api.utils.DataHelper)4 Optional (java.util.Optional)4 FacadeProvider (de.symeda.sormas.api.FacadeProvider)3