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));
}
});
}
}
}
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;
}
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;
}
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;
}
Aggregations