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