Search in sources :

Example 1 with FacilityReferenceDto

use of de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class SampleListEntryDtoResultTransformer method transformTuple.

@Override
public Object transformTuple(Object[] objects, String[] strings) {
    boolean referred = objects[5] != null;
    String labName = (String) objects[11];
    String labUuid = (String) objects[12];
    FacilityReferenceDto lab = new FacilityReferenceDto(labUuid, FacilityHelper.buildFacilityString(labUuid, labName), null);
    AdditionalTestingStatus additionalTestingStatus = Boolean.TRUE.equals(objects[16]) ? AdditionalTestingStatus.PERFORMED : (Boolean.TRUE.equals(objects[15]) ? AdditionalTestingStatus.REQUESTED : AdditionalTestingStatus.NOT_REQUESTED);
    return new SampleListEntryDto((String) objects[0], (SampleMaterial) objects[1], (PathogenTestResultType) objects[2], (SpecimenCondition) objects[3], (SamplePurpose) objects[4], referred, (boolean) objects[6], (Date) objects[7], (boolean) objects[8], (Date) objects[9], (Date) objects[10], lab, (SamplingReason) objects[13], (String) objects[14], additionalTestingStatus, (long) objects[17]);
}
Also used : FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) SampleListEntryDto(de.symeda.sormas.api.sample.SampleListEntryDto) AdditionalTestingStatus(de.symeda.sormas.api.sample.AdditionalTestingStatus)

Example 2 with FacilityReferenceDto

use of de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class PreviousHospitalizationsField method updateColumns.

@Override
protected void updateColumns() {
    Table table = getTable();
    table.addGeneratedColumn(PERIOD, new Table.ColumnGenerator() {

        @Override
        public Object generateCell(Table source, Object itemId, Object columnId) {
            PreviousHospitalizationDto prevHospitalization = (PreviousHospitalizationDto) itemId;
            if (prevHospitalization.getAdmissionDate() == null && prevHospitalization.getDischargeDate() == null) {
                return I18nProperties.getString(Strings.notSpecified);
            } else {
                StringBuilder periodBuilder = new StringBuilder();
                periodBuilder.append(prevHospitalization.getAdmissionDate() != null ? DateFormatHelper.formatDate(prevHospitalization.getAdmissionDate()) : "?");
                periodBuilder.append(" - ");
                periodBuilder.append(prevHospitalization.getDischargeDate() != null ? DateFormatHelper.formatDate(prevHospitalization.getDischargeDate()) : "?");
                return periodBuilder.toString();
            }
        }
    });
    table.addGeneratedColumn(COMMUNITY, (Table.ColumnGenerator) (source, itemId, columnId) -> {
        PreviousHospitalizationDto prevHospitalization = (PreviousHospitalizationDto) itemId;
        return prevHospitalization.getCommunity();
    });
    table.addGeneratedColumn(DISTRICT, (Table.ColumnGenerator) (source, itemId, columnId) -> {
        PreviousHospitalizationDto prevHospitalization = (PreviousHospitalizationDto) itemId;
        DistrictReferenceDto district = prevHospitalization.getDistrict();
        return district != null ? district.getCaption() : I18nProperties.getCaption(Captions.unknown);
    });
    table.addGeneratedColumn(PreviousHospitalizationDto.HEALTH_FACILITY, (Table.ColumnGenerator) (source, itemId, columnId) -> {
        PreviousHospitalizationDto prevHospitalization = (PreviousHospitalizationDto) itemId;
        FacilityReferenceDto healthFacility = prevHospitalization.getHealthFacility();
        return healthFacility != null ? healthFacility.getCaption() : I18nProperties.getCaption(Captions.unknown);
    });
    table.setVisibleColumns(EDIT_COLUMN_ID, PERIOD, PreviousHospitalizationDto.HEALTH_FACILITY, COMMUNITY, DISTRICT, PreviousHospitalizationDto.DESCRIPTION, PreviousHospitalizationDto.ISOLATED);
    table.setColumnExpandRatio(EDIT_COLUMN_ID, 0);
    table.setColumnExpandRatio(PERIOD, 0);
    table.setColumnExpandRatio(PreviousHospitalizationDto.HEALTH_FACILITY, 0);
    table.setColumnExpandRatio(COMMUNITY, 0);
    table.setColumnExpandRatio(DISTRICT, 0);
    table.setColumnExpandRatio(PreviousHospitalizationDto.DESCRIPTION, 0);
    table.setColumnExpandRatio(PreviousHospitalizationDto.ISOLATED, 0);
    for (Object columnId : table.getVisibleColumns()) {
        if (columnId.equals(EDIT_COLUMN_ID)) {
            table.setColumnHeader(columnId, "&nbsp");
        } else {
            table.setColumnHeader(columnId, I18nProperties.getPrefixCaption(PreviousHospitalizationDto.I18N_PREFIX, (String) columnId));
        }
    }
}
Also used : AbstractTableField(de.symeda.sormas.ui.caze.AbstractTableField) Table(com.vaadin.v7.ui.Table) DataHelper(de.symeda.sormas.api.utils.DataHelper) VaadinUiUtil(de.symeda.sormas.ui.utils.VaadinUiUtil) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) DateFormatHelper(de.symeda.sormas.ui.utils.DateFormatHelper) Window(com.vaadin.ui.Window) CommitListener(de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent.CommitListener) DeleteListener(de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent.DeleteListener) Captions(de.symeda.sormas.api.i18n.Captions) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) Consumer(java.util.function.Consumer) UserRight(de.symeda.sormas.api.user.UserRight) CommitDiscardWrapperComponent(de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent) PreviousHospitalizationDto(de.symeda.sormas.api.hospitalization.PreviousHospitalizationDto) FieldVisibilityCheckers(de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers) Strings(de.symeda.sormas.api.i18n.Strings) UiFieldAccessCheckers(de.symeda.sormas.api.utils.fieldaccess.UiFieldAccessCheckers) UserProvider(de.symeda.sormas.ui.UserProvider) Table(com.vaadin.v7.ui.Table) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) PreviousHospitalizationDto(de.symeda.sormas.api.hospitalization.PreviousHospitalizationDto) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)

Example 3 with FacilityReferenceDto

use of de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto 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 4 with FacilityReferenceDto

use of de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto in project SORMAS-Project by hzi-braunschweig.

the class HospitalizationForm method addFields.

@SuppressWarnings("deprecation")
@Override
protected void addFields() {
    if (caze == null || viewMode == null) {
        return;
    }
    Label hospitalizationHeadingLabel = new Label(I18nProperties.getString(Strings.headingHospitalization));
    hospitalizationHeadingLabel.addStyleName(H3);
    getContent().addComponent(hospitalizationHeadingLabel, HOSPITALIZATION_HEADING_LOC);
    Label previousHospitalizationsHeadingLabel = new Label(I18nProperties.getString(Strings.headingPreviousHospitalizations));
    previousHospitalizationsHeadingLabel.addStyleName(H3);
    getContent().addComponent(previousHospitalizationsHeadingLabel, PREVIOUS_HOSPITALIZATIONS_HEADING_LOC);
    TextField facilityField = addCustomField(HEALTH_FACILITY, FacilityReferenceDto.class, TextField.class);
    FacilityReferenceDto healthFacility = caze.getHealthFacility();
    final boolean noneFacility = healthFacility == null || healthFacility.getUuid().equalsIgnoreCase(FacilityDto.NONE_FACILITY_UUID);
    facilityField.setValue(noneFacility || !FacilityType.HOSPITAL.equals(caze.getFacilityType()) ? null : healthFacility.toString());
    facilityField.setReadOnly(true);
    final NullableOptionGroup admittedToHealthFacilityField = addField(HospitalizationDto.ADMITTED_TO_HEALTH_FACILITY, NullableOptionGroup.class);
    final DateField admissionDateField = addField(HospitalizationDto.ADMISSION_DATE, DateField.class);
    final DateField dischargeDateField = addDateField(HospitalizationDto.DISCHARGE_DATE, DateField.class, 7);
    intensiveCareUnit = addField(HospitalizationDto.INTENSIVE_CARE_UNIT, NullableOptionGroup.class);
    intensiveCareUnitStart = addField(HospitalizationDto.INTENSIVE_CARE_UNIT_START, DateField.class);
    intensiveCareUnitStart.setVisible(false);
    intensiveCareUnitEnd = addField(HospitalizationDto.INTENSIVE_CARE_UNIT_END, DateField.class);
    intensiveCareUnitEnd.setVisible(false);
    FieldHelper.setVisibleWhen(intensiveCareUnit, Arrays.asList(intensiveCareUnitStart, intensiveCareUnitEnd), Arrays.asList(YesNoUnknown.YES), true);
    final Field isolationDateField = addField(HospitalizationDto.ISOLATION_DATE);
    final TextArea descriptionField = addField(HospitalizationDto.DESCRIPTION, TextArea.class);
    descriptionField.setRows(4);
    final NullableOptionGroup isolatedField = addField(HospitalizationDto.ISOLATED, NullableOptionGroup.class);
    final NullableOptionGroup leftAgainstAdviceField = addField(HospitalizationDto.LEFT_AGAINST_ADVICE, NullableOptionGroup.class);
    final ComboBox hospitalizationReason = addField(HospitalizationDto.HOSPITALIZATION_REASON);
    final TextField otherHospitalizationReason = addField(HospitalizationDto.OTHER_HOSPITALIZATION_REASON, TextField.class);
    NullableOptionGroup hospitalizedPreviouslyField = addField(HospitalizationDto.HOSPITALIZED_PREVIOUSLY, NullableOptionGroup.class);
    CssStyles.style(hospitalizedPreviouslyField, CssStyles.ERROR_COLOR_PRIMARY);
    PreviousHospitalizationsField previousHospitalizationsField = addField(HospitalizationDto.PREVIOUS_HOSPITALIZATIONS, PreviousHospitalizationsField.class);
    FieldHelper.setEnabledWhen(admittedToHealthFacilityField, Arrays.asList(YesNoUnknown.YES, YesNoUnknown.NO, YesNoUnknown.UNKNOWN), Arrays.asList(facilityField, admissionDateField, dischargeDateField, intensiveCareUnit, intensiveCareUnitStart, intensiveCareUnitEnd, isolationDateField, descriptionField, isolatedField, leftAgainstAdviceField, hospitalizationReason, otherHospitalizationReason), false);
    initializeVisibilitiesAndAllowedVisibilities();
    initializeAccessAndAllowedAccesses();
    if (isVisibleAllowed(HospitalizationDto.ISOLATION_DATE)) {
        FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.ISOLATION_DATE, HospitalizationDto.ISOLATED, Arrays.asList(YesNoUnknown.YES), true);
    }
    if (isVisibleAllowed(HospitalizationDto.PREVIOUS_HOSPITALIZATIONS)) {
        FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.PREVIOUS_HOSPITALIZATIONS, HospitalizationDto.HOSPITALIZED_PREVIOUSLY, Arrays.asList(YesNoUnknown.YES), true);
    }
    FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.OTHER_HOSPITALIZATION_REASON, HospitalizationDto.HOSPITALIZATION_REASON, Collections.singletonList(HospitalizationReasonType.OTHER), true);
    // Validations
    // Add a visual-only validator to check if symptomonsetdate<admissiondate, as saving should be possible either way
    admissionDateField.addValueChangeListener(event -> {
        if (caze.getSymptoms().getOnsetDate() != null && DateTimeComparator.getDateOnlyInstance().compare(admissionDateField.getValue(), caze.getSymptoms().getOnsetDate()) < 0) {
            admissionDateField.setComponentError(new ErrorMessage() {

                @Override
                public ErrorLevel getErrorLevel() {
                    return ErrorLevel.INFO;
                }

                @Override
                public String getFormattedHtmlMessage() {
                    return I18nProperties.getValidationError(Validations.afterDateSoft, admissionDateField.getCaption(), I18nProperties.getPrefixCaption(SymptomsDto.I18N_PREFIX, SymptomsDto.ONSET_DATE));
                }
            });
        } else {
            // remove all invalidity-indicators and re-evaluate field
            admissionDateField.setComponentError(null);
            admissionDateField.markAsDirty();
        }
        // re-evaluate validity of dischargeDate (necessary because discharge has to be after admission)
        dischargeDateField.markAsDirty();
    });
    admissionDateField.addValidator(new DateComparisonValidator(admissionDateField, dischargeDateField, true, false, I18nProperties.getValidationError(Validations.beforeDate, admissionDateField.getCaption(), dischargeDateField.getCaption())));
    dischargeDateField.addValidator(new DateComparisonValidator(dischargeDateField, admissionDateField, false, false, I18nProperties.getValidationError(Validations.afterDate, dischargeDateField.getCaption(), admissionDateField.getCaption())));
    // re-evaluate admission date for consistent validation of all fields
    dischargeDateField.addValueChangeListener(event -> admissionDateField.markAsDirty());
    intensiveCareUnitStart.addValidator(new DateComparisonValidator(intensiveCareUnitStart, admissionDateField, false, false, I18nProperties.getValidationError(Validations.afterDate, intensiveCareUnitStart.getCaption(), admissionDateField.getCaption())));
    intensiveCareUnitStart.addValidator(new DateComparisonValidator(intensiveCareUnitStart, intensiveCareUnitEnd, true, false, I18nProperties.getValidationError(Validations.beforeDate, intensiveCareUnitStart.getCaption(), intensiveCareUnitEnd.getCaption())));
    intensiveCareUnitEnd.addValidator(new DateComparisonValidator(intensiveCareUnitEnd, intensiveCareUnitStart, false, false, I18nProperties.getValidationError(Validations.afterDate, intensiveCareUnitEnd.getCaption(), intensiveCareUnitStart.getCaption())));
    intensiveCareUnitEnd.addValidator(new DateComparisonValidator(intensiveCareUnitEnd, dischargeDateField, true, false, I18nProperties.getValidationError(Validations.beforeDate, intensiveCareUnitEnd.getCaption(), dischargeDateField.getCaption())));
    intensiveCareUnitStart.addValueChangeListener(event -> intensiveCareUnitEnd.markAsDirty());
    intensiveCareUnitEnd.addValueChangeListener(event -> intensiveCareUnitStart.markAsDirty());
    hospitalizedPreviouslyField.addValueChangeListener(e -> updatePrevHospHint(hospitalizedPreviouslyField, previousHospitalizationsField));
    previousHospitalizationsField.addValueChangeListener(e -> updatePrevHospHint(hospitalizedPreviouslyField, previousHospitalizationsField));
}
Also used : NullableOptionGroup(de.symeda.sormas.ui.utils.NullableOptionGroup) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) TextArea(com.vaadin.v7.ui.TextArea) ComboBox(com.vaadin.v7.ui.ComboBox) Label(com.vaadin.ui.Label) DateComparisonValidator(de.symeda.sormas.ui.utils.DateComparisonValidator) DateField(com.vaadin.v7.ui.DateField) Field(com.vaadin.v7.ui.Field) TextField(com.vaadin.v7.ui.TextField) TextField(com.vaadin.v7.ui.TextField) ErrorLevel(com.vaadin.shared.ui.ErrorLevel) DateField(com.vaadin.v7.ui.DateField) ErrorMessage(com.vaadin.server.ErrorMessage)

Example 5 with FacilityReferenceDto

use of de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto 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)

Aggregations

FacilityReferenceDto (de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto)43 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)25 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)21 RegionReferenceDto (de.symeda.sormas.api.infrastructure.region.RegionReferenceDto)15 ArrayList (java.util.ArrayList)15 List (java.util.List)15 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)10 Date (java.util.Date)9 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)8 FacilityDto (de.symeda.sormas.api.infrastructure.facility.FacilityDto)8 FacilityType (de.symeda.sormas.api.infrastructure.facility.FacilityType)8 PersonDto (de.symeda.sormas.api.person.PersonDto)8 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Label (com.vaadin.ui.Label)6 Test (org.junit.Test)6 Button (com.vaadin.ui.Button)5 Disease (de.symeda.sormas.api.Disease)5 UserDto (de.symeda.sormas.api.user.UserDto)5 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)5