use of org.activityinfo.promise.Maybe 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.promise.Maybe in project activityinfo by bedatadriven.
the class FormStoreTest method offlineRecordFetching.
@Test
public void offlineRecordFetching() {
AsyncClientStub client = new AsyncClientStub();
HttpStore httpStore = new HttpStore(client, scheduler);
OfflineStore offlineStore = new OfflineStore(httpStore, new IDBFactoryStub());
FormStoreImpl formStore = new FormStoreImpl(httpStore, offlineStore, scheduler);
Survey survey = client.getStorageProvider().getSurvey();
// Start online
Connection<FormOfflineStatus> offlineStatusView = connect(formStore.getOfflineStatus(survey.getFormId()));
// Initially form should not be loaded
assertFalse(offlineStatusView.assertLoaded().isEnabled());
// and mark the survey form for offline usage
offlineStore.enableOffline(survey.getFormId(), true);
assertTrue(offlineStatusView.assertLoaded().isEnabled());
assertFalse(offlineStatusView.assertLoaded().isCached());
// Now synchronize...
RecordSynchronizer synchronizer = new RecordSynchronizer(httpStore, offlineStore);
runScheduled();
// We go offline...
client.setConnected(false);
// Should be able to view the form class and a record
Connection<FormTree> schemaView = connect(formStore.getFormTree(survey.getFormId()));
Connection<Maybe<FormRecord>> recordView = connect(formStore.getRecord(survey.getRecordRef(0)));
runScheduled();
schemaView.assertLoaded();
recordView.assertLoaded();
assertTrue(offlineStatusView.assertLoaded().isEnabled());
assertTrue(offlineStatusView.assertLoaded().isCached());
}
use of org.activityinfo.promise.Maybe in project activityinfo by bedatadriven.
the class RecordTreeLoaderTest method subforms.
@Test
public void subforms() {
IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
RecordRef rootRecordRef = incidentForm.getRecordRef(0);
Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(rootRecordRef);
Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
Iterable<FormInstance> subRecords = recordTreeView.assertLoaded().get().getSubRecords(rootRecordRef, ReferralSubForm.FORM_ID);
assertThat(Iterables.size(subRecords), equalTo(4));
}
use of org.activityinfo.promise.Maybe 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.promise.Maybe 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;
}
Aggregations