Search in sources :

Example 1 with LookupKeySet

use of org.activityinfo.model.formTree.LookupKeySet 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 LookupKeySet

use of org.activityinfo.model.formTree.LookupKeySet in project activityinfo by bedatadriven.

the class LookupKeySetTest method multipleTextKeysTest.

@Test
public void multipleTextKeysTest() {
    TestingStorageProvider catalog = setup.getCatalog();
    SimpleReferenceForm refForm = catalog.getSimpleReferenceForm();
    MultipleTextKeysForm multTextKeyForm = catalog.getMultipleTextKeysForm();
    FormField refField = refForm.getRefField();
    CompoundExpr pathToKey1 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.FIRST_TEXT_KEY_ID.asString());
    CompoundExpr pathToKey2 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.SECOND_TEXT_KEY_ID.asString());
    FormTree formTree = catalog.getFormTree(multTextKeyForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, refField);
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(refField.getId());
    assertThat(formulas.values(), containsInAnyOrder(pathToKey1, pathToKey2));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 3 with LookupKeySet

use of org.activityinfo.model.formTree.LookupKeySet in project activityinfo by bedatadriven.

the class LookupKeySetTest method overlappingHierarchies.

/**
 * For classical location types especially, a reference field's range might include multiple forms,
 * such as [province, territory, health zone].
 *
 * This means that the user can <i>either</i> select a province, <i>or</i> a territory, <i>or</i>
 * a health zone.
 *
 * In this case, you also have a hierarchy
 * <pre>
 *               Province.Name
 *                 ^         ^
 *                 |         |
 *     Territory.Name     Health Zone.Name
 *        ^
 *        |
 *     Sector.Name
 * </pre>
 *
 * But the record reference depends on which fields are completed. If only province is completed,
 * then the selected record is from province. Otherwise from territory, etc.
 */
@Test
public void overlappingHierarchies() {
    TestingStorageProvider catalog = setup.getCatalog();
    LocaliteForm localiteForm = catalog.getLocaliteForm();
    FormTree formTree = setup.getFormTree(localiteForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
    // The resulting key set should only include 3 keys, not 6
    // because the three different forms in the range overlap
    assertThat(lookupKeySet.getLookupKeys(), hasSize(3));
    assertThat(lookupKeySet.getKey(0).getKeyLabel(), equalTo("Province Name"));
    assertThat(lookupKeySet.getKey(1).getKeyLabel(), equalTo("Territory Name"));
    assertThat(lookupKeySet.getKey(2).getKeyLabel(), equalTo("Zone de Sante Name"));
    // We need the relationships between the forms
    ResourceId provinceId = catalog.getProvince().getFormId();
    ResourceId territoryId = catalog.getTerritory().getFormId();
    assertThat(lookupKeySet.getAncestorForms(provinceId), hasSize(0));
    assertThat(lookupKeySet.getAncestorForms(territoryId), contains(provinceId));
    // Formulas...
    for (LookupKey lookupKey : lookupKeySet.getLeafKeys()) {
        System.out.println(lookupKey.getKeyLabel() + " => " + lookupKey.getKeyFormulas());
    }
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) LookupKey(org.activityinfo.model.formTree.LookupKey) Test(org.junit.Test)

Example 4 with LookupKeySet

use of org.activityinfo.model.formTree.LookupKeySet 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 LookupKeySet

use of org.activityinfo.model.formTree.LookupKeySet in project activityinfo by bedatadriven.

the class EffectiveTableModel method addKeyColumns.

private void addKeyColumns(List<EffectiveTableColumn> columns, FormTree.Node node) {
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, node.getField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(node.getFieldId());
    for (LookupKey lookupKey : lookupKeySet.getLookupKeys()) {
        ImmutableTableColumn tableColumn = ImmutableTableColumn.builder().id(node.getFieldId().asString() + "_k" + lookupKey.getKeyIndex()).formula(formulas.get(lookupKey).toString()).label(lookupKey.getKeyLabel()).build();
        columns.add(new EffectiveTableColumn(formTree, tableColumn));
    }
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) LookupKey(org.activityinfo.model.formTree.LookupKey) ImmutableTableColumn(org.activityinfo.model.analysis.ImmutableTableColumn)

Aggregations

LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)9 Test (org.junit.Test)8 FormTree (org.activityinfo.model.formTree.FormTree)6 LookupKey (org.activityinfo.model.formTree.LookupKey)6 FormulaNode (org.activityinfo.model.formula.FormulaNode)5 FormField (org.activityinfo.model.form.FormField)2 RecordTree (org.activityinfo.model.formTree.RecordTree)2 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)2 RecordRef (org.activityinfo.model.type.RecordRef)2 ReferenceValue (org.activityinfo.model.type.ReferenceValue)2 Maybe (org.activityinfo.promise.Maybe)2 ParsedFormula (org.activityinfo.analysis.ParsedFormula)1 ImmutableTableColumn (org.activityinfo.model.analysis.ImmutableTableColumn)1 SymbolNode (org.activityinfo.model.formula.SymbolNode)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1 ColumnView (org.activityinfo.model.query.ColumnView)1 QueryModel (org.activityinfo.model.query.QueryModel)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 ReferenceType (org.activityinfo.model.type.ReferenceType)1 TextType (org.activityinfo.model.type.primitive.TextType)1