use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class AnalysisViewModelTest method dumpQuery.
private void dumpQuery(ResourceId formId, String... columns) {
if (DUMP_RAW_DATA) {
try {
File tempFile = File.createTempFile("query", ".csv");
try (PrintWriter writer = new PrintWriter(tempFile)) {
QueryModel model = new QueryModel(formId);
for (int i = 0; i < columns.length; i++) {
model.selectExpr(columns[i]).as("c" + i);
}
ColumnSet columnSet = assertLoads(formStore.query(model));
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
writer.print(",");
}
writer.print(columns[i]);
}
writer.println();
for (int i = 0; i < columnSet.getNumRows(); i++) {
for (int j = 0; j < columns.length; j++) {
if (j > 0) {
writer.print(",");
}
ColumnView columnView = columnSet.getColumnView("c" + j);
Object cell = columnView.get(i);
String cells = "";
if (cell != null) {
cells = cell.toString();
}
writer.print(cells);
}
writer.println();
}
}
System.out.println("Dumped data to " + tempFile);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class OfflineStoreGwtTest method verifyWeCanQueryRecords.
private Promise<Void> verifyWeCanQueryRecords(Void input) {
QueryModel queryModel = new QueryModel(survey.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectField(survey.getNameFieldId()).as("name");
queryModel.selectField(survey.getAgeFieldId()).as("age");
return formStore.query(queryModel).once().then(columnSet -> {
assertEquals(survey.getRowCount(), columnSet.getNumRows());
ColumnView name = columnSet.getColumnView("name");
ColumnView age = columnSet.getColumnView("age");
assertEquals(survey.getRowCount(), columnSet.getNumRows());
assertEquals("Melanie", name.get(0));
assertEquals("Joe", name.get(1));
assertEquals("Matilda", name.get(2));
return null;
});
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class Presenter method choices.
private Supplier<Promise<List<Choice>>> choices(final Level level) {
final QueryModel queryModel = new QueryModel(level.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectExpr("label").as("label");
if (!level.isRoot()) {
Choice selectedParent = getSelection(level.getParent());
queryModel.selectExpr("parent").as("parent");
queryModel.setFilter(Formulas.equals(new SymbolNode("parent"), Formulas.idConstant(selectedParent.getRef().getRecordId())));
}
return new Supplier<Promise<List<Choice>>>() {
@Override
public Promise<List<Choice>> get() {
return locator.queryTable(queryModel).then(new Function<ColumnSet, List<Choice>>() {
@Nullable
@Override
public List<Choice> apply(ColumnSet input) {
ColumnView id = input.getColumnView("id");
ColumnView label = input.getColumnView("label");
ColumnView parent = input.getColumnView("parent");
List<Choice> choices = new ArrayList<>();
for (int i = 0; i < input.getNumRows(); i++) {
if (parent == null) {
choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i)));
} else {
choices.add(new Choice(level.getFormId(), ResourceId.valueOf(id.getString(i)), label.getString(i), new RecordRef(level.getParent().getFormId(), ResourceId.valueOf(parent.getString(i)))));
}
}
return choices;
}
});
}
};
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class ApiViewModel method getQueryPostBody.
public String getQueryPostBody() {
QueryModel queryModel = new QueryModel(tableModel.getFormId());
for (EffectiveTableColumn tableColumn : tableModel.getColumns()) {
List<ColumnModel> columns = tableColumn.getQueryModel();
if (columns.size() == 1) {
ColumnModel columnModel = new ColumnModel();
columnModel.setId(tableColumn.getLabel());
columnModel.setFormula(tableColumn.getFormulaString());
queryModel.addColumn(columnModel);
} else if (columns.size() > 1) {
for (int i = 0; i < columns.size(); i++) {
ColumnModel columnModel = new ColumnModel();
columnModel.setId(tableColumn.getLabel() + "." + i);
columnModel.setFormula(tableColumn.getFormulaString());
queryModel.addColumn(columnModel);
}
}
}
return Json.stringify(queryModel.toJson(), 2);
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class GetSitesHandler method buildMonthlyQuery.
private QueryModel buildMonthlyQuery(FormTree formTree, FormClass form) {
QueryModel query = new QueryModel(form.getId());
fieldBindingMap.put(form.getId(), Lists.<FieldBinding>newLinkedList());
addBinding(new SiteDimBinding(), query, formTree);
addBinding(new ActivityIdFieldBinding(), query, formTree);
addBinding(new StartEndDateFieldBinding(), query, formTree);
if (command.fetchAnyIndicators()) {
query = buildIndicatorQuery(query, formTree, form);
}
if (command.isFetchAttributes()) {
query = buildAttributeQuery(query, formTree, form);
}
return query;
}
Aggregations