Search in sources :

Example 1 with DeaContentEntry

use of de.symeda.sormas.api.travelentry.DeaContentEntry in project SORMAS-Project by hzi-braunschweig.

the class TravelEntryImportFacadeEjb method insertColumnEntryIntoData.

private void insertColumnEntryIntoData(TravelEntryDto travelEntry, PersonDto person, String entry, String[] entryHeaderPath) throws InvalidColumnException, ImportErrorException {
    String propertyCaption = String.join("", entryHeaderPath);
    // Build the SORMAS property based on the DEA caption
    String personProperty = getPersonProperty(propertyCaption);
    Object currentElement = personProperty != null ? person : travelEntry;
    if (personProperty != null) {
        // Map the entry to an expected SORMAS value if necessary
        entry = getPersonValue(personProperty, entry);
    }
    Language language = I18nProperties.getUserLanguage();
    try {
        // Some person-related fields need to be handled in a specific way for the DEA import
        if (PersonDto.BIRTH_DATE.equals(personProperty)) {
            Date birthDate = DateHelper.parseDate(entry, new SimpleDateFormat("dd.MM.yyyy"));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(birthDate);
            person.setBirthdateDD(calendar.get(Calendar.DAY_OF_MONTH));
            // In calendar API months are indexed from 0 @see https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH
            int birthdateMonth = calendar.get(Calendar.MONTH) + 1;
            person.setBirthdateMM(birthdateMonth);
            person.setBirthdateYYYY(calendar.get(Calendar.YEAR));
            return;
        } else if (PHONE_PRIVATE.equals(personProperty)) {
            person.setPhone(entry);
            return;
        } else if (PHONE_ADDITIONAL.equals(personProperty)) {
            person.setAdditionalPhone(entry);
            return;
        } else if (EMAIL.equals(personProperty)) {
            person.setEmailAddress(entry);
            return;
        }
        String relevantProperty = personProperty != null ? personProperty : propertyCaption;
        PropertyDescriptor pd = new PropertyDescriptor(relevantProperty, currentElement.getClass());
        Class<?> propertyType = pd.getPropertyType();
        // according to the types of the case or person fields
        if (importFacade.executeDefaultInvoke(pd, currentElement, entry, entryHeaderPath, false)) {
        // No action needed
        } else if (propertyType.isAssignableFrom(DistrictReferenceDto.class)) {
            List<DistrictReferenceDto> district = districtFacade.getByName(entry, ImportHelper.getRegionBasedOnDistrict(pd.getName(), null, null, travelEntry, 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, travelEntry.getResponsibleDistrict(), 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(PointOfEntryReferenceDto.class)) {
            PointOfEntryReferenceDto pointOfEntryReference;
            DistrictReferenceDto pointOfEntryDistrict = travelEntry.getPointOfEntryDistrict() != null ? travelEntry.getPointOfEntryDistrict() : travelEntry.getResponsibleDistrict();
            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.importPropertyTypeNotAllowed, propertyType.getName()));
        }
    } catch (IntrospectionException e) {
        // Add the property to the deaContent field of the travel entry
        if (travelEntry.getDeaContent() == null) {
            travelEntry.setDeaContent(new ArrayList<>());
        }
        travelEntry.getDeaContent().add(new DeaContentEntry(propertyCaption, entry));
    } 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 | UnsupportedOperationException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.error("Unexpected error when trying to import a travel entry: " + e.getMessage(), e);
        throw new ImportErrorException(I18nProperties.getValidationError(Validations.importUnexpectedError));
    }
}
Also used : IntrospectionException(java.beans.IntrospectionException) ArrayList(java.util.ArrayList) 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) DeaContentEntry(de.symeda.sormas.api.travelentry.DeaContentEntry) Calendar(java.util.Calendar) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Date(java.util.Date) 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) PointOfEntryReferenceDto(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryReferenceDto) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DeaContentEntry

use of de.symeda.sormas.api.travelentry.DeaContentEntry in project SORMAS-Project by hzi-braunschweig.

the class TravelEntryCreateForm method buildDeaContent.

private void buildDeaContent(TravelEntryDto newFieldValue) {
    final List<DeaContentEntry> deaContent = newFieldValue.getDeaContent();
    if (CollectionUtils.isNotEmpty(deaContent)) {
        deaFormBuilder = new DEAFormBuilder(deaContent, true);
        deaFormBuilder.buildForm();
        getContent().addComponent(deaFormBuilder.getLayout(), DEA_CONTENT_LOC);
    }
}
Also used : DeaContentEntry(de.symeda.sormas.api.travelentry.DeaContentEntry) DEAFormBuilder(de.symeda.sormas.ui.travelentry.DEAFormBuilder)

Example 3 with DeaContentEntry

use of de.symeda.sormas.api.travelentry.DeaContentEntry in project SORMAS-Project by hzi-braunschweig.

the class DEAFormBuilder method buildForm.

public void buildForm() {
    SormasFieldGroupFieldFactory fieldFactory = new SormasFieldGroupFieldFactory(new FieldVisibilityCheckers(), UiFieldAccessCheckers.getNoop());
    for (DeaContentEntry deaContentEntry : deaContentEntries) {
        final TextField textField = fieldFactory.createField(String.class, TextField.class);
        textField.setWidthFull();
        final String caption = deaContentEntry.getCaption();
        textField.setId(caption);
        CssStyles.style(textField, CssStyles.TEXTFIELD_ROW);
        textField.setCaption(caption);
        if (!isCreate) {
            textField.setValue(deaContentEntry.getValue());
        }
        gridLayout.addComponent(textField);
        fields.add(textField);
    }
}
Also used : SormasFieldGroupFieldFactory(de.symeda.sormas.ui.utils.SormasFieldGroupFieldFactory) DeaContentEntry(de.symeda.sormas.api.travelentry.DeaContentEntry) FieldVisibilityCheckers(de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers) TextField(com.vaadin.v7.ui.TextField)

Aggregations

DeaContentEntry (de.symeda.sormas.api.travelentry.DeaContentEntry)3 TextField (com.vaadin.v7.ui.TextField)1 Language (de.symeda.sormas.api.Language)1 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)1 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)1 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)1 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)1 PointOfEntryReferenceDto (de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryReferenceDto)1 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)1 FieldVisibilityCheckers (de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers)1 DEAFormBuilder (de.symeda.sormas.ui.travelentry.DEAFormBuilder)1 SormasFieldGroupFieldFactory (de.symeda.sormas.ui.utils.SormasFieldGroupFieldFactory)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1