use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class SubFormSummaryTest method minMaxFunction.
@Test
public void minMaxFunction() {
TestingStorageProvider catalog = new TestingStorageProvider();
ClinicForm clinicForm = catalog.getClinicForm();
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
QueryModel queryModel = new QueryModel(clinicForm.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectExpr("MIN(NUM_CONSULT)").as("min");
queryModel.selectExpr("MAX(NUM_CONSULT)").as("max");
ColumnSet columnSet = builder.build(queryModel);
ColumnView id = columnSet.getColumnView("id");
ColumnView min = columnSet.getColumnView("min");
ColumnView max = columnSet.getColumnView("max");
System.out.println(columnSet.getColumnView("id"));
System.out.println(min);
System.out.println(max);
assertThat(id.getString(0), equalTo("c0"));
assertThat(min.getDouble(0), equalTo(56.0));
assertThat(max.getDouble(0), equalTo(247.0));
}
use of org.activityinfo.model.query.ColumnSet 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());
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class XlsTableWriterTest method export.
private HSSFWorkbook export(TableModel tableModel) throws IOException {
TableViewModel viewModel = new TableViewModel(formSource, tableModel);
EffectiveTableModel effectiveTableModel = viewModel.getEffectiveTable().waitFor();
if (effectiveTableModel.getRootFormState() != FormTree.State.VALID) {
throw new IllegalStateException("Root Form has state: " + effectiveTableModel.getRootFormState());
}
ColumnSet columnSet = effectiveTableModel.getColumnSet().waitFor();
XlsTableWriter writer = new XlsTableWriter();
writer.addSheet(effectiveTableModel, columnSet);
writer.write(new FileOutputStream("build/" + tableModel.getFormId().asString() + ".xls"));
return writer.getBook();
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class FormResource method queryRows.
@GET
@NoCache
@Path("query/rows")
@Produces(MediaType.APPLICATION_JSON)
public Response queryRows(@Context UriInfo uriInfo) {
final ColumnSet columnSet = query(uriInfo);
LOGGER.info("Query completed with " + columnSet.getNumRows() + " rows.");
final StreamingOutput output = outputStream -> {
RowBasedJsonWriter writer = new RowBasedJsonWriter(outputStream, Charsets.UTF_8);
writer.write(columnSet);
writer.flush();
};
return Response.ok(output).type(JSON_CONTENT_TYPE).build();
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class InstanceScoreTest method adminEntityScoring.
@OnDataSet("/dbunit/nfi-import.db.xml")
@Test
public void adminEntityScoring() throws IOException {
setUser(3);
FormTree formTree = assertResolves(locator.getFormTree(ImportWithMultiClassRangeTest.SCHOOL_FORM_CLASS));
FormTreePrettyPrinter.print(formTree);
importModel = new ImportModel(formTree);
importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
// Step 1: User pastes in data to import
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "school-import.csv"), Charsets.UTF_8));
importModel.setSource(source);
dumpList("COLUMNS", source.getColumns());
importModel.setColumnAction(columnIndex("School"), target("Name"));
// Province is at the root of both hierarchies
importModel.setColumnAction(columnIndex("Province"), target("Province Name"));
// Admin hierarchy
importModel.setColumnAction(columnIndex("District"), target("District Name"));
importModel.setColumnAction(columnIndex("Territoire"), target("Territoire Name"));
importModel.setColumnAction(columnIndex("Secteur"), target("Secteur Name"));
importModel.setColumnAction(columnIndex("Groupement"), target("Secteur Name"));
// health ministry hierarchy
importModel.setColumnAction(columnIndex("Zone de Santé"), target("Zone de Santé Name"));
FormTree.Node rootField = formTree.getRootField(ADMINISTRATIVE_UNIT_FIELD);
TargetCollector targetCollector = new TargetCollector(rootField);
Map<TargetSiteId, ColumnAccessor> mappedColumns = importModel.getMappedColumns(rootField.getFieldId());
List<ColumnAccessor> sourceColumns = Lists.newArrayList();
Map<FieldPath, Integer> referenceFields = targetCollector.getPathMap(mappedColumns, sourceColumns);
// Province level
ColumnSet columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.PROVINCE_LEVEL));
InstanceScoreSource scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.PROVINCE_LEVEL), referenceFields, sourceColumns).build(columnSet);
InstanceScorer.Score score = score(source.getRows().get(0), scoreSource);
assertScore(score, "Katanga");
// District level
columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.DISTRICT_LEVEL));
scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.DISTRICT_LEVEL), referenceFields, sourceColumns).build(columnSet);
score = score(source.getRows().get(1), scoreSource);
assertScore(score, "Katanga");
assertScore(score, "Tanganika");
// Territoire level
columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.TERRITOIRE_LEVEL));
scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.TERRITOIRE_LEVEL), referenceFields, sourceColumns).build(columnSet);
score = score(source.getRows().get(2), scoreSource);
assertScore(score, "Katanga");
assertScore(score, "Tanganika");
assertScore(score, "Kalemie");
assertThat(scoreSource.getReferenceInstanceIds().get(score.getBestMatchIndex()), equalTo(ImportWithMultiClassRangeTest.TERRITOIRE_KALEMIE));
}
Aggregations