Search in sources :

Example 41 with ColumnSet

use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.

the class PivotAdapter method executeIndicatorValuesQuery.

private void executeIndicatorValuesQuery(final Activity activity, LinkedActivity linkedActivity) throws SQLException {
    // Double check that this activity has not been deleted
    if (isDeleted(activity, linkedActivity)) {
        return;
    }
    // Query the SOURCE form tree
    FormTree formTree = formTrees.get(linkedActivity.getLeafFormClassId());
    Preconditions.checkNotNull(formTree, "No form tree for form " + linkedActivity.getLeafFormClassId());
    QueryModel queryModel = new QueryModel(linkedActivity.getLeafFormClassId());
    List<ActivityField> indicators = selectedIndicators(activity);
    // Add Indicators to the query
    // Keep track of alias to destination map
    final Multimap<String, ActivityField> aliasToIndicator = HashMultimap.create();
    for (ActivityField indicator : indicators) {
        Collection<Integer> sourceIndicatorIds = linkedActivity.getSourceIndicatorIdsFor(indicator.getId());
        for (Integer sourceIndicatorId : sourceIndicatorIds) {
            String alias = "I" + sourceIndicatorId;
            queryModel.selectExpr(fieldExpression(sourceIndicatorId)).as(alias);
            aliasToIndicator.put(alias, indicator);
        }
    }
    // These are the columns we will use for grouping
    for (DimBinding dimension : groupBy) {
        for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
            queryModel.addColumn(columnModel);
        }
    }
    // if any of the indicators are "site count" indicators, then we need
    // to query the site id as well
    addSiteIdToQuery(activity, queryModel);
    // declare the filter
    queryModel.setFilter(composeFilter(formTree));
    // Query the table
    enqueueQuery(queryModel, new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            // Now add the results to the buckets
            int[] siteIds = extractSiteIds(columnSet);
            DimensionCategory[][] categories = extractCategories(activity, columnSet);
            for (String sourceAlias : aliasToIndicator.keySet()) {
                ColumnView measureView = columnSet.getColumnView(sourceAlias);
                // A single source indicator may be mapped to multiple destination Indicators
                for (ActivityField destinationIndicator : aliasToIndicator.get(sourceAlias)) {
                    DimensionCategory indicatorCategory = null;
                    if (indicatorDimension.isPresent()) {
                        indicatorCategory = indicatorDimension.get().category(destinationIndicator);
                    }
                    for (int i = 0; i < columnSet.getNumRows(); i++) {
                        if (destinationIndicator.getAggregation() == IndicatorDTO.AGGREGATE_SITE_COUNT) {
                            Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
                            Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
                            bucket.addSite(siteIds[i]);
                        } else {
                            double value = measureView.getDouble(i);
                            if (!Double.isNaN(value) && !Double.isInfinite(value)) {
                                Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
                                if (command.getValueType() == PivotSites.ValueType.INDICATOR) {
                                    Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
                                    bucket.addValue(value);
                                } else {
                                    Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
                                    bucket.addSite(siteIds[i]);
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) FormTree(org.activityinfo.model.formTree.FormTree) ColumnModel(org.activityinfo.model.query.ColumnModel) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) Nullable(javax.annotation.Nullable)

Example 42 with ColumnSet

use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.

the class HierarchyClassImporter method prepare.

@Override
public Promise<Void> prepare(ResourceLocator locator, List<? extends SourceRow> batch) {
    final List<Promise<Void>> promises = Lists.newArrayList();
    for (final ResourceId formId : rootField.getRange()) {
        if (isMatchableAtLevel(formId, referenceFields.keySet())) {
            QueryModel queryModel = new QueryModel(formId);
            queryModel.selectResourceId().as("_id");
            for (FieldPath referenceFieldPath : referenceFields.keySet()) {
                queryModel.selectField(referenceFieldPath).as(referenceFieldPath.toString());
            }
            promises.add(locator.queryTable(queryModel).then(new Function<ColumnSet, Void>() {

                @Override
                public Void apply(ColumnSet columnSet) {
                    scoreSources.put(formId, new InstanceScoreSourceBuilder(formId, referenceFields, sourceColumns).build(columnSet));
                    return null;
                }
            }));
        }
    }
    return Promise.waitAll(promises);
}
Also used : Promise(org.activityinfo.promise.Promise) Function(com.google.common.base.Function) ResourceId(org.activityinfo.model.resource.ResourceId) FieldPath(org.activityinfo.model.formTree.FieldPath) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel)

Example 43 with ColumnSet

use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.

the class SingleClassImporter method prepare.

@Override
public Promise<Void> prepare(ResourceLocator locator, List<? extends SourceRow> batch) {
    QueryModel queryModel = new QueryModel(rangeFormId);
    queryModel.selectResourceId().as("_id");
    for (FieldPath fieldPath : referenceFields.keySet()) {
        queryModel.selectField(fieldPath).as(fieldPath.toString());
    }
    return locator.queryTable(queryModel).then(new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet input) {
            scoreSource = new InstanceScoreSourceBuilder(rangeFormId, referenceFields, sources).build(input);
            instanceScorer = new InstanceScorer(scoreSource);
            return null;
        }
    });
}
Also used : FieldPath(org.activityinfo.model.formTree.FieldPath) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 44 with ColumnSet

use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.

the class LookupKeySetTest method overlappingHierarchiesKeyQuery.

@Test
public void overlappingHierarchiesKeyQuery() {
    TestingStorageProvider catalog = setup.getCatalog();
    LocaliteForm localiteForm = catalog.getLocaliteForm();
    FormTree formTree = setup.getFormTree(localiteForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(localiteForm.getAdminField().getId());
    QueryModel queryModel = new QueryModel(localiteForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(0))).as("province");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(1))).as("territory");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(2))).as("zs");
    Connection<ColumnSet> table = setup.connect(setup.getFormStore().query(queryModel));
    ColumnSet columnSet = table.assertLoaded();
    ColumnView id = columnSet.getColumnView("id");
    ColumnView province = columnSet.getColumnView("province");
    ColumnView territory = columnSet.getColumnView("territory");
    ColumnView zs = columnSet.getColumnView("zs");
    // First row references a Zone de Sante
    assertThat(id.getString(0), equalTo("c0"));
    assertThat(province.getString(0), equalTo("Province 14"));
    assertThat(territory.getString(0), nullValue());
    assertThat(zs.getString(0), equalTo("Zone de Sante 56"));
    // Second row references only a Province
    assertThat(id.getString(1), equalTo("c1"));
    assertThat(province.getString(1), equalTo("Province 8"));
    assertThat(territory.getString(1), nullValue());
    assertThat(zs.getString(1), nullValue());
    // Fifth row references a territory
    assertThat(id.getString(4), equalTo("c4"));
    assertThat(province.getString(4), equalTo("Province 15"));
    assertThat(territory.getString(4), equalTo("Territory 42"));
    assertThat(zs.getString(4), nullValue());
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 45 with ColumnSet

use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.

the class LookupViewModelTest method nullKeys.

@Test
public void nullKeys() {
    FormClass keyForm = new FormClass(ResourceId.valueOf("FORM1"));
    keyForm.addField(ResourceId.valueOf("PROVINCE")).setLabel("Province").setKey(true).setRequired(true).setType(TextType.SIMPLE);
    keyForm.addField(ResourceId.valueOf("SCHOOL")).setLabel("School").setKey(true).setRequired(true).setType(TextType.SIMPLE);
    FormClass form = new FormClass(ResourceId.valueOf("FORM2"));
    FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
    ReferenceType referenceType = new ReferenceType(Cardinality.SINGLE, ResourceId.valueOf("FORM1"));
    referenceField.setType(referenceType);
    form.addField(ResourceId.valueOf("PROJECT")).setLabel("Project name").setType(referenceType);
    FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProviderStub(form, keyForm));
    FormTree formTree = treeBuilder.queryTree(form.getId());
    Map<String, ColumnView> columnSet = new HashMap<>();
    columnSet.put("id", new StringArrayColumnView(Arrays.asList("R1", "R2", "R3", "R4")));
    columnSet.put("k1", new StringArrayColumnView(Arrays.asList("PZ", null, "PA", "PA")));
    columnSet.put("k2", new StringArrayColumnView(Arrays.asList("S1", "S2", null, "S3")));
    FormSource formSource = EasyMock.createMock(FormSource.class);
    EasyMock.expect(formSource.query(EasyMock.anyObject(QueryModel.class))).andReturn(Observable.just(new ColumnSet(3, columnSet))).anyTimes();
    EasyMock.replay(formSource);
    LookupViewModel viewModel = new LookupViewModel(formSource, formTree, referenceField);
    LookupKeyViewModel provinceKey = viewModel.getLookupKeys().get(0);
    assertThat(provinceKey.getChoices().get(), contains("PA", "PZ"));
    viewModel.select(provinceKey.getLookupKey(), "PA");
    LookupKeyViewModel schoolKey = viewModel.getLookupKeys().get(1);
    Connection<List<String>> schoolChoices = new Connection<>(schoolKey.getChoices());
    assertThat(schoolChoices.assertLoaded(), contains("S3"));
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) Connection(org.activityinfo.observable.Connection) ColumnSet(org.activityinfo.model.query.ColumnSet) ReferenceType(org.activityinfo.model.type.ReferenceType) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) FormSource(org.activityinfo.store.query.shared.FormSource) FormField(org.activityinfo.model.form.FormField) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) FormMetadataProviderStub(org.activityinfo.model.formTree.FormMetadataProviderStub) Test(org.junit.Test)

Aggregations

ColumnSet (org.activityinfo.model.query.ColumnSet)50 QueryModel (org.activityinfo.model.query.QueryModel)42 Test (org.junit.Test)28 ColumnView (org.activityinfo.model.query.ColumnView)24 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)14 FormTree (org.activityinfo.model.formTree.FormTree)12 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)12 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)7 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)7 Nullable (javax.annotation.Nullable)6 Charsets (com.google.common.base.Charsets)5 Operation (io.swagger.v3.oas.annotations.Operation)5 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)5 ColumnModel (org.activityinfo.model.query.ColumnModel)5 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)5 PrintWriter (java.io.PrintWriter)4 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 FormField (org.activityinfo.model.form.FormField)4 RecordRef (org.activityinfo.model.type.RecordRef)4