Search in sources :

Example 16 with CompoundExpr

use of org.activityinfo.model.formula.CompoundExpr in project activityinfo by bedatadriven.

the class LookupKeySetTest method simpleHierarchyTest.

/**
 * In the case of a simple hierarchy, we are referencing a single form that
 * in turn has key reference fields.
 *
 * For example, if the field references a Village form, this should yield:
 *
 * <pre>
 *     Province.Name
 *        ^
 *        |
 *     District.Name
 *        ^
 *        |
 *     Village.Name
 * </pre>
 *
 * All fields are required because we ultimately need the reference to the village record.
 */
@Test
public void simpleHierarchyTest() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    NfiForm nfiForm = catalog.getNfiForm();
    VillageForm villageForm = catalog.getVillageForm();
    AdminLevelForm territoryForm = catalog.getVillageForm().getParentForm();
    AdminLevelForm provinceForm = territoryForm.getParentForm().get();
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, nfiForm.getVillageField());
    // Keys need to be topologically sorted,
    // with parent keys preceding child keys in the list
    assertThat(lookupKeySet.getKey(0).getKeyLabel(), equalTo("Province Name"));
    assertThat(lookupKeySet.getKey(1).getKeyLabel(), equalTo("Territory Name"));
    assertThat(lookupKeySet.getKey(2).getKeyLabel(), equalTo("Village Name"));
    SymbolNode villageName = Formulas.symbol(villageForm.getNameField().getId());
    SymbolNode villageTerritory = new SymbolNode(villageForm.getAdminFieldId());
    FormulaNode territoryName = new CompoundExpr(villageTerritory, territoryForm.getNameFieldId());
    FormulaNode territoryProvince = new CompoundExpr(villageTerritory, territoryForm.getParentFieldId());
    FormulaNode provinceName = new CompoundExpr(territoryProvince, provinceForm.getNameFieldId());
    assertThat(lookupKeySet.getLeafKeys(), hasSize(1));
    LookupKey leafKey = Iterables.getOnlyElement(lookupKeySet.getLeafKeys());
    assertThat(leafKey.getKeyFormulas().values(), containsInAnyOrder(villageName, territoryName, provinceName));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) Test(org.junit.Test)

Aggregations

CompoundExpr (org.activityinfo.model.formula.CompoundExpr)16 SymbolNode (org.activityinfo.model.formula.SymbolNode)12 FormulaNode (org.activityinfo.model.formula.FormulaNode)7 ColumnModel (org.activityinfo.model.query.ColumnModel)6 Test (org.junit.Test)4 FormTree (org.activityinfo.model.formTree.FormTree)3 ArrayList (java.util.ArrayList)2 LookupKey (org.activityinfo.model.formTree.LookupKey)2 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 ImmutableTableColumn (org.activityinfo.model.analysis.ImmutableTableColumn)1 ImmutableTableModel (org.activityinfo.model.analysis.ImmutableTableModel)1 TableColumn (org.activityinfo.model.analysis.TableColumn)1 TableModel (org.activityinfo.model.analysis.TableModel)1 FormClass (org.activityinfo.model.form.FormClass)1 FormField (org.activityinfo.model.form.FormField)1 FieldPath (org.activityinfo.model.formTree.FieldPath)1 ConstantNode (org.activityinfo.model.formula.ConstantNode)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1