Search in sources :

Example 1 with InvalidQuestionTypeException

use of services.question.exceptions.InvalidQuestionTypeException in project civiform by seattle-uat.

the class ProgramBlockPredicatesEditView method createScalarDropdown.

private ContainerTag createScalarDropdown(QuestionDefinition questionDefinition) {
    ImmutableSet<Scalar> scalars;
    try {
        scalars = Scalar.getScalars(questionDefinition.getQuestionType());
    } catch (InvalidQuestionTypeException | UnsupportedQuestionTypeException e) {
        // This should never happen since we filter out Enumerator questions before this point.
        return div().withText("Sorry, you cannot create a show/hide predicate with this question type.");
    }
    ImmutableList<ContainerTag> options = scalars.stream().map(scalar -> option(scalar.toDisplayString()).withValue(scalar.name()).withData("type", scalar.toScalarType().name().toLowerCase())).collect(toImmutableList());
    return new SelectWithLabel().setFieldName("scalar").setLabelText("Field").setCustomOptions(options).addReferenceClass(ReferenceClasses.PREDICATE_SCALAR_SELECT).getContainer();
}
Also used : InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) TagCreator.text(j2html.TagCreator.text) Arrays(java.util.Arrays) Styles(views.style.Styles) LinkElement(views.components.LinkElement) Content(play.twirl.api.Content) TagCreator.h1(j2html.TagCreator.h1) TagCreator.h2(j2html.TagCreator.h2) Attr(j2html.attributes.Attr) QuestionOption(services.question.QuestionOption) PredicateAction(services.program.predicate.PredicateAction) Inject(javax.inject.Inject) TagCreator.option(j2html.TagCreator.option) ImmutableList(com.google.common.collect.ImmutableList) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) HtmlBundle(views.HtmlBundle) BaseHtmlView(views.BaseHtmlView) SelectWithLabel(views.components.SelectWithLabel) BlockDefinition(services.program.BlockDefinition) TagCreator.input(j2html.TagCreator.input) Operator(services.program.predicate.Operator) BaseStyles(views.style.BaseStyles) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Scalar(services.applicant.question.Scalar) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) QuestionDefinition(services.question.types.QuestionDefinition) TagCreator.each(j2html.TagCreator.each) TagCreator.form(j2html.TagCreator.form) controllers.admin.routes(controllers.admin.routes) ProgramDefinition(services.program.ProgramDefinition) Modal(views.components.Modal) ContainerTag(j2html.tags.ContainerTag) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) AdminStyles(views.style.AdminStyles) AdminLayout(views.admin.AdminLayout) FieldWithLabel(views.components.FieldWithLabel) ToastMessage(views.components.ToastMessage) POST(play.mvc.Http.HttpVerbs.POST) ReferenceClasses(views.style.ReferenceClasses) TagCreator.div(j2html.TagCreator.div) Http(play.mvc.Http) Tag(j2html.tags.Tag) Icons(views.components.Icons) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) ContainerTag(j2html.tags.ContainerTag) Scalar(services.applicant.question.Scalar) SelectWithLabel(views.components.SelectWithLabel)

Example 2 with InvalidQuestionTypeException

use of services.question.exceptions.InvalidQuestionTypeException in project civiform by seattle-uat.

the class Question method setQuestionOptions.

/**
 * Add {@link QuestionOption}s to the builder, taking into account legacy columns.
 *
 * <p>The majority of questions should have a `questionOptions` and not `legacyQuestionOptions`.
 */
private void setQuestionOptions(QuestionDefinitionBuilder builder) throws InvalidQuestionTypeException {
    if (!QuestionType.of(questionType).isMultiOptionType()) {
        return;
    }
    // `legacyQuestionOptions` is a legacy implementation that only supported a single locale.
    if (questionOptions != null) {
        builder.setQuestionOptions(questionOptions);
        return;
    }
    // If the multi option question does have legacyQuestionOptions, we can assume there is only one
    // locale and convert the strings to QuestionOption instances each with a single locale.
    Locale firstKey = legacyQuestionOptions.keySet().stream().iterator().next();
    ImmutableList<QuestionOption> options = Streams.mapWithIndex(legacyQuestionOptions.get(firstKey).stream(), (optionText, i) -> QuestionOption.create(Long.valueOf(i), Long.valueOf(i), LocalizedStrings.of(firstKey, optionText))).collect(toImmutableList());
    builder.setQuestionOptions(options);
}
Also used : Locale(java.util.Locale) PrePersist(javax.persistence.PrePersist) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionOption(services.question.QuestionOption) ArrayList(java.util.ArrayList) Table(javax.persistence.Table) ImmutableList(com.google.common.collect.ImmutableList) DbArray(io.ebean.annotation.DbArray) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) DbJsonB(io.ebean.annotation.DbJsonB) Locale(java.util.Locale) PostLoad(javax.persistence.PostLoad) Constraints(play.data.validation.Constraints) ManyToMany(javax.persistence.ManyToMany) Entity(javax.persistence.Entity) JoinTable(javax.persistence.JoinTable) PreUpdate(javax.persistence.PreUpdate) ImmutableMap(com.google.common.collect.ImmutableMap) PostPersist(javax.persistence.PostPersist) QuestionType(services.question.types.QuestionType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) QuestionDefinition(services.question.types.QuestionDefinition) Streams(com.google.common.collect.Streams) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) List(java.util.List) LocalizedStrings(services.LocalizedStrings) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Optional(java.util.Optional) PostUpdate(javax.persistence.PostUpdate) QuestionOption(services.question.QuestionOption)

Example 3 with InvalidQuestionTypeException

use of services.question.exceptions.InvalidQuestionTypeException in project civiform by seattle-uat.

the class AdminQuestionController method create.

/**
 * POST endpoint for creating a new question in the draft version.
 */
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public Result create(Request request, String questionType) {
    QuestionForm questionForm;
    try {
        questionForm = QuestionFormBuilder.createFromRequest(request, formFactory, QuestionType.of(questionType));
    } catch (InvalidQuestionTypeException e) {
        return badRequest(invalidQuestionTypeMessage(questionType));
    }
    QuestionDefinition questionDefinition;
    try {
        questionDefinition = getBuilder(Optional.empty(), questionForm).build();
    } catch (UnsupportedQuestionTypeException e) {
        // Valid question type that is not yet fully supported.
        return badRequest(e.getMessage());
    }
    ErrorAnd<QuestionDefinition, CiviFormError> result = service.create(questionDefinition);
    if (result.isError()) {
        String errorMessage = joinErrors(result.getErrors());
        ReadOnlyQuestionService roService = service.getReadOnlyQuestionService().toCompletableFuture().join();
        ImmutableList<EnumeratorQuestionDefinition> enumeratorQuestionDefinitions = roService.getUpToDateEnumeratorQuestions();
        return ok(editView.renderNewQuestionForm(request, questionForm, enumeratorQuestionDefinitions, errorMessage));
    }
    String successMessage = String.format("question %s created", questionForm.getQuestionName());
    return withMessage(redirect(routes.AdminQuestionController.index()), successMessage);
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) MultiOptionQuestionForm(forms.MultiOptionQuestionForm) QuestionForm(forms.QuestionForm) EnumeratorQuestionForm(forms.EnumeratorQuestionForm) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) CiviFormError(services.CiviFormError) ReadOnlyQuestionService(services.question.ReadOnlyQuestionService) Secure(org.pac4j.play.java.Secure)

Example 4 with InvalidQuestionTypeException

use of services.question.exceptions.InvalidQuestionTypeException in project civiform by seattle-uat.

the class AdminQuestionController method edit.

/**
 * Return a HTML page containing all configurations of a question in the draft version and forms
 * to edit them.
 */
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public CompletionStage<Result> edit(Request request, Long id) {
    return service.getReadOnlyQuestionService().thenApplyAsync(readOnlyService -> {
        QuestionDefinition questionDefinition;
        try {
            questionDefinition = readOnlyService.getQuestionDefinition(id);
        } catch (QuestionNotFoundException e) {
            return badRequest(e.toString());
        }
        Optional<QuestionDefinition> maybeEnumerationQuestion = maybeGetEnumerationQuestion(readOnlyService, questionDefinition);
        try {
            return ok(editView.renderEditQuestionForm(request, questionDefinition, maybeEnumerationQuestion));
        } catch (InvalidQuestionTypeException e) {
            return badRequest(invalidQuestionTypeMessage(questionDefinition.getQuestionType().toString()));
        }
    }, httpExecutionContext.current());
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) Secure(org.pac4j.play.java.Secure)

Example 5 with InvalidQuestionTypeException

use of services.question.exceptions.InvalidQuestionTypeException in project civiform by seattle-uat.

the class AdminQuestionController method show.

/**
 * Return a HTML page displaying all configurations of a question without the ability to update
 * it.
 */
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public CompletionStage<Result> show(long id) {
    return service.getReadOnlyQuestionService().thenApplyAsync(readOnlyService -> {
        QuestionDefinition questionDefinition;
        try {
            questionDefinition = readOnlyService.getQuestionDefinition(id);
        } catch (QuestionNotFoundException e) {
            return badRequest(e.toString());
        }
        Optional<QuestionDefinition> maybeEnumerationQuestion = maybeGetEnumerationQuestion(readOnlyService, questionDefinition);
        try {
            return ok(editView.renderViewQuestionForm(questionDefinition, maybeEnumerationQuestion));
        } catch (InvalidQuestionTypeException e) {
            return badRequest(invalidQuestionTypeMessage(questionDefinition.getQuestionType().toString()));
        }
    }, httpExecutionContext.current());
}
Also used : EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) Secure(org.pac4j.play.java.Secure)

Aggregations

InvalidQuestionTypeException (services.question.exceptions.InvalidQuestionTypeException)8 UnsupportedQuestionTypeException (services.question.exceptions.UnsupportedQuestionTypeException)6 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)6 MultiOptionQuestionDefinition (services.question.types.MultiOptionQuestionDefinition)6 QuestionDefinition (services.question.types.QuestionDefinition)6 Secure (org.pac4j.play.java.Secure)5 QuestionNotFoundException (services.question.exceptions.QuestionNotFoundException)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 EnumeratorQuestionForm (forms.EnumeratorQuestionForm)2 MultiOptionQuestionForm (forms.MultiOptionQuestionForm)2 QuestionForm (forms.QuestionForm)2 CiviFormError (services.CiviFormError)2 QuestionOption (services.question.QuestionOption)2 ReadOnlyQuestionService (services.question.ReadOnlyQuestionService)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1