Search in sources :

Example 1 with QueryModel

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);
        }
    }
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) File(java.io.File) QueryModel(org.activityinfo.model.query.QueryModel) PrintWriter(java.io.PrintWriter)

Example 2 with QueryModel

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;
    });
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) QueryModel(org.activityinfo.model.query.QueryModel)

Example 3 with QueryModel

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;
                }
            });
        }
    };
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) Supplier(com.google.common.base.Supplier) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 4 with QueryModel

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);
}
Also used : ColumnModel(org.activityinfo.model.query.ColumnModel) QueryModel(org.activityinfo.model.query.QueryModel)

Example 5 with QueryModel

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;
}
Also used : SiteDimBinding(org.activityinfo.server.command.handler.binding.dim.SiteDimBinding) QueryModel(org.activityinfo.model.query.QueryModel)

Aggregations

QueryModel (org.activityinfo.model.query.QueryModel)59 ColumnSet (org.activityinfo.model.query.ColumnSet)40 Test (org.junit.Test)30 ColumnView (org.activityinfo.model.query.ColumnView)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)13 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)11 FormTree (org.activityinfo.model.formTree.FormTree)9 ResourceId (org.activityinfo.model.resource.ResourceId)9 FormClass (org.activityinfo.model.form.FormClass)7 Nullable (javax.annotation.Nullable)6 FormField (org.activityinfo.model.form.FormField)6 ColumnModel (org.activityinfo.model.query.ColumnModel)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)4 Charsets (com.google.common.base.Charsets)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FieldPath (org.activityinfo.model.formTree.FieldPath)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3