Search in sources :

Example 1 with ImportCellData

use of de.symeda.sormas.api.importexport.ImportCellData 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 2 with ImportCellData

use of de.symeda.sormas.api.importexport.ImportCellData 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 3 with ImportCellData

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

the class CaseImportFacadeEjb method insertRowIntoData.

protected ImportLineResultDto<CaseImportEntities> 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();
                    StringBuilder additionalInfo = new StringBuilder();
                    for (int j = 0; j < entityPropertyPath.length; j++) {
                        additionalInfo.append(" ").append(entityPropertyPath[j]);
                    }
                    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 4 with ImportCellData

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

the class DataImporter method insertRowIntoData.

/**
 * Provides the structure to insert a whole line into the object entity. The actual inserting has to take
 * place in a callback.
 *
 * @param ignoreEmptyEntries
 *            If true, invokes won't be performed for empty values
 * @param insertCallback
 *            The callback that is used to actually do the inserting
 *
 * @return True if the import succeeded without errors, false if not
 */
protected boolean insertRowIntoData(String[] values, String[] entityClasses, String[][] entityPropertyPaths, boolean ignoreEmptyEntries, Function<ImportCellData, Exception> insertCallback) throws IOException {
    boolean dataHasImportError = false;
    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, hasEntityClassRow ? entityClasses[i] : null, entityPropertyPath));
            if (exception != null) {
                if (exception instanceof ImportErrorException) {
                    dataHasImportError = true;
                    writeImportError(values, exception.getMessage());
                    break;
                } else if (exception instanceof InvalidColumnException) {
                    invalidColumns.add(((InvalidColumnException) exception).getColumnName());
                }
            }
        }
    }
    if (invalidColumns.size() > 0) {
        LoggerFactory.getLogger(getClass()).warn("Unhandled columns [{}]", String.join(", ", invalidColumns));
    }
    return dataHasImportError;
}
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) CsvValidationException(com.opencsv.exceptions.CsvValidationException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) FileNotFoundException(java.io.FileNotFoundException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 5 with ImportCellData

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

the class PopulationDataImporter method importDataFromCsvLine.

@Override
protected ImportLineResult importDataFromCsvLine(String[] values, String[] entityClasses, String[] entityProperties, String[][] entityPropertyPaths, boolean firstLine) throws IOException, InvalidColumnException, 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;
    }
    // Reference population data that contains the region, district and community for this line
    RegionReferenceDto region = null;
    DistrictReferenceDto district = null;
    CommunityReferenceDto community = null;
    // Retrieve the region and district from the database or throw an error if more or less than one entry have been retrieved
    for (int i = 0; i < entityProperties.length; i++) {
        if (PopulationDataDto.REGION.equalsIgnoreCase(entityProperties[i])) {
            List<RegionReferenceDto> regions = FacadeProvider.getRegionFacade().getReferencesByName(values[i], false);
            if (regions.size() != 1) {
                writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
                return ImportLineResult.ERROR;
            }
            region = regions.get(0);
        }
        if (PopulationDataDto.DISTRICT.equalsIgnoreCase(entityProperties[i])) {
            if (DataHelper.isNullOrEmpty(values[i])) {
                district = null;
            } else {
                List<DistrictReferenceDto> districts = FacadeProvider.getDistrictFacade().getByName(values[i], region, false);
                if (districts.size() != 1) {
                    writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
                    return ImportLineResult.ERROR;
                }
                district = districts.get(0);
            }
        }
        if (PopulationDataDto.COMMUNITY.equalsIgnoreCase(entityProperties[i])) {
            if (DataHelper.isNullOrEmpty(values[i])) {
                community = null;
            } else {
                List<CommunityReferenceDto> communities = FacadeProvider.getCommunityFacade().getByName(values[i], district, false);
                if (communities.size() != 1) {
                    writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
                    return ImportLineResult.ERROR;
                }
                community = communities.get(0);
            }
        }
    }
    // The region and district that will be used to save the population data to the database
    final RegionReferenceDto finalRegion = region;
    final DistrictReferenceDto finalDistrict = district;
    final CommunityReferenceDto finalCommunity = community;
    // Retrieve the existing population data for the region and district
    PopulationDataCriteria criteria = new PopulationDataCriteria().region(finalRegion);
    if (finalCommunity == null) {
        criteria.communityIsNull(true);
    } else {
        criteria.community(finalCommunity);
    }
    if (district == null) {
        criteria.districtIsNull(true);
    } else {
        criteria.district(finalDistrict);
    }
    List<PopulationDataDto> existingPopulationDataList = FacadeProvider.getPopulationDataFacade().getPopulationData(criteria);
    List<PopulationDataDto> modifiedPopulationDataList = new ArrayList<PopulationDataDto>();
    boolean populationDataHasImportError = insertRowIntoData(values, entityClasses, entityPropertyPaths, false, new Function<ImportCellData, Exception>() {

        @Override
        public Exception apply(ImportCellData cellData) {
            try {
                if (PopulationDataDto.REGION.equalsIgnoreCase(cellData.getEntityPropertyPath()[0]) || PopulationDataDto.DISTRICT.equalsIgnoreCase(cellData.getEntityPropertyPath()[0]) || PopulationDataDto.COMMUNITY.equalsIgnoreCase(cellData.getEntityPropertyPath()[0])) {
                // Ignore the region, district and community columns
                } else if (RegionDto.GROWTH_RATE.equalsIgnoreCase(cellData.getEntityPropertyPath()[0])) {
                    // Update the growth rate of the region or district
                    if (!DataHelper.isNullOrEmpty(cellData.getValue())) {
                        Float growthRate = Float.parseFloat(cellData.getValue());
                        if (finalCommunity != null) {
                            CommunityDto communityDto = FacadeProvider.getCommunityFacade().getByUuid(finalCommunity.getUuid());
                            communityDto.setGrowthRate(growthRate);
                            FacadeProvider.getCommunityFacade().save(communityDto);
                        } else if (finalDistrict != null) {
                            DistrictDto districtDto = FacadeProvider.getDistrictFacade().getByUuid(finalDistrict.getUuid());
                            districtDto.setGrowthRate(growthRate);
                            FacadeProvider.getDistrictFacade().save(districtDto);
                        } else {
                            RegionDto regionDto = FacadeProvider.getRegionFacade().getByUuid(finalRegion.getUuid());
                            regionDto.setGrowthRate(growthRate);
                            FacadeProvider.getRegionFacade().save(regionDto);
                        }
                    }
                } else {
                    // Add the data from the currently processed cell to a new population data object
                    PopulationDataDto newPopulationData = PopulationDataDto.build(collectionDate);
                    insertCellValueIntoData(newPopulationData, cellData.getValue(), cellData.getEntityPropertyPath());
                    Optional<PopulationDataDto> existingPopulationData = existingPopulationDataList.stream().filter(populationData -> populationData.getAgeGroup() == newPopulationData.getAgeGroup() && populationData.getSex() == newPopulationData.getSex()).findFirst();
                    // Check whether this population data set already exists in the database; if yes, override it
                    if (existingPopulationData.isPresent()) {
                        existingPopulationData.get().setPopulation(newPopulationData.getPopulation());
                        existingPopulationData.get().setCollectionDate(collectionDate);
                        modifiedPopulationDataList.add(existingPopulationData.get());
                    } else {
                        newPopulationData.setRegion(finalRegion);
                        newPopulationData.setDistrict(finalDistrict);
                        newPopulationData.setCommunity(finalCommunity);
                        modifiedPopulationDataList.add(newPopulationData);
                    }
                }
            } catch (ImportErrorException | InvalidColumnException | NumberFormatException e) {
                return e;
            }
            return null;
        }
    });
    // Validate and save the population data object into the database if the import has no errors
    if (!populationDataHasImportError) {
        try {
            FacadeProvider.getPopulationDataFacade().savePopulationData(modifiedPopulationDataList);
            return ImportLineResult.SUCCESS;
        } catch (ValidationRuntimeException e) {
            writeImportError(values, e.getMessage());
            return ImportLineResult.ERROR;
        }
    } else {
        return ImportLineResult.ERROR;
    }
}
Also used : PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) Date(java.util.Date) FacadeProvider(de.symeda.sormas.api.FacadeProvider) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) ValueSeparator(de.symeda.sormas.api.importexport.ValueSeparator) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) Function(java.util.function.Function) AgeGroup(de.symeda.sormas.api.AgeGroup) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ArrayList(java.util.ArrayList) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) Validations(de.symeda.sormas.api.i18n.Validations) DataHelper(de.symeda.sormas.api.utils.DataHelper) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) UserDto(de.symeda.sormas.api.user.UserDto) Sex(de.symeda.sormas.api.person.Sex) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IOException(java.io.IOException) ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) File(java.io.File) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) List(java.util.List) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) Optional(java.util.Optional) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) ArrayList(java.util.ArrayList) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) 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) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto)

Aggregations

ImportCellData (de.symeda.sormas.api.importexport.ImportCellData)5 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)5 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)5 ArrayList (java.util.ArrayList)5 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)4 IntrospectionException (java.beans.IntrospectionException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ParseException (java.text.ParseException)4 IOException (java.io.IOException)2 CsvValidationException (com.opencsv.exceptions.CsvValidationException)1 AgeGroup (de.symeda.sormas.api.AgeGroup)1 FacadeProvider (de.symeda.sormas.api.FacadeProvider)1 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)1 Validations (de.symeda.sormas.api.i18n.Validations)1 ValueSeparator (de.symeda.sormas.api.importexport.ValueSeparator)1 PopulationDataCriteria (de.symeda.sormas.api.infrastructure.PopulationDataCriteria)1 PopulationDataDto (de.symeda.sormas.api.infrastructure.PopulationDataDto)1 CommunityDto (de.symeda.sormas.api.infrastructure.community.CommunityDto)1 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)1 DistrictDto (de.symeda.sormas.api.infrastructure.district.DistrictDto)1