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")));
}
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));
}
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());
}
}
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")));
}
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));
}
}
Aggregations