use of services.LocalizedStrings in project civiform by seattle-uat.
the class QuestionTest method canSerializeAndDeserializeEnumeratorQuestion.
@Test
public void canSerializeAndDeserializeEnumeratorQuestion() throws UnsupportedQuestionTypeException {
LocalizedStrings entityType = LocalizedStrings.of(Locale.US, "entity");
QuestionDefinition definition = new QuestionDefinitionBuilder().setQuestionType(QuestionType.ENUMERATOR).setName("").setDescription("").setEnumeratorId(Optional.of(123L)).setQuestionText(LocalizedStrings.of()).setQuestionHelpText(LocalizedStrings.empty()).setEntityType(entityType).build();
Question question = new Question(definition);
question.save();
Question found = repo.lookupQuestion(question.id).toCompletableFuture().join().get();
assertThat(found.getQuestionDefinition().getQuestionType()).isEqualTo(QuestionType.ENUMERATOR);
EnumeratorQuestionDefinition enumerator = (EnumeratorQuestionDefinition) found.getQuestionDefinition();
assertThat(enumerator.getEntityType()).isEqualTo(entityType);
}
use of services.LocalizedStrings in project civiform by seattle-uat.
the class Question method setQuestionHelpText.
/**
* Add {@link LocalizedStrings} for question help text to the builder, taking into account legacy
* columns.
*
* <p>The majority of questions should have `questionHelpText` and not `legacyQuestionHelpText`.
*/
private Question setQuestionHelpText(QuestionDefinitionBuilder builder) {
LocalizedStrings textToSet = Optional.ofNullable(questionHelpText).orElseGet(() -> LocalizedStrings.create(legacyQuestionHelpText, /* canBeEmpty= */
true));
builder.setQuestionHelpText(textToSet);
return this;
}
use of services.LocalizedStrings in project civiform by seattle-uat.
the class QuestionDefinition method validate.
/**
* Validate that all required fields are present and valid for the question.
*/
public ImmutableSet<CiviFormError> validate() {
ImmutableSet.Builder<CiviFormError> errors = new ImmutableSet.Builder<>();
if (name.isBlank()) {
errors.add(CiviFormError.of("Name cannot be blank"));
}
if (questionText.isEmpty()) {
errors.add(CiviFormError.of("Question text cannot be blank"));
}
if (questionText.hasEmptyTranslation()) {
errors.add(CiviFormError.of("Question text cannot be blank"));
}
if (getQuestionType().equals(QuestionType.ENUMERATOR)) {
EnumeratorQuestionDefinition enumeratorQuestionDefinition = (EnumeratorQuestionDefinition) this;
if (enumeratorQuestionDefinition.getEntityType().hasEmptyTranslation()) {
errors.add(CiviFormError.of("Enumerator question must have specified entity type"));
}
}
if (isRepeated() && !questionTextAndHelpTextContainsRepeatedEntityNameFormatString()) {
errors.add(CiviFormError.of("Repeated questions must reference '$this' in the text and help text (if present)"));
}
if (getQuestionType().isMultiOptionType()) {
MultiOptionQuestionDefinition multiOptionQuestionDefinition = (MultiOptionQuestionDefinition) this;
if (multiOptionQuestionDefinition.getOptions().isEmpty()) {
errors.add(CiviFormError.of("Multi-option questions must have at least one option"));
}
if (multiOptionQuestionDefinition.getOptions().stream().anyMatch(option -> option.optionText().hasEmptyTranslation())) {
errors.add(CiviFormError.of("Multi-option questions cannot have blank options"));
}
int numOptions = multiOptionQuestionDefinition.getOptions().size();
int numUniqueOptionDefaultValues = multiOptionQuestionDefinition.getOptions().stream().map(QuestionOption::optionText).map(LocalizedStrings::getDefault).distinct().mapToInt(s -> 1).sum();
if (numUniqueOptionDefaultValues != numOptions) {
errors.add(CiviFormError.of("Multi-option question options must be unique"));
}
OptionalInt minChoicesRequired = multiOptionQuestionDefinition.getMultiOptionValidationPredicates().minChoicesRequired();
OptionalInt maxChoicesAllowed = multiOptionQuestionDefinition.getMultiOptionValidationPredicates().maxChoicesAllowed();
if (minChoicesRequired.isPresent()) {
if (minChoicesRequired.getAsInt() < 0) {
errors.add(CiviFormError.of("Minimum number of choices required cannot be negative"));
}
if (minChoicesRequired.getAsInt() > numOptions) {
errors.add(CiviFormError.of("Minimum number of choices required cannot exceed the number of options"));
}
}
if (maxChoicesAllowed.isPresent()) {
if (maxChoicesAllowed.getAsInt() < 0) {
errors.add(CiviFormError.of("Maximum number of choices allowed cannot be negative"));
}
if (maxChoicesAllowed.getAsInt() > numOptions) {
errors.add(CiviFormError.of("Maximum number of choices allowed cannot exceed the number of options"));
}
}
if (minChoicesRequired.isPresent() && maxChoicesAllowed.isPresent()) {
if (minChoicesRequired.getAsInt() == 0 && maxChoicesAllowed.getAsInt() == 0) {
errors.add(CiviFormError.of("Cannot require exactly 0 choices"));
}
if (minChoicesRequired.getAsInt() > maxChoicesAllowed.getAsInt()) {
errors.add(CiviFormError.of("Minimum number of choices required must be less than or equal to the maximum" + " choices allowed"));
}
}
}
return errors.build();
}
use of services.LocalizedStrings in project civiform by seattle-uat.
the class Question method setQuestionText.
/**
* Add {@link LocalizedStrings} for question text to the builder, taking into account legacy
* columns.
*
* <p>The majority of questions should have `questionText` and not `legacyQuestionText`.
*/
private Question setQuestionText(QuestionDefinitionBuilder builder) {
LocalizedStrings textToSet = Optional.ofNullable(questionText).orElseGet(() -> LocalizedStrings.create(legacyQuestionText));
builder.setQuestionText(textToSet);
return this;
}
use of services.LocalizedStrings in project civiform by seattle-uat.
the class QuestionForm method getBuilder.
public QuestionDefinitionBuilder getBuilder() {
LocalizedStrings questionTextMap = questionText.isEmpty() ? LocalizedStrings.of() : LocalizedStrings.of(Locale.US, questionText);
LocalizedStrings questionHelpTextMap = questionHelpText.isEmpty() ? LocalizedStrings.empty() : LocalizedStrings.of(Locale.US, questionHelpText);
QuestionDefinitionBuilder builder = new QuestionDefinitionBuilder().setQuestionType(getQuestionType()).setName(questionName).setDescription(questionDescription).setEnumeratorId(enumeratorId).setQuestionText(questionTextMap).setQuestionHelpText(questionHelpTextMap);
return builder;
}
Aggregations