Search in sources :

Example 6 with RecordRef

use of org.activityinfo.model.type.RecordRef 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 7 with RecordRef

use of org.activityinfo.model.type.RecordRef 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 8 with RecordRef

use of org.activityinfo.model.type.RecordRef 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 9 with RecordRef

use of org.activityinfo.model.type.RecordRef 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)

Example 10 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testMultipleSelectPersistence.

@Test
public void testMultipleSelectPersistence() {
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    FormInputViewModelBuilder builder = builderFor(intakeForm);
    FormInputModel inputModel = new FormInputModel(new RecordRef(intakeForm.getFormId(), ResourceId.generateId())).update(intakeForm.getOpenDateFieldId(), new LocalDate(2017, 1, 1)).update(intakeForm.getNationalityFieldId(), new EnumValue(intakeForm.getPalestinianId(), intakeForm.getJordanianId()));
    FormInputViewModel viewModel = builder.build(inputModel);
    assertThat(viewModel.isValid(), equalTo(true));
    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) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)66 Test (org.junit.Test)31 ResourceId (org.activityinfo.model.resource.ResourceId)26 ReferenceValue (org.activityinfo.model.type.ReferenceValue)26 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)14 FormInstance (org.activityinfo.model.form.FormInstance)7 FormTree (org.activityinfo.model.formTree.FormTree)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 Optional (com.google.common.base.Optional)5 FieldValue (org.activityinfo.model.type.FieldValue)5 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)5 FormClass (org.activityinfo.model.form.FormClass)4 ResultSet (java.sql.ResultSet)3 FormField (org.activityinfo.model.form.FormField)3 RecordTree (org.activityinfo.model.formTree.RecordTree)3 ColumnSet (org.activityinfo.model.query.ColumnSet)3 ColumnView (org.activityinfo.model.query.ColumnView)3 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3