Search in sources :

Example 26 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class CodeEnumFieldValueParser method parse.

@Override
public FieldValue parse(String text) {
    if (text == null) {
        throw new IllegalArgumentException("Malformed Element passed to OdkFieldValueParser.parse()");
    }
    String[] selected = text.split(" ");
    List<ResourceId> resourceIds = Lists.newArrayListWithCapacity(selected.length);
    for (String item : selected) {
        ResourceId resourceId = values.get(item);
        if (resourceId != null)
            resourceIds.add(resourceId);
    }
    return new EnumValue(resourceIds);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) EnumValue(org.activityinfo.model.type.enumerated.EnumValue)

Example 27 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testSurveyRelevance.

@Test
public void testSurveyRelevance() {
    Survey survey = setup.getCatalog().getSurvey();
    FormInputViewModelBuilder builder = builderFor(survey);
    // Start with no input
    FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
    // Is this valid?
    FormInputViewModel viewModel = builder.build(inputModel);
    // Fields with invalid relevance are considered relevant
    assertTrue("field with bad relevance is relevant", viewModel.isRelevant(survey.getAgeFieldId()));
    assertThat("pregnant is not yet relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
    assertThat("prenatale care is not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
    assertThat("form is valid", viewModel.isValid(), equalTo(false));
    // Answer the gender
    inputModel = inputModel.update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getFemaleId())));
    viewModel = builder.build(inputModel);
    assertThat("pregnant is now relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
    assertThat("prenatale care is still not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
    // Answer pregnant = yes
    inputModel = inputModel.update(survey.getPregnantFieldId(), new FieldInput(new EnumValue(survey.getPregnantId())));
    viewModel = builder.build(inputModel);
    assertThat("pregnant is still relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
    assertThat("prenatale is now relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(true));
    // Change gender = Male
    inputModel = inputModel.update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getMaleId())));
    viewModel = builder.build(inputModel);
    assertThat("pregnant is not relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
    assertThat("prenatale is not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
}
Also used : EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) Test(org.junit.Test)

Example 28 with EnumValue

use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testNewlyIrrelvantFieldSetToEmpty.

@Test
public void testNewlyIrrelvantFieldSetToEmpty() {
    TestingFormStore store = new TestingFormStore();
    Survey survey = store.getCatalog().getSurvey();
    RecordRef recordRef = survey.getRecordRef(8);
    FormStructure structure = fetchStructure(recordRef);
    FormInputViewModelBuilder builder = new FormInputViewModelBuilder(store, structure.getFormTree(), new TestingActivePeriodMemory());
    FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
    // The record was saved as GENDER=Female, and PREGNANT=No
    FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
    assertThat(viewModel.getField(survey.getGenderFieldId()), equalTo(new EnumValue(survey.getFemaleId())));
    assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
    assertThat(viewModel.getField(survey.getPregnantFieldId()), equalTo(new EnumValue(survey.getPregnantNo())));
    // When we change the Gender to Male, then PREGNANT should be set to empty
    inputModel = inputModel.update(survey.getGenderFieldId(), new EnumValue(survey.getMaleId()));
    viewModel = builder.build(inputModel, structure.getExistingRecord());
    assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
    RecordTransaction tx = viewModel.buildTransaction();
    assertThat(tx.getChangeArray(), arrayWithSize(1));
    RecordUpdate update = tx.getChanges().iterator().next();
    assertTrue(update.getFields().get(survey.getPregnantFieldId().asString()).isJsonNull());
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Aggregations

EnumValue (org.activityinfo.model.type.enumerated.EnumValue)28 Test (org.junit.Test)13 ResourceId (org.activityinfo.model.resource.ResourceId)11 FormInstance (org.activityinfo.model.form.FormInstance)6 RecordRef (org.activityinfo.model.type.RecordRef)6 LocalDate (org.activityinfo.model.type.time.LocalDate)6 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)5 FieldValue (org.activityinfo.model.type.FieldValue)4 EnumType (org.activityinfo.model.type.enumerated.EnumType)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)4 FormClass (org.activityinfo.model.form.FormClass)3 Quantity (org.activityinfo.model.type.number.Quantity)3 CheckBox (com.sencha.gxt.widget.core.client.form.CheckBox)2 FormRecord (org.activityinfo.model.form.FormRecord)2 ReferenceValue (org.activityinfo.model.type.ReferenceValue)2 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1