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);
}
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));
}
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());
}
Aggregations