use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.
the class LookupKeySetTest method labelTest.
@Test
public void labelTest() {
NfiForm nfiForm = setup.getNfiForm();
VillageForm villageForm = setup.getVillageForm();
AdminLevelForm territoryForm = villageForm.getParentForm();
AdminLevelForm provinceForm = territoryForm.getParentForm().get();
Observable<Maybe<RecordTree>> nfiRecordTree = setup.getFormStore().getRecordTree(nfiForm.getRecordRef(0));
Connection<Maybe<RecordTree>> nfiRecordTreeView = setup.connect(nfiRecordTree);
RecordTree tree = nfiRecordTreeView.assertLoaded().get();
LookupKeySet lookupKeySet = new LookupKeySet(tree.getFormTree(), tree.getFormTree().getRootField(nfiForm.getVillageField().getId()).getField());
assertThat(lookupKeySet.getLookupKeys().size(), equalTo(3));
ReferenceValue referenceValue = (ReferenceValue) tree.getRoot().get(nfiForm.getVillageField().getId());
RecordRef villageRef = referenceValue.getOnlyReference();
RecordTree villageTree = tree.subTree(villageRef);
referenceValue = (ReferenceValue) villageTree.getRoot().get(villageForm.getAdminFieldId());
RecordRef territoryRef = referenceValue.getOnlyReference();
RecordTree territoryTree = villageTree.subTree(territoryRef);
referenceValue = (ReferenceValue) territoryTree.getRoot().get(territoryForm.getParentFieldId());
RecordRef provinceRef = referenceValue.getOnlyReference();
Maybe<String> villageLabel = lookupKeySet.label(tree, villageRef);
Maybe<String> territoryLabel = lookupKeySet.label(tree, territoryRef);
Maybe<String> provinceLabel = lookupKeySet.label(tree, provinceRef);
assertThat(villageLabel, equalTo(Maybe.of("Village 660")));
assertThat(territoryLabel, equalTo(Maybe.of("Territory 85")));
assertThat(provinceLabel, equalTo(Maybe.of("Province 11")));
}
use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.
the class RecordTreeLoaderTest method references.
@Test
public void references() {
BioDataForm bioDataForm = setup.getBioDataForm();
Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(bioDataForm.getRecordRef(0));
Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
RecordTree tree = recordTreeView.assertLoaded().get();
LookupKeySet lookupKeySet = new LookupKeySet(tree.getFormTree(), tree.getFormTree().getRootField(BioDataForm.PROTECTION_CODE_FIELD_ID).getField());
assertThat(lookupKeySet.getLookupKeys(), hasSize(1));
ReferenceValue referenceValue = (ReferenceValue) tree.getRoot().get(BioDataForm.PROTECTION_CODE_FIELD_ID);
RecordRef ref = referenceValue.getOnlyReference();
assertThat(lookupKeySet.label(tree, ref), equalTo(Maybe.of("00667")));
}
use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.
the class FormInputViewModelBuilder method build.
public FormInputViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingRecord, boolean placeholder) {
// Combine the original values of the record with the newly entered data
Map<ResourceId, FieldValue> existingValues = existingRecord.transform(r -> r.getRoot().getFieldValueMap()).or(emptyMap());
FormInstance record = computeUpdatedRecord(existingValues, inputModel);
// Now construct the viewModel that includes everything about
// the current state of data entry.
FormInputViewModel viewModel = new FormInputViewModel();
viewModel.formTree = this.formTree;
viewModel.inputModel = inputModel;
viewModel.fieldValueMap = record.getFieldValueMap();
viewModel.subFormMap = computeSubViewModels(inputModel, existingRecord);
viewModel.existingValues = existingValues;
viewModel.placeholder = placeholder;
viewModel.relevant = computeRelevance(record);
viewModel.missing = computeMissing(record, viewModel.relevant);
viewModel.validationErrors = validateFieldValues(record);
viewModel.dirty = computeDirty(placeholder, existingValues, record);
viewModel.locked = checkLocks(record);
viewModel.valid = allInputValid(inputModel) && viewModel.missing.isEmpty() && viewModel.validationErrors.isEmpty() && viewModel.subFormMap.values().stream().allMatch(SubFormViewModel::isValid);
LOGGER.info("Valid = " + viewModel.valid);
return viewModel;
}
use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.
the class SubFormViewModelBuilder method build.
public SubFormViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingParentRecord) {
List<FormInputViewModel> subRecordViews = new ArrayList<>();
// First do existing records
Set<RecordRef> existingRefs = new HashSet<>();
List<RecordTree> existingSubTrees;
if (existingParentRecord.isVisible()) {
existingSubTrees = existingParentRecord.get().buildSubTrees(inputModel.getRecordRef(), subTree);
} else {
existingSubTrees = Collections.emptyList();
}
Set<RecordRef> deletedRecords = new HashSet<>();
for (RecordTree existingSubRecord : existingSubTrees) {
RecordRef ref = existingSubRecord.getRootRef();
if (inputModel.isDeleted(ref)) {
deletedRecords.add(ref);
} else {
FormInputModel subInput = inputModel.getSubRecord(ref).orElse(new FormInputModel(ref));
FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.of(existingSubRecord));
existingRefs.add(ref);
subRecordViews.add(subViewModel);
}
}
// Now add sub records newly added by the user
for (FormInputModel subInput : inputModel.getSubRecords()) {
if (!existingRefs.contains(subInput.getRecordRef())) {
if (subInput.getRecordRef().getFormId().equals(subFormId)) {
FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.notFound());
subRecordViews.add(subViewModel);
}
}
}
if (keyField.isPresent()) {
// Keyed/Period subforms have a single active record
RecordRef activeRecord = computeActiveSubRecord(inputModel.getRecordRef(), inputModel);
FormInputViewModel activeRecordViewModel = find(activeRecord, subRecordViews);
return new SubFormViewModel(fieldId, subFormKind, subRecordViews, activeRecordViewModel, deletedRecords);
} else {
if (subRecordViews.isEmpty()) {
RecordRef placeholderId = placeholderRecordRef();
if (!inputModel.isDeleted(placeholderId)) {
subRecordViews.add(formBuilder.placeholder(placeholderId));
}
}
return new SubFormViewModel(fieldId, subRecordViews, deletedRecords);
}
}
use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.
the class RecordTreeLoaderTest method simpleSurvey.
@Test
public void simpleSurvey() {
Survey survey = setup.getSurveyForm();
Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(survey.getRecordRef(0));
Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
RecordTree tree = recordTreeView.assertLoaded().get();
assertThat(tree.getRoot().get(survey.getNameFieldId()), equalTo(TextValue.valueOf("Melanie")));
}
Aggregations