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();
}
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;
}
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));
}
}
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();
}
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;
}
Aggregations