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