Search in sources :

Example 1 with LocalizedStrings

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);
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) AddressQuestionDefinition(services.question.types.AddressQuestionDefinition) TextQuestionDefinition(services.question.types.TextQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) LocalizedStrings(services.LocalizedStrings) Test(org.junit.Test)

Example 2 with LocalizedStrings

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;
}
Also used : LocalizedStrings(services.LocalizedStrings)

Example 3 with LocalizedStrings

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OptionalInt(java.util.OptionalInt) CiviFormError(services.CiviFormError) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) Sets(com.google.common.collect.Sets) QuestionOption(services.question.QuestionOption) Objects(java.util.Objects) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) OptionalLong(java.util.OptionalLong) LocalizedStrings(services.LocalizedStrings) Locale(java.util.Locale) Optional(java.util.Optional) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Path(services.Path) RepeatedEntity(services.applicant.RepeatedEntity) QuestionOption(services.question.QuestionOption) ImmutableSet(com.google.common.collect.ImmutableSet) CiviFormError(services.CiviFormError) OptionalInt(java.util.OptionalInt)

Example 4 with LocalizedStrings

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;
}
Also used : LocalizedStrings(services.LocalizedStrings)

Example 5 with LocalizedStrings

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;
}
Also used : QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) LocalizedStrings(services.LocalizedStrings)

Aggregations

LocalizedStrings (services.LocalizedStrings)6 QuestionDefinitionBuilder (services.question.types.QuestionDefinitionBuilder)3 QuestionOption (services.question.QuestionOption)2 MultiOptionQuestionDefinition (services.question.types.MultiOptionQuestionDefinition)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 OptionalLong (java.util.OptionalLong)1 Test (org.junit.Test)1 CiviFormError (services.CiviFormError)1