Search in sources :

Example 1 with QuestionForm

use of forms.QuestionForm 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 2 with QuestionForm

use of forms.QuestionForm in project civiform by seattle-uat.

the class AdminQuestionController method update.

/**
 * POST endpoint for updating a question in the draft version.
 */
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public Result update(Request request, Long id, String questionType) {
    QuestionForm questionForm;
    try {
        questionForm = QuestionFormBuilder.createFromRequest(request, formFactory, QuestionType.of(questionType));
    } catch (InvalidQuestionTypeException e) {
        return badRequest(invalidQuestionTypeMessage(questionType));
    }
    ReadOnlyQuestionService roService = service.getReadOnlyQuestionService().toCompletableFuture().join();
    Optional<QuestionDefinition> maybeExisting;
    try {
        maybeExisting = Optional.of(roService.getQuestionDefinition(id));
    } catch (QuestionNotFoundException e) {
        maybeExisting = Optional.empty();
    }
    QuestionDefinition questionDefinition;
    try {
        questionDefinition = getBuilder(maybeExisting, questionForm).setId(id).build();
    } catch (UnsupportedQuestionTypeException e) {
        // type
        return badRequest(e.getMessage());
    }
    ErrorAnd<QuestionDefinition, CiviFormError> errorAndUpdatedQuestionDefinition;
    try {
        errorAndUpdatedQuestionDefinition = service.update(questionDefinition);
    } catch (InvalidUpdateException e) {
        // Ill-formed update request.
        return badRequest(e.toString());
    }
    if (errorAndUpdatedQuestionDefinition.isError()) {
        String errorMessage = joinErrors(errorAndUpdatedQuestionDefinition.getErrors());
        Optional<QuestionDefinition> maybeEnumerationQuestion = maybeGetEnumerationQuestion(roService, questionDefinition);
        return ok(editView.renderEditQuestionForm(request, id, questionForm, maybeEnumerationQuestion, errorMessage));
    }
    try {
        service.setExportState(errorAndUpdatedQuestionDefinition.getResult(), QuestionTag.valueOf(questionForm.getQuestionExportState()));
    } catch (InvalidUpdateException | QuestionNotFoundException e) {
        return badRequest(e.toString());
    }
    String successMessage = String.format("question %s updated", 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) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) MultiOptionQuestionForm(forms.MultiOptionQuestionForm) QuestionForm(forms.QuestionForm) EnumeratorQuestionForm(forms.EnumeratorQuestionForm) ReadOnlyQuestionService(services.question.ReadOnlyQuestionService) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) CiviFormError(services.CiviFormError) InvalidUpdateException(services.question.exceptions.InvalidUpdateException) Secure(org.pac4j.play.java.Secure)

Example 3 with QuestionForm

use of forms.QuestionForm in project civiform by seattle-uat.

the class QuestionEditView method renderViewQuestionForm.

/**
 * Render a read-only non-submittable question form.
 */
public Content renderViewQuestionForm(QuestionDefinition questionDefinition, Optional<QuestionDefinition> maybeEnumerationQuestionDefinition) throws InvalidQuestionTypeException {
    QuestionForm questionForm = QuestionFormBuilder.create(questionDefinition);
    QuestionType questionType = questionForm.getQuestionType();
    String title = String.format("View %s question", questionType.toString().toLowerCase());
    SelectWithLabel enumeratorOption = enumeratorOptionsFromMaybeEnumerationQuestionDefinition(maybeEnumerationQuestionDefinition);
    ContainerTag formContent = buildQuestionContainer(title, QuestionFormBuilder.create(questionDefinition)).with(buildViewOnlyQuestionForm(questionForm, enumeratorOption));
    return renderWithPreview(formContent, questionType, title);
}
Also used : MultiOptionQuestionForm(forms.MultiOptionQuestionForm) QuestionForm(forms.QuestionForm) QuestionType(services.question.types.QuestionType) ContainerTag(j2html.tags.ContainerTag) SelectWithLabel(views.components.SelectWithLabel)

Aggregations

MultiOptionQuestionForm (forms.MultiOptionQuestionForm)3 QuestionForm (forms.QuestionForm)3 EnumeratorQuestionForm (forms.EnumeratorQuestionForm)2 Secure (org.pac4j.play.java.Secure)2 CiviFormError (services.CiviFormError)2 ReadOnlyQuestionService (services.question.ReadOnlyQuestionService)2 InvalidQuestionTypeException (services.question.exceptions.InvalidQuestionTypeException)2 UnsupportedQuestionTypeException (services.question.exceptions.UnsupportedQuestionTypeException)2 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)2 MultiOptionQuestionDefinition (services.question.types.MultiOptionQuestionDefinition)2 QuestionDefinition (services.question.types.QuestionDefinition)2 ContainerTag (j2html.tags.ContainerTag)1 InvalidUpdateException (services.question.exceptions.InvalidUpdateException)1 QuestionNotFoundException (services.question.exceptions.QuestionNotFoundException)1 QuestionType (services.question.types.QuestionType)1 SelectWithLabel (views.components.SelectWithLabel)1