use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class ImportSimpleTest method locationWithMissingAdminLevel.
@Test
public void locationWithMissingAdminLevel() throws IOException {
FormTree formTree = assertResolves(locator.getFormTree(VILLAGE_FORM_ID));
FormTreePrettyPrinter.print(formTree);
importModel = new ImportModel(formTree);
// Step 1: User pastes in data to import
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis-villages.csv"), Charsets.UTF_8));
source.parseAllRows();
assertThat(source.getRows().size(), equalTo(1));
importModel.setSource(source);
importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
dumpList("COLUMNS", source.getColumns());
// Step 2: User maps imported columns to FormFields
List<ImportTarget> targets = importer.getImportTargets();
dumpList("FIELDS", targets);
importModel.setColumnAction(columnIndex("Name"), target("Name"));
importModel.setColumnAction(columnIndex("District"), target("District Name"));
// Step 3: Validate for user
ValidatedRowTable validatedResult = assertResolves(importer.validateRows(importModel));
showValidationGrid(validatedResult);
assertResolves(importer.persist(importModel));
// AND... verify
QueryModel queryModel = new QueryModel(VILLAGE_FORM_ID);
queryModel.selectExpr("Name").as("name");
queryModel.selectField(CuidAdapter.field(VILLAGE_FORM_ID, CuidAdapter.ADMIN_FIELD)).as("admin");
ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
assertThat(columnSet.getNumRows(), equalTo(1));
assertThat(columnSet.getColumnView("name").getString(0), equalTo("Village 1"));
assertThat(columnSet.getColumnView("admin").getString(0), equalTo(CuidAdapter.cuid(CuidAdapter.ADMIN_ENTITY_DOMAIN, 2).asString()));
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class NodeMatcherTest method resolve.
private Collection<String> resolve(String exprString) {
FormTree tree = tree();
symbolTable = new NodeMatcher(tree);
FormulaNode expr = FormulaParser.parse(exprString);
Collection<NodeMatch> matches;
if (expr instanceof SymbolNode) {
matches = symbolTable.resolveSymbol((SymbolNode) expr);
} else if (expr instanceof CompoundExpr) {
matches = symbolTable.resolveCompoundExpr((CompoundExpr) expr);
} else {
throw new IllegalArgumentException(exprString);
}
// Create a string that we can match against easily
List<String> strings = Lists.newArrayList();
for (NodeMatch match : matches) {
strings.add(match.toDebugString());
}
System.out.println("Resolved " + exprString + " => " + strings);
return strings;
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class BatchingFormTreeBuilder method queryTrees.
public Map<ResourceId, FormTree> queryTrees(Collection<ResourceId> rootFormIds) {
// Fetch Required FormClasses in batches
Set<ResourceId> toFetch = Sets.newHashSet();
toFetch.addAll(rootFormIds);
while (!toFetch.isEmpty()) {
// First round: fetch root form classes
List<FormClass> fetched = fetchFormClasses(toFetch);
toFetch.clear();
// Find newly referenced forms
for (FormClass formClass : fetched) {
if (formClass.isSubForm()) {
if (!formCache.containsKey(formClass.getParentFormId().get())) {
toFetch.add(formClass.getParentFormId().get());
}
}
for (FormField formField : formClass.getFields()) {
if (formField.getType() instanceof ReferenceType) {
ReferenceType refType = (ReferenceType) formField.getType();
for (ResourceId refFormId : refType.getRange()) {
if (!formCache.containsKey(refFormId)) {
toFetch.add(refFormId);
}
}
} else if (formField.getType() instanceof SubFormReferenceType) {
SubFormReferenceType subFormType = (SubFormReferenceType) formField.getType();
if (!formCache.containsKey(subFormType.getClassId())) {
toFetch.add(subFormType.getClassId());
}
}
}
}
}
// Now assemble trees
Map<ResourceId, FormTree> treeMap = new HashMap<>();
FormTreeBuilder builder = new FormTreeBuilder(new FormClassProvider() {
@Override
public FormClass getFormClass(ResourceId formId) {
return formCache.get(formId);
}
});
for (ResourceId rootFormId : rootFormIds) {
treeMap.put(rootFormId, builder.queryTree(rootFormId));
}
return treeMap;
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class FormTreeBuilderTest method buildAndLoadBlankSubform.
@Test
public void buildAndLoadBlankSubform() {
List<TestForm> testForms = Lists.newArrayList();
GenericForm generic = new GenericForm();
BlankSubForm blank = new BlankSubForm(generic);
testForms.add(generic);
testForms.add(blank);
TestFormMetadataProvider provider = new TestFormMetadataProvider(testForms);
FormTreeBuilder builder = new FormTreeBuilder(provider);
// Build the form tree, and attempt to fetch the blank sub form
FormTree tree = builder.queryTree(generic.getFormId());
tree.getFormClass(blank.getFormId());
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class MySqlCatalogIntegrationTest method testActivity.
@Test
public void testActivity() throws IOException {
QueryModel model = new QueryModel(activityFormClass(33));
model.selectField("date1");
model.selectField("date2");
model.selectExpr("Partner.name");
FormTree formTree = new FormTreeBuilder(catalog).queryTree(activityFormClass(33));
FormTreePrettyPrinter.print(formTree);
FormClass formClass = catalog.getForm(activityFormClass(33)).get().getFormClass();
for (FormField field : formClass.getFields()) {
if (field.getType() instanceof QuantityType) {
model.selectField(field.getId()).as("I" + CuidAdapter.getLegacyIdFromCuid(field.getId()));
}
}
Stopwatch stopwatch = Stopwatch.createStarted();
ColumnSet columnSet = columnSetBuilder.build(model);
System.out.println("Query executed in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
assertThat(columnSet.getNumRows(), equalTo(652));
StringWriter stringWriter = new StringWriter();
RowBasedJsonWriter writer = new RowBasedJsonWriter(stringWriter);
writer.write(columnSet);
System.out.println(stringWriter.toString());
}
Aggregations