Search in sources :

Example 1 with CampaignFormTranslations

use of de.symeda.sormas.api.campaign.form.CampaignFormTranslations in project SORMAS-Project by hzi-braunschweig.

the class CampaignFormMetaFacadeEjb method validateAndClean.

@Override
public void validateAndClean(CampaignFormMetaDto campaignFormMetaDto) throws ValidationRuntimeException {
    if (CollectionUtils.isEmpty(campaignFormMetaDto.getCampaignFormElements())) {
        return;
    }
    // Throw an exception when the schema definition contains an element without an ID or type
    campaignFormMetaDto.getCampaignFormElements().stream().filter(e -> StringUtils.isBlank(e.getId()) || StringUtils.isBlank(e.getType())).findFirst().ifPresent(e -> {
        if (StringUtils.isBlank(e.getId())) {
            throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormElementIdRequired));
        } else {
            throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormElementTypeRequired, e.getId()));
        }
    });
    // Throw an exception when the schema definition contains the same ID more than once
    campaignFormMetaDto.getCampaignFormElements().forEach(e -> {
        if (Collections.frequency(campaignFormMetaDto.getCampaignFormElements(), e) > 1) {
            throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormElementDuplicateId, e.getId()));
        }
    });
    // Throw an error if any translation does not have a language code or contains an element without an ID or caption
    if (CollectionUtils.isNotEmpty(campaignFormMetaDto.getCampaignFormTranslations())) {
        campaignFormMetaDto.getCampaignFormTranslations().forEach(cft -> {
            if (StringUtils.isBlank(cft.getLanguageCode())) {
                throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormTranslationLanguageCodeRequired));
            }
            cft.getTranslations().stream().filter(t -> StringUtils.isBlank(t.getElementId()) || StringUtils.isBlank(t.getCaption())).findFirst().ifPresent(e -> {
                if (StringUtils.isBlank(e.getElementId())) {
                    throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormTranslationIdRequired));
                } else {
                    throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormTranslationCaptionRequired, e.getElementId(), cft.getLanguageCode()));
                }
            });
        });
    }
    Map<String, String> idsAndTypes = campaignFormMetaDto.getCampaignFormElements().stream().collect(Collectors.toMap(CampaignFormElement::getId, CampaignFormElement::getType));
    for (CampaignFormElement element : campaignFormMetaDto.getCampaignFormElements()) {
        // Clean the element caption from all HTML tags that are not explicitly allowed
        if (StringUtils.isNotBlank(element.getCaption())) {
            Whitelist whitelist = Whitelist.none();
            whitelist.addTags(CampaignFormElement.ALLOWED_HTML_TAGS);
            element.setCaption(HtmlHelper.cleanHtml(element.getCaption(), whitelist));
        }
        // Validate form elements
        validateCampaignFormElementType(element.getId(), element.getType());
        validateCampaignFormElementStyles(element.getId(), element.getStyles());
        if (StringUtils.isNotBlank(element.getDependingOn()) && ArrayUtils.isEmpty(element.getDependingOnValues())) {
            throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormDependingOnValuesMissing, element.getId()));
        }
        validateCampaignFormMetaDependency(element.getId(), element.getDependingOn(), element.getDependingOnValues(), idsAndTypes);
    }
    // Validate element IDs used in translations and clean HTML used in translation captions
    if (CollectionUtils.isNotEmpty(campaignFormMetaDto.getCampaignFormTranslations())) {
        for (CampaignFormTranslations translations : campaignFormMetaDto.getCampaignFormTranslations()) {
            translations.getTranslations().forEach(e -> {
                if (idsAndTypes.get(e.getElementId()) == null) {
                    throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.campaignFormTranslationIdInvalid, e.getElementId(), translations.getLanguageCode()));
                }
                if (StringUtils.isNotBlank(e.getCaption())) {
                    Whitelist whitelist = Whitelist.none();
                    whitelist.addTags(CampaignFormElement.ALLOWED_HTML_TAGS);
                    e.setCaption(HtmlHelper.cleanHtml(e.getCaption(), whitelist));
                }
            });
        }
    }
}
Also used : CampaignFormTranslations(de.symeda.sormas.api.campaign.form.CampaignFormTranslations) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) Whitelist(org.jsoup.safety.Whitelist) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException)

Example 2 with CampaignFormTranslations

use of de.symeda.sormas.api.campaign.form.CampaignFormTranslations in project SORMAS-Project by hzi-braunschweig.

the class CampaignFormDataFragmentUtils method getUserTranslations.

public static Map<String, String> getUserTranslations(CampaignFormMeta campaignFormMeta) {
    final Map<String, String> userTranslations = new HashMap<>();
    final Locale locale = I18nProperties.getUserLanguage().getLocale();
    if (locale != null) {
        final List<CampaignFormTranslations> campaignFormTranslations = campaignFormMeta.getCampaignFormTranslations();
        campaignFormTranslations.forEach(cft -> {
            if (cft.getLanguageCode().equalsIgnoreCase(locale.toString())) {
                cft.getTranslations().forEach(translationElement -> userTranslations.put(translationElement.getElementId(), translationElement.getCaption()));
            }
        });
    }
    return userTranslations;
}
Also used : Locale(java.util.Locale) CampaignFormTranslations(de.symeda.sormas.api.campaign.form.CampaignFormTranslations) HashMap(java.util.HashMap)

Example 3 with CampaignFormTranslations

use of de.symeda.sormas.api.campaign.form.CampaignFormTranslations in project SORMAS-Project by hzi-braunschweig.

the class CampaignFormMetaFacadeEjb method buildCampaignFormMetaFromJson.

@Override
public CampaignFormMetaDto buildCampaignFormMetaFromJson(String formId, String languageCode, String schemaDefinitionJson, String translationsJson) throws IOException {
    CampaignFormMetaDto campaignForm = new CampaignFormMetaDto();
    campaignForm.setFormId(formId);
    campaignForm.setLanguageCode(languageCode);
    ObjectMapper mapper = new ObjectMapper();
    if (StringUtils.isNotBlank(schemaDefinitionJson)) {
        campaignForm.setCampaignFormElements(Arrays.asList(mapper.readValue(schemaDefinitionJson, CampaignFormElement[].class)));
    }
    if (StringUtils.isNotBlank(translationsJson)) {
        campaignForm.setCampaignFormTranslations(Arrays.asList(mapper.readValue(translationsJson, CampaignFormTranslations[].class)));
    }
    return campaignForm;
}
Also used : CampaignFormTranslations(de.symeda.sormas.api.campaign.form.CampaignFormTranslations) CampaignFormMetaDto(de.symeda.sormas.api.campaign.form.CampaignFormMetaDto) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with CampaignFormTranslations

use of de.symeda.sormas.api.campaign.form.CampaignFormTranslations in project SORMAS-Project by hzi-braunschweig.

the class CampaignFormMeta method getCampaignFormTranslationsList.

@Transient
public List<CampaignFormTranslations> getCampaignFormTranslationsList() {
    if (campaignFormTranslationsList == null) {
        if (StringUtils.isBlank(campaignFormTranslations)) {
            campaignFormTranslationsList = new ArrayList<>();
        } else {
            try {
                ObjectMapper mapper = new ObjectMapper();
                campaignFormTranslationsList = Arrays.asList(mapper.readValue(campaignFormTranslations, CampaignFormTranslations[].class));
            } catch (IOException e) {
                throw new ValidationRuntimeException("Content of campaignFormTranslations could not be parsed to List<CampaignFormTranslations> - ID: " + getId());
            }
        }
    }
    return campaignFormTranslationsList;
}
Also used : CampaignFormTranslations(de.symeda.sormas.api.campaign.form.CampaignFormTranslations) IOException(java.io.IOException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Transient(javax.persistence.Transient)

Aggregations

CampaignFormTranslations (de.symeda.sormas.api.campaign.form.CampaignFormTranslations)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CampaignFormElement (de.symeda.sormas.api.campaign.form.CampaignFormElement)2 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)2 CampaignFormMetaDto (de.symeda.sormas.api.campaign.form.CampaignFormMetaDto)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Transient (javax.persistence.Transient)1 Whitelist (org.jsoup.safety.Whitelist)1