Search in sources :

Example 1 with BirthDateDto

use of de.symeda.sormas.api.caze.BirthDateDto 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());
    }
}
Also used : FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) PersonReferenceDto(de.symeda.sormas.api.person.PersonReferenceDto) IntrospectionException(java.beans.IntrospectionException) DataHelper(de.symeda.sormas.api.utils.DataHelper) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) List(java.util.List) ArrayList(java.util.ArrayList) PropertyDescriptor(java.beans.PropertyDescriptor) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) EventParticipantDto(de.symeda.sormas.api.event.EventParticipantDto) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) CsvValidationException(com.opencsv.exceptions.CsvValidationException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) IOException(java.io.IOException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) BirthDateDto(de.symeda.sormas.api.caze.BirthDateDto)

Example 2 with BirthDateDto

use of de.symeda.sormas.api.caze.BirthDateDto 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));
        }
    }
}
Also used : FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) PersonReferenceDto(de.symeda.sormas.api.person.PersonReferenceDto) IntrospectionException(java.beans.IntrospectionException) DataHelper(de.symeda.sormas.api.utils.DataHelper) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) Language(de.symeda.sormas.api.Language) List(java.util.List) ArrayList(java.util.ArrayList) PropertyDescriptor(java.beans.PropertyDescriptor) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) Optional(java.util.Optional) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) BirthDateDto(de.symeda.sormas.api.caze.BirthDateDto) PointOfEntryReferenceDto(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryReferenceDto) ParseException(java.text.ParseException)

Example 3 with BirthDateDto

use of de.symeda.sormas.api.caze.BirthDateDto 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());
    }
}
Also used : FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) PersonReferenceDto(de.symeda.sormas.api.person.PersonReferenceDto) IntrospectionException(java.beans.IntrospectionException) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) ContactDto(de.symeda.sormas.api.contact.ContactDto) SimilarContactDto(de.symeda.sormas.api.contact.SimilarContactDto) List(java.util.List) ArrayList(java.util.ArrayList) LocationDto(de.symeda.sormas.api.location.LocationDto) PropertyDescriptor(java.beans.PropertyDescriptor) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) PersonDto(de.symeda.sormas.api.person.PersonDto) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) CsvValidationException(com.opencsv.exceptions.CsvValidationException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) IOException(java.io.IOException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) BirthDateDto(de.symeda.sormas.api.caze.BirthDateDto)

Example 4 with BirthDateDto

use of de.symeda.sormas.api.caze.BirthDateDto in project SORMAS-Project by hzi-braunschweig.

the class BirthDateField method initContent.

@Override
protected Component initContent() {
    if (getValue() == null) {
        setValue(new BirthDateDto());
    }
    HorizontalLayout layout = new HorizontalLayout();
    dateOfBirthYear.setId("dateOfBirthYear");
    dateOfBirthYear.setEmptySelectionAllowed(true);
    dateOfBirthYear.setItems(DateHelper.getYearsToNow());
    dateOfBirthYear.setWidth(80, Unit.PIXELS);
    dateOfBirthYear.addStyleName(CssStyles.CAPTION_OVERFLOW);
    binder.forField(dateOfBirthYear).withValidator((e, context) -> {
        try {
            ControllerProvider.getPersonController().validateBirthDate(e, dateOfBirthMonth.getValue(), dateOfBirthDay.getValue());
            return ValidationResult.ok();
        } catch (Validator.InvalidValueException ex) {
            return ValidationResult.error(ex.getMessage());
        }
    }).bind(BirthDateDto.DATE_OF_BIRTH_YYYY);
    dateOfBirthMonth.setId("dateOfBirthMonth");
    dateOfBirthMonth.setEmptySelectionAllowed(true);
    dateOfBirthMonth.setItems(DateHelper.getMonthsInYear());
    dateOfBirthMonth.setPageLength(12);
    setItemCaptionsForMonths(dateOfBirthMonth);
    dateOfBirthMonth.setWidth(120, Unit.PIXELS);
    binder.forField(dateOfBirthMonth).withValidator((e, context) -> {
        try {
            ControllerProvider.getPersonController().validateBirthDate(dateOfBirthYear.getValue(), e, dateOfBirthDay.getValue());
            return ValidationResult.ok();
        } catch (Validator.InvalidValueException ex) {
            return ValidationResult.error(ex.getMessage());
        }
    }).bind(BirthDateDto.DATE_OF_BIRTH_MM);
    dateOfBirthDay.setId("dateOfBirthDay");
    dateOfBirthDay.setEmptySelectionAllowed(true);
    dateOfBirthDay.setWidth(80, Unit.PIXELS);
    binder.forField(dateOfBirthDay).withValidator((e, context) -> {
        try {
            ControllerProvider.getPersonController().validateBirthDate(dateOfBirthYear.getValue(), dateOfBirthMonth.getValue(), e);
            return ValidationResult.ok();
        } catch (Validator.InvalidValueException ex) {
            return ValidationResult.error(ex.getMessage());
        }
    }).bind(BirthDateDto.DATE_OF_BIRTH_DD);
    // Update the list of days according to the selected month and year
    dateOfBirthYear.addValueChangeListener(e -> {
        getValue().setDateOfBirthYYYY(e.getValue());
        updateListOfDays(e.getValue(), dateOfBirthMonth.getValue(), dateOfBirthDay);
        dateOfBirthMonth.markAsDirty();
        dateOfBirthDay.markAsDirty();
    });
    dateOfBirthMonth.addValueChangeListener(e -> {
        getValue().setDateOfBirthMM(e.getValue());
        updateListOfDays(dateOfBirthYear.getValue(), e.getValue(), dateOfBirthDay);
        dateOfBirthYear.markAsDirty();
        dateOfBirthDay.markAsDirty();
    });
    dateOfBirthDay.addValueChangeListener(e -> {
        getValue().setDateOfBirthDD(e.getValue());
        dateOfBirthYear.markAsDirty();
        dateOfBirthMonth.markAsDirty();
    });
    layout.addComponents(dateOfBirthYear, dateOfBirthMonth, dateOfBirthDay);
    layout.setComponentAlignment(dateOfBirthMonth, Alignment.BOTTOM_LEFT);
    layout.setComponentAlignment(dateOfBirthDay, Alignment.BOTTOM_LEFT);
    return layout;
}
Also used : CustomField(com.vaadin.ui.CustomField) Month(java.time.Month) Validator(com.vaadin.v7.data.Validator) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) Alignment(com.vaadin.ui.Alignment) ComboBox(com.vaadin.ui.ComboBox) DateHelper(de.symeda.sormas.api.utils.DateHelper) ValidationResult(com.vaadin.data.ValidationResult) Binder(com.vaadin.data.Binder) ControllerProvider(de.symeda.sormas.ui.ControllerProvider) List(java.util.List) BirthDateDto(de.symeda.sormas.api.caze.BirthDateDto) CssStyles(de.symeda.sormas.ui.utils.CssStyles) HorizontalLayout(com.vaadin.ui.HorizontalLayout) Component(com.vaadin.ui.Component) BirthDateDto(de.symeda.sormas.api.caze.BirthDateDto) HorizontalLayout(com.vaadin.ui.HorizontalLayout)

Aggregations

BirthDateDto (de.symeda.sormas.api.caze.BirthDateDto)4 List (java.util.List)4 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)3 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)3 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)3 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)3 FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)3 PersonReferenceDto (de.symeda.sormas.api.person.PersonReferenceDto)3 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)3 IntrospectionException (java.beans.IntrospectionException)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 CsvValidationException (com.opencsv.exceptions.CsvValidationException)2 DataHelper (de.symeda.sormas.api.utils.DataHelper)2 IOException (java.io.IOException)2 Binder (com.vaadin.data.Binder)1 ValidationResult (com.vaadin.data.ValidationResult)1 Alignment (com.vaadin.ui.Alignment)1 ComboBox (com.vaadin.ui.ComboBox)1