Search in sources :

Example 1 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testSubFormInput.

@Test
public void testSubFormInput() {
    FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
    // Start with empty input
    FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
    // Should see one (empty) sub form record
    FormInputViewModel viewModel = builder.build(inputModel);
    SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(1));
    // We can update this sub record
    FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
    inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
    viewModel = builder.build(inputModel);
    referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(1));
    // Now add a second record
    inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
    viewModel = builder.build(inputModel);
    referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(2));
    // Verify that the transaction is built is correctly
    RecordTransaction tx = viewModel.buildTransaction();
    RecordUpdate[] changes = tx.getChangeArray();
    assertThat(changes.length, equalTo(3));
    RecordUpdate parentChange = changes[0];
    RecordUpdate subFormChange = changes[1];
    assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
    assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 2 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method editRecordWithSubRecords.

@Test
public void editRecordWithSubRecords() {
    IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
    RecordRef rootRecordRef = incidentForm.getRecordRef(0);
    FormStructure structure = fetchStructure(rootRecordRef);
    FormInputViewModelBuilder builder = builderFor(structure);
    FormInputModel inputModel = new FormInputModel(rootRecordRef);
    FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
    SubFormViewModel subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(subFormField.getSubRecords(), hasSize(4));
    // Try deleting the first one...
    RecordRef deletedRef = subFormField.getSubRecords().get(0).getRecordRef();
    inputModel = inputModel.deleteSubRecord(deletedRef);
    viewModel = builder.build(inputModel, structure.getExistingRecord());
    subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(subFormField.getSubRecords(), hasSize(3));
    // Should show up in transaction
    RecordTransaction tx = viewModel.buildTransaction();
    assertThat(tx.getChanges(), hasItem(hasProperty("deleted", equalTo(true))));
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 3 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method hiddenSubForm.

/**
 * Test the ViewModel for when the user does not have access to a referenced sub form.
 */
@Test
public void hiddenSubForm() {
    FormClass parentForm = new FormClass(ResourceId.valueOf("PARENT_FORM"));
    parentForm.addField(ResourceId.valueOf("F1")).setLabel("What is your name?").setType(TextType.SIMPLE);
    parentForm.addField(ResourceId.valueOf("F2")).setLabel("What are your secrets?").setType(new SubFormReferenceType(ResourceId.valueOf("SECRET_FORM")));
    FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProvider() {

        @Override
        public FormMetadata getFormMetadata(ResourceId formId) {
            if (formId.equals(parentForm.getId())) {
                return FormMetadata.of(1, parentForm, FormPermissions.owner());
            } else {
                return FormMetadata.forbidden(formId);
            }
        }
    });
    FormTree formTree = treeBuilder.queryTree(parentForm.getId());
    FormStore formStore = EasyMock.createMock(FormStore.class);
    EasyMock.replay(formStore);
    FormInputViewModelBuilder viewModelBuilder = new FormInputViewModelBuilder(formStore, formTree, new TestingActivePeriodMemory());
    FormInputViewModel viewModel = viewModelBuilder.build(new FormInputModel(new RecordRef(parentForm.getId(), ResourceId.valueOf("R1"))));
}
Also used : FormMetadataProvider(org.activityinfo.model.formTree.FormMetadataProvider) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormMetadata(org.activityinfo.model.form.FormMetadata) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) FormStore(org.activityinfo.ui.client.store.FormStore) Test(org.junit.Test)

Example 4 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method monthlySubForms.

@Test
public void monthlySubForms() {
    ClinicForm clinicForm = setup.getCatalog().getClinicForm();
    ResourceId consultCountFieldId = clinicForm.getSubForm().getConsultsField().getId();
    FormInputViewModelBuilder builder = builderFor(clinicForm);
    ResourceId parentRecordId = ResourceId.generateId();
    FormInputModel inputModel = new FormInputModel(new RecordRef(clinicForm.getFormId(), parentRecordId));
    FormInputViewModel viewModel = builder.build(inputModel);
    // Get the sub form view model.
    // The active ref should be set to 2017-10 by default.
    ResourceId subFormFieldId = clinicForm.getSubFormField().getId();
    SubFormViewModel subForm = viewModel.getSubForm(subFormFieldId);
    assertThat(subForm, notNullValue());
    RecordRef octoberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-10"));
    RecordRef novemberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-11"));
    assertThat(subForm.getActiveRecordRef(), equalTo(octoberId));
    // Update a field in the active form
    inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(33))));
    viewModel = builder.build(inputModel);
    FormInputViewModel subFormViewModel = viewModel.getSubForm(subFormFieldId).getActiveSubViewModel();
    assertThat(subFormViewModel.getField(consultCountFieldId), equalTo(new Quantity(33)));
    assertThat(subFormViewModel.isValid(), equalTo(true));
    // Now change the active record to another month
    inputModel = inputModel.updateActiveSubRecord(subFormFieldId, novemberId);
    viewModel = builder.build(inputModel);
    subForm = viewModel.getSubForm(subFormFieldId);
    // At this point, the subform is not valid because required fields are not filled in,
    // but it is also not dirty because we have not changed anything
    assertThat(subForm.getActiveSubViewModel().isValid(), equalTo(false));
    assertThat(subForm.getActiveSubViewModel().isDirty(), equalTo(false));
    assertThat(subForm.getActiveSubViewModel().isPlaceholder(), equalTo(true));
    // Now fill in the required field
    inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(44))));
    viewModel = builder.build(inputModel);
    assertThat(viewModel.getSubForm(subFormFieldId).getActiveRecordRef(), equalTo(novemberId));
    assertThat(viewModel.getSubForm(subFormFieldId).isValid(), equalTo(true));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef) Quantity(org.activityinfo.model.type.number.Quantity) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) Test(org.junit.Test)

Example 5 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testPersistence.

@Test
public void testPersistence() {
    Survey survey = setup.getCatalog().getSurvey();
    FormInputViewModelBuilder builder = builderFor(survey);
    // Start with no input
    FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId())).update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getMaleId()))).update(survey.getNameFieldId(), new FieldInput(TextValue.valueOf("BOB"))).update(survey.getDobFieldId(), new FieldInput(new LocalDate(1982, 1, 16))).update(survey.getAgeFieldId(), new FieldInput(new Quantity(35)));
    // Verify that it's valid
    FormInputViewModel viewModel = builder.build(inputModel);
    assertThat(viewModel.isValid(), equalTo(true));
    // Now build the update transaction and save!
    Promise<Void> completed = setup.getFormStore().updateRecords(viewModel.buildTransaction());
    assertThat(completed.getState(), equalTo(Promise.State.FULFILLED));
}
Also used : EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) Quantity(org.activityinfo.model.type.number.Quantity) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)15 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)15 Test (org.junit.Test)13 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)6 ResourceId (org.activityinfo.model.resource.ResourceId)4 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)4 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 LocalDate (org.activityinfo.model.type.time.LocalDate)3 TestingFormStore (org.activityinfo.ui.client.store.TestingFormStore)3 FormTree (org.activityinfo.model.formTree.FormTree)2 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 RecordTree (org.activityinfo.model.formTree.RecordTree)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 Quantity (org.activityinfo.model.type.number.Quantity)2 FormStore (org.activityinfo.ui.client.store.FormStore)2 Predicate (com.google.common.base.Predicate)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 Collections.emptyMap (java.util.Collections.emptyMap)1