use of services.question.exceptions.InvalidUpdateException 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(), questionForm.getQuestionExportStateTag());
} 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 services.question.exceptions.InvalidUpdateException 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));
}
try {
service.setExportState(result.getResult(), questionForm.getQuestionExportStateTag());
} catch (InvalidUpdateException | QuestionNotFoundException e) {
return badRequest(e.toString());
}
String successMessage = String.format("question %s created", questionForm.getQuestionName());
return withMessage(redirect(routes.AdminQuestionController.index()), successMessage);
}
use of services.question.exceptions.InvalidUpdateException in project civiform by seattle-uat.
the class QuestionServiceImpl method setExportState.
@Override
public void setExportState(QuestionDefinition questionDefinition, QuestionTag questionExportState) throws QuestionNotFoundException, InvalidUpdateException {
Optional<Question> questionMaybe = questionRepository.lookupQuestion(questionDefinition.getId()).toCompletableFuture().join();
if (questionMaybe.isEmpty()) {
throw new QuestionNotFoundException(questionDefinition.getId());
}
Question question = questionMaybe.get();
if (ExporterService.NON_EXPORTED_QUESTION_TYPES.contains(questionDefinition.getQuestionType())) {
question.removeTag(QuestionTag.DEMOGRAPHIC_PII);
question.removeTag(QuestionTag.NON_DEMOGRAPHIC);
question.removeTag(QuestionTag.DEMOGRAPHIC);
}
switch(questionExportState) {
case DEMOGRAPHIC:
question.removeTag(QuestionTag.DEMOGRAPHIC_PII);
question.removeTag(QuestionTag.NON_DEMOGRAPHIC);
question.addTag(QuestionTag.DEMOGRAPHIC);
break;
case DEMOGRAPHIC_PII:
question.removeTag(QuestionTag.DEMOGRAPHIC);
question.removeTag(QuestionTag.NON_DEMOGRAPHIC);
question.addTag(QuestionTag.DEMOGRAPHIC_PII);
break;
case NON_DEMOGRAPHIC:
question.removeTag(QuestionTag.DEMOGRAPHIC_PII);
question.removeTag(QuestionTag.DEMOGRAPHIC);
question.addTag(QuestionTag.NON_DEMOGRAPHIC);
break;
default:
throw new InvalidUpdateException(String.format("Unknown question export state: %s", questionExportState));
}
question.save();
}
use of services.question.exceptions.InvalidUpdateException in project civiform by seattle-uat.
the class AdminQuestionTranslationsController method update.
/**
* Save updates to a question's localizations.
*
* @param request the current {@link Http.Request}
* @param id the ID of the question to update
* @param locale the locale to update, as an ISO language tag
* @return redirects to the admin's home page if updates were successful; otherwise, renders the
* same {@link QuestionTranslationView} with error messages
*/
@Secure(authorizers = Authorizers.Labels.CIVIFORM_ADMIN)
public CompletionStage<Result> update(Http.Request request, long id, String locale) {
Locale updatedLocale = Locale.forLanguageTag(locale);
return questionService.getReadOnlyQuestionService().thenApplyAsync(readOnlyQuestionService -> {
try {
QuestionDefinition toUpdate = readOnlyQuestionService.getQuestionDefinition(id);
QuestionTranslationForm form = buildFormFromRequest(request, toUpdate.getQuestionType());
QuestionDefinition definitionWithUpdates = form.builderWithUpdates(toUpdate, updatedLocale).build();
ErrorAnd<QuestionDefinition, CiviFormError> result = questionService.update(definitionWithUpdates);
if (result.isError()) {
String errorMessage = joinErrors(result.getErrors());
return ok(translationView.renderErrors(request, updatedLocale, definitionWithUpdates, errorMessage));
}
return redirect(routes.AdminQuestionController.index().url());
} catch (QuestionNotFoundException e) {
return notFound(e.getMessage());
} catch (UnsupportedQuestionTypeException e) {
return badRequest(e.getMessage());
} catch (InvalidUpdateException e) {
return internalServerError(e.getMessage());
}
}, httpExecutionContext.current());
}
use of services.question.exceptions.InvalidUpdateException in project civiform by seattle-uat.
the class QuestionServiceImpl method archiveQuestion.
@Override
public void archiveQuestion(Long id) throws InvalidUpdateException {
Optional<Question> question = questionRepository.lookupQuestion(id).toCompletableFuture().join();
if (question.isEmpty()) {
throw new InvalidUpdateException("Did not find question.");
}
Version draftVersion = versionRepositoryProvider.get().getDraftVersion();
if (!draftVersion.addTombstoneForQuestion(question.get())) {
throw new InvalidUpdateException("Already tombstoned.");
}
draftVersion.save();
}
Aggregations