Search in sources :

Example 46 with FormTree

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

the class FieldTreeBuilder method addReferencedForms.

private void addReferencedForms(FormTree tree, Iterable<FormTree.Node> nodes) {
    for (FormTree.Node childNode : nodes) {
        if (childNode.isReference() && !childNode.isSubForm()) {
            addReferencedForms(tree, childNode.getChildren());
            for (ResourceId referencedFormId : childNode.getRange()) {
                FormClass referencedForm = tree.getFormClass(referencedFormId);
                addReferencedForm(referencedForm);
            }
        }
    }
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass)

Example 47 with FormTree

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

Example 48 with FormTree

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

the class LookupKeySetTest method overlappingHierarchiesParse.

@Test
public void overlappingHierarchiesParse() {
    TestingStorageProvider catalog = setup.getCatalog();
    LocaliteForm localiteForm = catalog.getLocaliteForm();
    FormTree formTree = setup.getFormTree(localiteForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(localiteForm.getAdminField().getId());
    ParsedFormula province = new ParsedFormula(formTree, formulas.get(lookupKeySet.getKey(0)).asExpression());
    assertThat(province.isValid(), equalTo(true));
    assertThat(province.getResultType(), instanceOf(TextType.class));
}
Also used : ParsedFormula(org.activityinfo.analysis.ParsedFormula) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) TextType(org.activityinfo.model.type.primitive.TextType) Test(org.junit.Test)

Example 49 with FormTree

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

the class LookupKeySetTest method noKeysTest.

@Test
public void noKeysTest() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    NfiForm nfiForm = catalog.getNfiForm();
    FormField field = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
    field.setType(new ReferenceType(Cardinality.SINGLE, nfiForm.getFormId()));
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, field);
    assertThat(lookupKeySet.getLookupKeys(), hasSize(1));
    assertThat(lookupKeySet.getLeafKeys(), hasSize(1));
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormTree(org.activityinfo.model.formTree.FormTree) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 50 with FormTree

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

the class LookupKeySetTest method overlappingHierarchiesKeyQuery.

@Test
public void overlappingHierarchiesKeyQuery() {
    TestingStorageProvider catalog = setup.getCatalog();
    LocaliteForm localiteForm = catalog.getLocaliteForm();
    FormTree formTree = setup.getFormTree(localiteForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(localiteForm.getAdminField().getId());
    QueryModel queryModel = new QueryModel(localiteForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(0))).as("province");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(1))).as("territory");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(2))).as("zs");
    Connection<ColumnSet> table = setup.connect(setup.getFormStore().query(queryModel));
    ColumnSet columnSet = table.assertLoaded();
    ColumnView id = columnSet.getColumnView("id");
    ColumnView province = columnSet.getColumnView("province");
    ColumnView territory = columnSet.getColumnView("territory");
    ColumnView zs = columnSet.getColumnView("zs");
    // First row references a Zone de Sante
    assertThat(id.getString(0), equalTo("c0"));
    assertThat(province.getString(0), equalTo("Province 14"));
    assertThat(territory.getString(0), nullValue());
    assertThat(zs.getString(0), equalTo("Zone de Sante 56"));
    // Second row references only a Province
    assertThat(id.getString(1), equalTo("c1"));
    assertThat(province.getString(1), equalTo("Province 8"));
    assertThat(territory.getString(1), nullValue());
    assertThat(zs.getString(1), nullValue());
    // Fifth row references a territory
    assertThat(id.getString(4), equalTo("c4"));
    assertThat(province.getString(4), equalTo("Province 15"));
    assertThat(territory.getString(4), equalTo("Territory 42"));
    assertThat(zs.getString(4), nullValue());
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

FormTree (org.activityinfo.model.formTree.FormTree)55 Test (org.junit.Test)35 ResourceId (org.activityinfo.model.resource.ResourceId)12 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)10 FormClass (org.activityinfo.model.form.FormClass)9 ColumnSet (org.activityinfo.model.query.ColumnSet)9 QueryModel (org.activityinfo.model.query.QueryModel)8 ImportModel (org.activityinfo.ui.client.component.importDialog.model.ImportModel)8 PastedTable (org.activityinfo.ui.client.component.importDialog.model.source.PastedTable)8 FormField (org.activityinfo.model.form.FormField)7 RecordRef (org.activityinfo.model.type.RecordRef)7 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)6 FormulaNode (org.activityinfo.model.formula.FormulaNode)6 ReferenceType (org.activityinfo.model.type.ReferenceType)6 LookupKey (org.activityinfo.model.formTree.LookupKey)5 ValidatedRowTable (org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable)5 Optional (com.google.common.base.Optional)4 ColumnView (org.activityinfo.model.query.ColumnView)4 Nullable (javax.annotation.Nullable)3 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)3