use of de.symeda.sormas.api.importexport.ImportErrorException in project SORMAS-Project by hzi-braunschweig.
the class DataImporter method executeDefaultInvoke.
/**
* Contains checks for the most common data types for entries in the import file. This method should be called
* in every subclass whenever data from the import file is supposed to be written to the entity in question.
* Additional invokes need to be executed manually in the subclass.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected boolean executeDefaultInvoke(PropertyDescriptor pd, Object element, String entry, String[] entryHeaderPath) throws InvocationTargetException, IllegalAccessException, ImportErrorException {
Class<?> propertyType = pd.getPropertyType();
if (propertyType.isEnum()) {
Enum enumValue = null;
Class<Enum> enumType = (Class<Enum>) propertyType;
try {
enumValue = Enum.valueOf(enumType, entry.toUpperCase());
} catch (IllegalArgumentException e) {
// ignore
}
if (enumValue == null) {
enumValue = enumCaptionCache.getEnumByCaption(enumType, entry);
}
pd.getWriteMethod().invoke(element, enumValue);
return true;
}
if (propertyType.isAssignableFrom(Date.class)) {
String dateFormat = I18nProperties.getUserLanguage().getDateFormat();
try {
pd.getWriteMethod().invoke(element, DateHelper.parseDateWithException(entry, dateFormat));
return true;
} catch (ParseException e) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importInvalidDate, pd.getName(), DateHelper.getAllowedDateFormats(dateFormat)));
}
}
if (propertyType.isAssignableFrom(Integer.class)) {
pd.getWriteMethod().invoke(element, Integer.parseInt(entry));
return true;
}
if (propertyType.isAssignableFrom(Double.class)) {
pd.getWriteMethod().invoke(element, Double.parseDouble(entry));
return true;
}
if (propertyType.isAssignableFrom(Float.class)) {
pd.getWriteMethod().invoke(element, Float.parseFloat(entry));
return true;
}
if (propertyType.isAssignableFrom(Boolean.class) || propertyType.isAssignableFrom(boolean.class)) {
pd.getWriteMethod().invoke(element, DataHelper.parseBoolean(entry));
return true;
}
if (propertyType.isAssignableFrom(AreaReferenceDto.class)) {
List<AreaReferenceDto> areas = FacadeProvider.getAreaFacade().getByName(entry, false);
if (areas.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
} else if (areas.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importAreaNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(element, areas.get(0));
return true;
}
}
if (propertyType.isAssignableFrom(SubcontinentReferenceDto.class)) {
List<SubcontinentReferenceDto> subcontinents = FacadeProvider.getSubcontinentFacade().getByDefaultName(entry, false);
if (subcontinents.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
} else if (subcontinents.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(element, subcontinents.get(0));
return true;
}
}
if (propertyType.isAssignableFrom(CountryReferenceDto.class)) {
List<CountryReferenceDto> countries = FacadeProvider.getCountryFacade().getByDefaultName(entry, false);
if (countries.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
} else if (countries.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(element, countries.get(0));
return true;
}
}
if (propertyType.isAssignableFrom(ContinentReferenceDto.class)) {
List<ContinentReferenceDto> continents = FacadeProvider.getContinentFacade().getByDefaultName(entry, false);
if (continents.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
} else if (continents.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(element, continents.get(0));
return true;
}
}
if (propertyType.isAssignableFrom(RegionReferenceDto.class)) {
List<RegionDto> regions = FacadeProvider.getRegionFacade().getByName(entry, false);
if (regions.isEmpty()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
} else if (regions.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importRegionNotUnique, entry, buildEntityProperty(entryHeaderPath)));
} else {
RegionDto region = regions.get(0);
CountryReferenceDto serverCountry = FacadeProvider.getCountryFacade().getServerCountry();
if (region.getCountry() != null && !region.getCountry().equals(serverCountry)) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importRegionNotInServerCountry, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(element, region.toReference());
return true;
}
}
}
if (propertyType.isAssignableFrom(UserReferenceDto.class)) {
UserDto user = FacadeProvider.getUserFacade().getByUserName(entry);
if (user != null) {
pd.getWriteMethod().invoke(element, user.toReference());
return true;
} else {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
}
}
if (propertyType.isAssignableFrom(String.class)) {
pd.getWriteMethod().invoke(element, entry);
return true;
}
return false;
}
use of de.symeda.sormas.api.importexport.ImportErrorException in project SORMAS-Project by hzi-braunschweig.
the class DataImporter method insertColumnEntryIntoRelatedObject.
/**
* Inserts the entry of a single cell into a related object
*/
protected void insertColumnEntryIntoRelatedObject(Object currentElement, String entry, String[] entryHeaderPath) throws InvalidColumnException, de.symeda.sormas.api.importexport.ImportErrorException {
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);
} else {
PropertyDescriptor pd = new PropertyDescriptor(headerPathElementName, currentElement.getClass());
Class<?> propertyType = pd.getPropertyType();
// according to the types of the sample or pathogen test fields
if (executeDefaultInvoke(pd, currentElement, entry, entryHeaderPath)) {
continue;
} else {
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importCasesPropertyTypeNotAllowed, 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 related object data data for {}: {}", entryHeaderPath, e.getMessage());
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importCasesUnexpectedError));
}
}
}
use of de.symeda.sormas.api.importexport.ImportErrorException in project SORMAS-Project by hzi-braunschweig.
the class EventParticipantImporter method insertColumnEntryIntoData.
/**
* Inserts the entry of a single cell into the eventparticipant or its person.
*/
private void insertColumnEntryIntoData(EventParticipantDto eventParticipant, PersonDto person, String entry, String[] entryHeaderPath) throws InvalidColumnException, ImportErrorException {
Object currentElement = eventParticipant;
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 (EventParticipantExportDto.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 eventparticipant 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(), null, 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)) {
List<CommunityReferenceDto> community = FacadeProvider.getCommunityFacade().getByName(entry, 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)) {
DataHelper.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 an eventparticipant: " + e.getMessage(), e);
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importCasesUnexpectedError));
}
}
ImportLineResultDto<EventParticipantDto> constraintErrors = validateConstraints(eventParticipant);
if (constraintErrors.isError()) {
throw new ImportErrorException(constraintErrors.getMessage());
}
}
use of de.symeda.sormas.api.importexport.ImportErrorException in project SORMAS-Project by hzi-braunschweig.
the class CaseImportFacadeEjb method insertColumnEntryIntoData.
/**
* Inserts the entry of a single cell into the case or its person.
*/
private void insertColumnEntryIntoData(CaseDataDto caze, PersonDto person, String entry, String[] entryHeaderPath) throws InvalidColumnException, ImportErrorException {
Object currentElement = caze;
for (int i = 0; i < entryHeaderPath.length; i++) {
String headerPathElementName = entryHeaderPath[i];
Language language = I18nProperties.getUserLanguage();
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 (CaseExportDto.BIRTH_DATE.equals(headerPathElementName)) {
BirthDateDto birthDateDto = PersonHelper.parseBirthdate(entry, language);
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 case or person fields
if (importFacade.executeDefaultInvoke(pd, currentElement, entry, entryHeaderPath, false)) {
continue;
} else if (propertyType.isAssignableFrom(DistrictReferenceDto.class)) {
List<DistrictReferenceDto> district = districtFacade.getByName(entry, ImportHelper.getRegionBasedOnDistrict(pd.getName(), caze, null, null, 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)) {
List<CommunityReferenceDto> community = communityFacade.getByName(entry, ImportHelper.getDistrictBasedOnCommunity(pd.getName(), caze, person, currentElement), 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)) {
DataHelper.Pair<DistrictReferenceDto, CommunityReferenceDto> infrastructureData = ImportHelper.getDistrictAndCommunityBasedOnFacility(pd.getName(), caze, person, currentElement);
if (I18nProperties.getPrefixCaption(FacilityDto.I18N_PREFIX, FacilityDto.OTHER_FACILITY).equals(entry)) {
entry = FacilityDto.OTHER_FACILITY;
}
if (I18nProperties.getPrefixCaption(FacilityDto.I18N_PREFIX, FacilityDto.NO_FACILITY).equals(entry)) {
entry = FacilityDto.NO_FACILITY;
}
List<FacilityReferenceDto> facilities = facilityFacade.getByNameAndType(entry, infrastructureData.getElement0(), infrastructureData.getElement1(), getTypeOfFacility(pd.getName(), currentElement), false);
if (facilities.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 (facilities.size() > 1 && infrastructureData.getElement1() == null) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importFacilityNotUniqueInDistrict, entry, buildEntityProperty(entryHeaderPath)));
} else if (facilities.size() > 1 && infrastructureData.getElement1() != null) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importFacilityNotUniqueInCommunity, entry, buildEntityProperty(entryHeaderPath)));
} else {
pd.getWriteMethod().invoke(currentElement, facilities.get(0));
}
} else if (propertyType.isAssignableFrom(PointOfEntryReferenceDto.class)) {
PointOfEntryReferenceDto pointOfEntryReference;
DistrictReferenceDto pointOfEntryDistrict = CaseLogic.getDistrictWithFallback(caze);
List<PointOfEntryReferenceDto> customPointsOfEntry = pointOfEntryFacade.getByName(entry, pointOfEntryDistrict, false);
if (customPointsOfEntry.isEmpty()) {
final String poeName = entry;
List<PointOfEntryDto> defaultPointOfEntries = pointOfEntryFacade.getByUuids(PointOfEntryDto.CONSTANT_POE_UUIDS);
Optional<PointOfEntryDto> defaultPointOfEntry = defaultPointOfEntries.stream().filter(defaultPoe -> InfrastructureHelper.buildPointOfEntryString(defaultPoe.getUuid(), defaultPoe.getName()).equals(poeName)).findFirst();
if (!defaultPointOfEntry.isPresent()) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrDistrict, entry, buildEntityProperty(entryHeaderPath)));
}
pointOfEntryReference = defaultPointOfEntry.get().toReference();
} else if (customPointsOfEntry.size() > 1) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importPointOfEntryNotUniqueInDistrict, entry, buildEntityProperty(entryHeaderPath)));
} else {
pointOfEntryReference = customPointsOfEntry.get(0);
}
pd.getWriteMethod().invoke(currentElement, pointOfEntryReference);
} else {
throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importCasesPropertyTypeNotAllowed, 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 | EnumService.InvalidEnumCaptionException e) {
throw new ImportErrorException(entry, buildEntityProperty(entryHeaderPath));
} catch (ParseException e) {
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importInvalidDate, buildEntityProperty(entryHeaderPath), DateHelper.getAllowedDateFormats(language.getDateFormat())));
} catch (ImportErrorException e) {
throw e;
} catch (Exception e) {
LOGGER.error("Unexpected error when trying to import a case: " + e.getMessage(), e);
throw new ImportErrorException(I18nProperties.getValidationError(Validations.importCasesUnexpectedError));
}
}
}
use of de.symeda.sormas.api.importexport.ImportErrorException in project SORMAS-Project by hzi-braunschweig.
the class CountryImporter method insertColumnEntryIntoData.
/**
* Inserts the entry of a single cell into the infrastructure object.
*/
private void insertColumnEntryIntoData(CountryDto 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();
if (!executeDefaultInvoke(pd, currentElement, value, entityPropertyPath)) {
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<CountryDto> constraintErrors = validateConstraints(newEntityDto);
if (constraintErrors.isError()) {
throw new ImportErrorException(constraintErrors.getMessage());
}
}
Aggregations