use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.
the class ContactImporter method insertColumnEntryIntoData.
/**
* Inserts the entry of a single cell into the contact or its person.
*/
private void insertColumnEntryIntoData(ContactDto contact, PersonDto person, String entry, String[] entryHeaderPath) throws InvalidColumnException, ImportErrorException {
Object currentElement = contact;
for (int i = 0; i < entryHeaderPath.length; i++) {
String headerPathElementName = entryHeaderPath[i];
try {
if (i != entryHeaderPath.length - 1) {
currentElement = new PropertyDescriptor(headerPathElementName, currentElement.getClass()).getReadMethod().invoke(currentElement);
// Set the current element to the created person
if (currentElement instanceof PersonReferenceDto) {
currentElement = person;
}
} else if (ContactExportDto.BIRTH_DATE.equals(headerPathElementName)) {
BirthDateDto birthDateDto = PersonHelper.parseBirthdate(entry, currentUser.getLanguage());
if (birthDateDto != null) {
person.setBirthdateDD(birthDateDto.getDateOfBirthDD());
person.setBirthdateMM(birthDateDto.getDateOfBirthMM());
person.setBirthdateYYYY(birthDateDto.getDateOfBirthYYYY());
}
} else {
PropertyDescriptor pd = new PropertyDescriptor(headerPathElementName, currentElement.getClass());
Class<?> propertyType = pd.getPropertyType();
// according to the types of the contact or person fields
if (executeDefaultInvoke(pd, currentElement, entry, entryHeaderPath)) {
continue;
} else if (propertyType.isAssignableFrom(DistrictReferenceDto.class)) {
List<DistrictReferenceDto> district = FacadeProvider.getDistrictFacade().getByName(entry, ImporterPersonHelper.getRegionBasedOnDistrict(pd.getName(), contact, person, currentElement), false);
if (district.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrRegion, entry, buildEntityProperty(entryHeaderPath)));
} else if (district.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importDistrictNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(currentElement, district.get(0));
}
} else if (propertyType.isAssignableFrom(CommunityReferenceDto.class)) {
DistrictReferenceDto district = currentElement instanceof ContactDto ? ((ContactDto) currentElement).getDistrict() : (currentElement instanceof LocationDto ? ((LocationDto) currentElement).getDistrict() : null);
List<CommunityReferenceDto> community = FacadeProvider.getCommunityFacade().getByName(entry, district != null ? district : ImporterPersonHelper.getPersonDistrict(pd.getName(), person), false);
if (community.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrDistrict, entry, buildEntityProperty(entryHeaderPath)));
} else if (community.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importCommunityNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(currentElement, community.get(0));
}
} else if (propertyType.isAssignableFrom(FacilityReferenceDto.class)) {
Pair<DistrictReferenceDto, CommunityReferenceDto> infrastructureData = ImporterPersonHelper.getPersonDistrictAndCommunity(pd.getName(), person);
List<FacilityReferenceDto> facility = FacadeProvider.getFacilityFacade().getByNameAndType(entry, infrastructureData.getElement0(), infrastructureData.getElement1(), getTypeOfFacility(pd.getName(), currentElement), false);
if (facility.isEmpty()) {
if (infrastructureData.getElement1() != null) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrCommunity, entry, buildEntityProperty(entryHeaderPath)));
} else {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrDistrict, entry, buildEntityProperty(entryHeaderPath)));
}
} else if (facility.size() > 1 && infrastructureData.getElement1() == null) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importFacilityNotUniqueInDistrict, entry, buildEntityProperty(entryHeaderPath)));
} else if (facility.size() > 1 && infrastructureData.getElement1() != null) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importFacilityNotUniqueInCommunity, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(currentElement, facility.get(0));
}
} else {
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
}
}
} catch (IntrospectionException e) {
throw new InvalidColumnException(buildEntityProperty(entryHeaderPath));
} catch (InvocationTargetException | IllegalAccessException e) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importErrorInColumn, buildEntityProperty(entryHeaderPath)));
} catch (IllegalArgumentException e) {
throw new ImportErrorException(entry, buildEntityProperty(entryHeaderPath));
} catch (ImportErrorException e) {
throw e;
} catch (Exception e) {
logger.error("Unexpected error when trying to import a contact: " + e.getMessage());
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importUnexpectedError));
}
}
ImportLineResultDto<ContactDto> contactErrors = validateConstraints(contact);
if (contactErrors.isError()) {
throw new ImportErrorException(contactErrors.getMessage());
}
ImportLineResultDto<PersonDto> personErrors = validateConstraints(person);
if (personErrors.isError()) {
throw new ImportErrorException(personErrors.getMessage());
}
}
use of de.symeda.sormas.api.importexport.InvalidColumnException 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;
}
use of de.symeda.sormas.api.importexport.InvalidColumnException 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;
}
}
use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.
the class InfrastructureImporter method insertColumnEntryIntoData.
/**
* Inserts the entry of a single cell into the infrastructure object.
*/
private void insertColumnEntryIntoData(EntityDto newEntityDto, String value, String[] entityPropertyPath) throws InvalidColumnException, ImportErrorException {
Object currentElement = newEntityDto;
for (int i = 0; i < entityPropertyPath.length; i++) {
String headerPathElementName = entityPropertyPath[i];
try {
if (i != entityPropertyPath.length - 1) {
currentElement = new PropertyDescriptor(headerPathElementName, currentElement.getClass()).getReadMethod().invoke(currentElement);
} else {
PropertyDescriptor pd = new PropertyDescriptor(headerPathElementName, currentElement.getClass());
Class<?> propertyType = pd.getPropertyType();
// is referenced in the imported object does not exist in the database
if (!executeDefaultInvoke(pd, currentElement, value, entityPropertyPath)) {
if (propertyType.isAssignableFrom(DistrictReferenceDto.class)) {
List<DistrictReferenceDto> district;
switch(type) {
case COMMUNITY:
district = FacadeProvider.getDistrictFacade().getByName(value, ((CommunityDto) newEntityDto).getRegion(), false);
break;
case FACILITY:
district = FacadeProvider.getDistrictFacade().getByName(value, ((FacilityDto) newEntityDto).getRegion(), false);
break;
case POINT_OF_ENTRY:
district = FacadeProvider.getDistrictFacade().getByName(value, ((PointOfEntryDto) newEntityDto).getRegion(), false);
break;
default:
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
}
if (district.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrRegion, value, buildEntityProperty(entityPropertyPath)));
} else if (district.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importDistrictNotUnique, value, buildEntityProperty(entityPropertyPath)));
} else {
pd.getWriteMethod().invoke(currentElement, district.get(0));
}
} else if (propertyType.isAssignableFrom(CommunityReferenceDto.class)) {
List<CommunityReferenceDto> community;
if (type == InfrastructureType.FACILITY) {
community = FacadeProvider.getCommunityFacade().getByName(value, ((FacilityDto) newEntityDto).getDistrict(), false);
} else {
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
}
if (community.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrRegion, value, buildEntityProperty(entityPropertyPath)));
} else if (community.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importDistrictNotUnique, value, buildEntityProperty(entityPropertyPath)));
} else {
pd.getWriteMethod().invoke(currentElement, community.get(0));
}
} else {
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
}
}
}
} catch (IntrospectionException e) {
throw new InvalidColumnException(buildEntityProperty(entityPropertyPath));
} catch (InvocationTargetException | IllegalAccessException e) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importErrorInColumn, buildEntityProperty(entityPropertyPath)));
} catch (IllegalArgumentException e) {
throw new ImportErrorException(value, buildEntityProperty(entityPropertyPath));
} catch (ImportErrorException e) {
throw e;
} catch (Exception e) {
logger.error("Unexpected error when trying to import infrastructure data: " + e.getMessage());
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importUnexpectedError));
}
}
ImportLineResultDto<EntityDto> constraintErrors = validateConstraints(newEntityDto);
if (constraintErrors.isError()) {
throw new ImportErrorException(constraintErrors.getMessage());
}
}
use of de.symeda.sormas.api.importexport.InvalidColumnException in project SORMAS-Project by hzi-braunschweig.
the class InfrastructureImporter method importDataFromCsvLine.
@Override
protected ImportLineResult importDataFromCsvLine(String[] values, String[] entityClasses, String[] entityProperties, String[][] entityPropertyPaths, boolean firstLine) throws IOException, InvalidColumnException {
// 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;
}
EntityDto newEntityDto;
switch(type) {
case COMMUNITY:
newEntityDto = CommunityDto.build();
break;
case DISTRICT:
newEntityDto = DistrictDto.build();
break;
case FACILITY:
newEntityDto = FacilityDto.build();
break;
case POINT_OF_ENTRY:
newEntityDto = PointOfEntryDto.build();
break;
case REGION:
newEntityDto = RegionDto.build();
break;
case AREA:
newEntityDto = AreaDto.build();
break;
case SUBCONTINENT:
newEntityDto = SubcontinentDto.build();
break;
case CONTINENT:
newEntityDto = ContinentDto.build();
break;
default:
throw new IllegalArgumentException(type.toString());
}
boolean iHasImportError = insertRowIntoData(values, entityClasses, entityPropertyPaths, false, (cellData) -> {
try {
// If the cell entry is not empty, try to insert it into the current infrastructure object
if (!StringUtils.isEmpty(cellData.getValue())) {
insertColumnEntryIntoData(newEntityDto, cellData.getValue(), cellData.getEntityPropertyPath());
}
} catch (ImportErrorException | InvalidColumnException e) {
return e;
}
return null;
});
// if there is already an infrastructure object with this name in the database
if (!iHasImportError) {
try {
switch(type) {
case COMMUNITY:
FacadeProvider.getCommunityFacade().save((CommunityDto) newEntityDto, allowOverwrite);
break;
case DISTRICT:
FacadeProvider.getDistrictFacade().save((DistrictDto) newEntityDto, allowOverwrite);
break;
case FACILITY:
FacadeProvider.getFacilityFacade().save((FacilityDto) newEntityDto, allowOverwrite);
break;
case POINT_OF_ENTRY:
FacadeProvider.getPointOfEntryFacade().save((PointOfEntryDto) newEntityDto, allowOverwrite);
break;
case REGION:
FacadeProvider.getRegionFacade().save((RegionDto) newEntityDto, allowOverwrite);
break;
case AREA:
FacadeProvider.getAreaFacade().save((AreaDto) newEntityDto, allowOverwrite);
break;
case SUBCONTINENT:
FacadeProvider.getSubcontinentFacade().save((SubcontinentDto) newEntityDto, allowOverwrite);
break;
case CONTINENT:
FacadeProvider.getContinentFacade().save((ContinentDto) newEntityDto, allowOverwrite);
break;
default:
throw new IllegalArgumentException(type.toString());
}
return ImportLineResult.SUCCESS;
} catch (ValidationRuntimeException e) {
writeImportError(values, e.getMessage());
return ImportLineResult.ERROR;
}
} else {
return ImportLineResult.ERROR;
}
}
Aggregations