Search in sources :

Example 1 with RepeatedEntity

use of services.applicant.RepeatedEntity in project civiform by seattle-uat.

the class ApplicantQuestionTest method questionTextAndHelpText_areContextualizedByRepeatedEntity.

@Test
public void questionTextAndHelpText_areContextualizedByRepeatedEntity() {
    ApplicantData applicantData = new ApplicantData();
    Path householdMembersPath = ApplicantData.APPLICANT_PATH.join(testQuestionBank.applicantHouseholdMembers().getQuestionDefinition().getQuestionPathSegment());
    QuestionAnswerer.answerEnumeratorQuestion(applicantData, householdMembersPath, ImmutableList.of("Jonathan"));
    Path householdMembersJobsPath = householdMembersPath.atIndex(0).join(testQuestionBank.applicantHouseholdMemberJobs().getQuestionDefinition().getQuestionPathSegment());
    QuestionAnswerer.answerEnumeratorQuestion(applicantData, householdMembersJobsPath, ImmutableList.of("JonCo"));
    RepeatedEntity jonathan = RepeatedEntity.createRepeatedEntities((EnumeratorQuestionDefinition) testQuestionBank.applicantHouseholdMembers().getQuestionDefinition(), applicantData).get(0);
    RepeatedEntity jonCo = jonathan.createNestedRepeatedEntities((EnumeratorQuestionDefinition) testQuestionBank.applicantHouseholdMemberJobs().getQuestionDefinition(), applicantData).get(0);
    ApplicantQuestion applicantQuestion = new ApplicantQuestion(testQuestionBank.applicantHouseholdMemberDaysWorked().getQuestionDefinition(), applicantData, Optional.of(jonCo));
    assertThat(applicantQuestion.getQuestionText()).isEqualTo("How many days has Jonathan worked at JonCo?");
    assertThat(applicantQuestion.getQuestionHelpText()).isEqualTo("How many days has Jonathan worked at JonCo?");
}
Also used : Path(services.Path) RepeatedEntity(services.applicant.RepeatedEntity) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) ApplicantData(services.applicant.ApplicantData) Test(org.junit.Test)

Example 2 with RepeatedEntity

use of services.applicant.RepeatedEntity in project civiform by seattle-uat.

the class JsonPathPredicateGeneratorTest method fromLeafNode_predicateBasedOnSiblingRepeatedQuestion_generatesCorrectPath.

@Test
public void fromLeafNode_predicateBasedOnSiblingRepeatedQuestion_generatesCorrectPath() throws Exception {
    ApplicantData applicantData = new ApplicantData();
    EnumeratorQuestionDefinition enumerator = (EnumeratorQuestionDefinition) questionBank.applicantHouseholdMembers().getQuestionDefinition();
    QuestionDefinition siblingQuestion = new QuestionDefinitionBuilder(questionBank.applicantHouseholdMemberName().getQuestionDefinition()).setEnumeratorId(Optional.of(enumerator.getId())).build();
    // Put an entity at the enumerator path so we can generate repeated contexts.
    ApplicantQuestion applicantEnumerator = new ApplicantQuestion(enumerator, applicantData, Optional.empty());
    applicantData.putRepeatedEntities(applicantEnumerator.getContextualizedPath(), ImmutableList.of("Bernard", "Alice"));
    // Just use a repeated entity for the first (index 0) entity.
    ImmutableList<RepeatedEntity> repeatedEntities = RepeatedEntity.createRepeatedEntities(enumerator, applicantData);
    Optional<RepeatedEntity> repeatedEntity = repeatedEntities.stream().findFirst();
    generator = new JsonPathPredicateGenerator(ImmutableList.of(enumerator, siblingQuestion), repeatedEntity);
    LeafOperationExpressionNode node = LeafOperationExpressionNode.create(// The predicate is based on the sibling "name" question.
    siblingQuestion.getId(), Scalar.FIRST_NAME, Operator.EQUAL_TO, PredicateValue.of("Bernard"));
    assertThat(generator.fromLeafNode(node)).isEqualTo(JsonPathPredicate.create("$.applicant.applicant_household_members[0].household_members_name" + "[?(@.first_name == \"Bernard\")]"));
}
Also used : RepeatedEntity(services.applicant.RepeatedEntity) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) ApplicantData(services.applicant.ApplicantData) LeafOperationExpressionNode(services.program.predicate.LeafOperationExpressionNode) ApplicantQuestion(services.applicant.question.ApplicantQuestion) Test(org.junit.Test)

Example 3 with RepeatedEntity

use of services.applicant.RepeatedEntity in project civiform by seattle-uat.

the class ApplicantProgramSummaryView method render.

/**
 * Renders a summary of all of the applicant's data for a specific application. Data includes:
 *
 * <p>Program Id, Applicant Id - Needed for link context (submit & edit)
 *
 * <p>Question Data for each question:
 *
 * <ul>
 *   <li>question text
 *   <li>answer text
 *   <li>block id (for edit link)
 * </ul>
 */
public Content render(Params params) {
    Messages messages = params.messages();
    String pageTitle = params.inReview() ? messages.at(MessageKey.TITLE_PROGRAM_REVIEW.getKeyName()) : messages.at(MessageKey.TITLE_PROGRAM_PREVIEW.getKeyName());
    HtmlBundle bundle = layout.getBundle().setTitle(String.format("%s — %s", pageTitle, params.programTitle()));
    ContainerTag applicationSummary = div().withId("application-summary").withClasses(Styles.MB_8);
    Optional<RepeatedEntity> previousRepeatedEntity = Optional.empty();
    boolean isFirstUnanswered = true;
    for (AnswerData answerData : params.summaryData()) {
        Optional<RepeatedEntity> currentRepeatedEntity = answerData.repeatedEntity();
        if (!currentRepeatedEntity.equals(previousRepeatedEntity) && currentRepeatedEntity.isPresent()) {
            applicationSummary.with(renderRepeatedEntitySection(currentRepeatedEntity.get(), messages));
        }
        applicationSummary.with(renderQuestionSummary(answerData, messages, params.applicantId(), params.inReview(), isFirstUnanswered));
        isFirstUnanswered = isFirstUnanswered && answerData.isAnswered();
        previousRepeatedEntity = currentRepeatedEntity;
    }
    // Add submit action (POST).
    String submitLink = routes.ApplicantProgramReviewController.submit(params.applicantId(), params.programId()).url();
    Tag continueOrSubmitButton;
    if (params.completedBlockCount() == params.totalBlockCount()) {
        continueOrSubmitButton = submitButton(messages.at(MessageKey.BUTTON_SUBMIT.getKeyName())).withClasses(ReferenceClasses.SUBMIT_BUTTON, ApplicantStyles.BUTTON_SUBMIT_APPLICATION);
    } else {
        String applyUrl = routes.ApplicantProgramsController.edit(params.applicantId(), params.programId()).url();
        continueOrSubmitButton = a().attr(HREF, applyUrl).withText(messages.at(MessageKey.BUTTON_CONTINUE.getKeyName())).withId("continue-application-button").withClasses(ReferenceClasses.CONTINUE_BUTTON, ApplicantStyles.BUTTON_SUBMIT_APPLICATION);
    }
    ContainerTag content = div().with(applicationSummary).with(form().withAction(submitLink).withMethod(Http.HttpVerbs.POST).with(makeCsrfTokenInputTag(params.request())).with(continueOrSubmitButton));
    if (!params.banner().isEmpty()) {
        bundle.addToastMessages(ToastMessage.error(params.banner()));
    }
    bundle.addMainContent(layout.renderProgramApplicationTitleAndProgressIndicator(params.programTitle(), params.completedBlockCount(), params.totalBlockCount(), true), h1(pageTitle).withClasses(ApplicantStyles.H1_PROGRAM_APPLICATION), content);
    bundle.addMainStyles(ApplicantStyles.MAIN_PROGRAM_APPLICATION);
    return layout.renderWithNav(params.request(), params.applicantName(), params.messages(), bundle);
}
Also used : RepeatedEntity(services.applicant.RepeatedEntity) HtmlBundle(views.HtmlBundle) AnswerData(services.applicant.AnswerData) Messages(play.i18n.Messages) ContainerTag(j2html.tags.ContainerTag) ContainerTag(j2html.tags.ContainerTag) Tag(j2html.tags.Tag)

Example 4 with RepeatedEntity

use of services.applicant.RepeatedEntity in project civiform by seattle-uat.

the class JsonPathPredicateGeneratorTest method fromLeafNode_twoLevelsDeepRepeater_generatesCorrectPath.

@Test
public void fromLeafNode_twoLevelsDeepRepeater_generatesCorrectPath() throws Exception {
    ApplicantData applicantData = new ApplicantData();
    // household members
    // \_ name (target), jobs
    // \_ days worked (current block)
    QuestionDefinition topLevelEnumerator = questionBank.applicantHouseholdMembers().getQuestionDefinition();
    QuestionDefinition targetQuestion = questionBank.applicantHouseholdMemberName().getQuestionDefinition();
    QuestionDefinition nestedEnumerator = questionBank.applicantHouseholdMemberJobs().getQuestionDefinition();
    QuestionDefinition currentQuestion = questionBank.applicantHouseholdMemberDaysWorked().getQuestionDefinition();
    // Put an entity at the enumerator path so we can generate repeated contexts.
    ApplicantQuestion applicantEnumerator = new ApplicantQuestion(topLevelEnumerator, applicantData, Optional.empty());
    applicantData.putRepeatedEntities(applicantEnumerator.getContextualizedPath(), ImmutableList.of("Bernard", "Alice"));
    // Context for index 1 ('Alice')
    Optional<RepeatedEntity> topLevelRepeatedEntity = RepeatedEntity.createRepeatedEntities((EnumeratorQuestionDefinition) topLevelEnumerator, applicantData).reverse().stream().findFirst();
    // Create entities for the nested enumerator
    ApplicantQuestion nestedApplicantEnumerator = new ApplicantQuestion(nestedEnumerator, applicantData, topLevelRepeatedEntity);
    applicantData.putRepeatedEntities(nestedApplicantEnumerator.getContextualizedPath(), ImmutableList.of("Software Engineer"));
    Optional<RepeatedEntity> currentContext = topLevelRepeatedEntity.get().createNestedRepeatedEntities((EnumeratorQuestionDefinition) nestedEnumerator, applicantData).stream().findFirst();
    generator = new JsonPathPredicateGenerator(ImmutableList.of(topLevelEnumerator, nestedEnumerator, targetQuestion, currentQuestion), currentContext);
    LeafOperationExpressionNode node = LeafOperationExpressionNode.create(// The predicate is based on the "name" question.
    targetQuestion.getId(), Scalar.FIRST_NAME, Operator.EQUAL_TO, PredicateValue.of("Alice"));
    assertThat(generator.fromLeafNode(node)).isEqualTo(JsonPathPredicate.create("$.applicant.applicant_household_members[1].household_members_name" + "[?(@.first_name == \"Alice\")]"));
}
Also used : RepeatedEntity(services.applicant.RepeatedEntity) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) ApplicantData(services.applicant.ApplicantData) LeafOperationExpressionNode(services.program.predicate.LeafOperationExpressionNode) ApplicantQuestion(services.applicant.question.ApplicantQuestion) Test(org.junit.Test)

Example 5 with RepeatedEntity

use of services.applicant.RepeatedEntity in project civiform by seattle-uat.

the class JsonPathPredicateGeneratorTest method fromLeafNode_targetQuestionNotAncestorOfCurrentContext_throwsException.

@Test
public void fromLeafNode_targetQuestionNotAncestorOfCurrentContext_throwsException() throws Exception {
    ApplicantData applicantData = new ApplicantData();
    QuestionDefinition topLevelEnumerator = questionBank.applicantHouseholdMembers().getQuestionDefinition();
    QuestionDefinition targetQuestion = new QuestionDefinitionBuilder(questionBank.applicantHouseholdMemberName().getQuestionDefinition()).setEnumeratorId(Optional.of(12345L)).build();
    QuestionDefinition currentQuestion = questionBank.applicantHouseholdMemberJobs().getQuestionDefinition();
    // Put an entity at the enumerator path so we can generate repeated contexts.
    ApplicantQuestion applicantEnumerator = new ApplicantQuestion(topLevelEnumerator, applicantData, Optional.empty());
    applicantData.putRepeatedEntities(applicantEnumerator.getContextualizedPath(), ImmutableList.of("Alice"));
    // Context for 'Alice'
    Optional<RepeatedEntity> currentContext = RepeatedEntity.createRepeatedEntities((EnumeratorQuestionDefinition) topLevelEnumerator, applicantData).reverse().stream().findFirst();
    generator = new JsonPathPredicateGenerator(ImmutableList.of(targetQuestion, currentQuestion), currentContext);
    LeafOperationExpressionNode node = LeafOperationExpressionNode.create(// The predicate is based on the "name" question.
    targetQuestion.getId(), Scalar.FIRST_NAME, Operator.EQUAL_TO, PredicateValue.of("Alice"));
    assertThatThrownBy(() -> generator.fromLeafNode(node)).isInstanceOf(InvalidPredicateException.class).hasMessageContaining("Enumerator 12345 is not an ancestor of the current repeated context");
}
Also used : RepeatedEntity(services.applicant.RepeatedEntity) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) QuestionDefinition(services.question.types.QuestionDefinition) InvalidPredicateException(services.applicant.exception.InvalidPredicateException) QuestionDefinitionBuilder(services.question.types.QuestionDefinitionBuilder) EnumeratorQuestionDefinition(services.question.types.EnumeratorQuestionDefinition) ApplicantData(services.applicant.ApplicantData) LeafOperationExpressionNode(services.program.predicate.LeafOperationExpressionNode) ApplicantQuestion(services.applicant.question.ApplicantQuestion) Test(org.junit.Test)

Aggregations

RepeatedEntity (services.applicant.RepeatedEntity)8 Test (org.junit.Test)5 ApplicantData (services.applicant.ApplicantData)5 EnumeratorQuestionDefinition (services.question.types.EnumeratorQuestionDefinition)5 QuestionDefinition (services.question.types.QuestionDefinition)5 ApplicantQuestion (services.applicant.question.ApplicantQuestion)4 LeafOperationExpressionNode (services.program.predicate.LeafOperationExpressionNode)4 QuestionDefinitionBuilder (services.question.types.QuestionDefinitionBuilder)3 ContainerTag (j2html.tags.ContainerTag)2 Path (services.Path)2 InvalidPredicateException (services.applicant.exception.InvalidPredicateException)2 Tag (j2html.tags.Tag)1 LocalDate (java.time.LocalDate)1 Messages (play.i18n.Messages)1 AnswerData (services.applicant.AnswerData)1 HtmlBundle (views.HtmlBundle)1 LinkElement (views.components.LinkElement)1