Search in sources :

Example 1 with InvalidUpdateException

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);
}
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 2 with InvalidUpdateException

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);
}
Also used : InvalidQuestionTypeException(services.question.exceptions.InvalidQuestionTypeException) MultiOptionQuestionForm(forms.MultiOptionQuestionForm) QuestionForm(forms.QuestionForm) EnumeratorQuestionForm(forms.EnumeratorQuestionForm) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) ReadOnlyQuestionService(services.question.ReadOnlyQuestionService) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) MultiOptionQuestionDefinition(services.question.types.MultiOptionQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) CiviFormError(services.CiviFormError) InvalidUpdateException(services.question.exceptions.InvalidUpdateException) Secure(org.pac4j.play.java.Secure)

Example 3 with InvalidUpdateException

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();
}
Also used : QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) Question(models.Question) InvalidUpdateException(services.question.exceptions.InvalidUpdateException)

Example 4 with InvalidUpdateException

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());
}
Also used : Locale(java.util.Locale) QuestionDefinition(services.question.types.QuestionDefinition) EnumeratorQuestionTranslationForm(forms.translation.EnumeratorQuestionTranslationForm) QuestionTranslationForm(forms.translation.QuestionTranslationForm) MultiOptionQuestionTranslationForm(forms.translation.MultiOptionQuestionTranslationForm) QuestionNotFoundException(services.question.exceptions.QuestionNotFoundException) CiviFormError(services.CiviFormError) UnsupportedQuestionTypeException(services.question.exceptions.UnsupportedQuestionTypeException) InvalidUpdateException(services.question.exceptions.InvalidUpdateException) Secure(org.pac4j.play.java.Secure)

Example 5 with InvalidUpdateException

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();
}
Also used : Version(models.Version) Question(models.Question) InvalidUpdateException(services.question.exceptions.InvalidUpdateException)

Aggregations

InvalidUpdateException (services.question.exceptions.InvalidUpdateException)8 Question (models.Question)5 CiviFormError (services.CiviFormError)4 QuestionNotFoundException (services.question.exceptions.QuestionNotFoundException)4 Version (models.Version)3 Secure (org.pac4j.play.java.Secure)3 UnsupportedQuestionTypeException (services.question.exceptions.UnsupportedQuestionTypeException)3 QuestionDefinition (services.question.types.QuestionDefinition)3 EnumeratorQuestionForm (forms.EnumeratorQuestionForm)2 MultiOptionQuestionForm (forms.MultiOptionQuestionForm)2 QuestionForm (forms.QuestionForm)2 ReadOnlyQuestionService (services.question.ReadOnlyQuestionService)2 InvalidQuestionTypeException (services.question.exceptions.InvalidQuestionTypeException)2 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)2 MultiOptionQuestionDefinition (services.question.types.MultiOptionQuestionDefinition)2 EnumeratorQuestionTranslationForm (forms.translation.EnumeratorQuestionTranslationForm)1 MultiOptionQuestionTranslationForm (forms.translation.MultiOptionQuestionTranslationForm)1 QuestionTranslationForm (forms.translation.QuestionTranslationForm)1 Locale (java.util.Locale)1