Search in sources :

Example 1 with Maybe

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")));
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) Maybe(org.activityinfo.promise.Maybe) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) Test(org.junit.Test)

Example 2 with Maybe

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());
}
Also used : Maybe(org.activityinfo.promise.Maybe) IDBFactoryStub(org.activityinfo.indexedb.IDBFactoryStub) FormTree(org.activityinfo.model.formTree.FormTree) HttpStore(org.activityinfo.ui.client.store.http.HttpStore) Test(org.junit.Test)

Example 3 with Maybe

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));
}
Also used : Maybe(org.activityinfo.promise.Maybe) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) IncidentForm(org.activityinfo.store.testing.IncidentForm) Test(org.junit.Test)

Example 4 with Maybe

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")));
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) Maybe(org.activityinfo.promise.Maybe) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) BioDataForm(org.activityinfo.store.testing.BioDataForm) Test(org.junit.Test)

Example 5 with Maybe

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;
}
Also used : java.util(java.util) FormulaParser(org.activityinfo.model.formula.FormulaParser) PeriodValue(org.activityinfo.model.type.time.PeriodValue) Multimap(com.google.common.collect.Multimap) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) Level(java.util.logging.Level) SerialNumberType(org.activityinfo.model.type.SerialNumberType) HashMultimap(com.google.common.collect.HashMultimap) ResourceId(org.activityinfo.model.resource.ResourceId) RecordTree(org.activityinfo.model.formTree.RecordTree) FormEvalContext(org.activityinfo.model.form.FormEvalContext) RecordRef(org.activityinfo.model.type.RecordRef) FormStore(org.activityinfo.ui.client.store.FormStore) FieldValue(org.activityinfo.model.type.FieldValue) RecordLockSet(org.activityinfo.model.database.RecordLockSet) Collections.emptyMap(java.util.Collections.emptyMap) FormInstance(org.activityinfo.model.form.FormInstance) Logger(java.util.logging.Logger) FormulaNode(org.activityinfo.model.formula.FormulaNode) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) Predicate(com.google.common.base.Predicate) Maybe(org.activityinfo.promise.Maybe) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance)

Aggregations

Maybe (org.activityinfo.promise.Maybe)7 Test (org.junit.Test)6 RecordTree (org.activityinfo.model.formTree.RecordTree)4 RecordRef (org.activityinfo.model.type.RecordRef)4 FormInstance (org.activityinfo.model.form.FormInstance)3 FormTree (org.activityinfo.model.formTree.FormTree)2 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)2 ReferenceValue (org.activityinfo.model.type.ReferenceValue)2 Predicate (com.google.common.base.Predicate)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 IDBFactoryStub (org.activityinfo.indexedb.IDBFactoryStub)1 RecordLockSet (org.activityinfo.model.database.RecordLockSet)1 FormEvalContext (org.activityinfo.model.form.FormEvalContext)1 FormField (org.activityinfo.model.form.FormField)1 FormRecord (org.activityinfo.model.form.FormRecord)1