Search in sources :

Example 41 with FormTree

use of org.activityinfo.model.formTree.FormTree 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 FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class PivotAdapter method findAttributeFilterNames.

/**
 * Maps attribute filter ids to their attribute group id and name.
 *
 * Attribute filters are _SERIALIZED_ as only the integer ids of the required attributes,
 * but they are actually applied by _NAME_ to all forms in the query.
 */
private void findAttributeFilterNames() {
    Set<Integer> attributeIds = filter.getRestrictions(DimensionType.Attribute);
    if (attributeIds.isEmpty()) {
        return;
    }
    for (FormTree formTree : formTrees.values()) {
        for (FormTree.Node node : formTree.getLeaves()) {
            if (node.isEnum()) {
                EnumType type = (EnumType) node.getType();
                for (EnumItem enumItem : type.getValues()) {
                    int attributeId = CuidAdapter.getLegacyIdFromCuid(enumItem.getId());
                    if (attributeIds.contains(attributeId)) {
                        attributeFilters.put(node.getField().getLabel(), enumItem.getLabel());
                        attributeIds.remove(attributeId);
                        if (attributeIds.isEmpty()) {
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 43 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class PivotAdapter method queryFormTrees.

private Map<ResourceId, FormTree> queryFormTrees() {
    treeTime.start();
    Set<ResourceId> formIds = new HashSet<>();
    for (Activity activity : activities) {
        formIds.add(activity.getLeafFormClassId());
        if (command.getValueType() == PivotSites.ValueType.TOTAL_SITES) {
            formIds.add(activity.getSiteFormClassId());
        }
        for (LinkedActivity linkedActivity : activity.getLinkedActivities()) {
            formIds.add(linkedActivity.getLeafFormClassId());
            if (command.getValueType() == PivotSites.ValueType.TOTAL_SITES) {
                formIds.add(activity.getSiteFormClassId());
            }
        }
    }
    BatchingFormTreeBuilder formTreeBuilder = new BatchingFormTreeBuilder(catalog);
    Map<ResourceId, FormTree> trees = formTreeBuilder.queryTrees(formIds);
    treeTime.stop();
    return trees;
}
Also used : LinkedActivity(org.activityinfo.store.mysql.metadata.LinkedActivity) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) Activity(org.activityinfo.store.mysql.metadata.Activity) LinkedActivity(org.activityinfo.store.mysql.metadata.LinkedActivity) BatchingFormTreeBuilder(org.activityinfo.store.spi.BatchingFormTreeBuilder)

Example 44 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class MySqlCatalogTest method testBoundLocation.

@Test
public void testBoundLocation() {
    ResourceId formClassId = CuidAdapter.activityFormClass(41);
    FormTree tree = this.queryFormTree(formClassId);
    FormTree.Node locationNode = tree.getRootField(CuidAdapter.locationField(41));
    FormTreePrettyPrinter.print(tree);
    assertTrue(locationNode.getRange().contains(CuidAdapter.adminLevelFormClass(2)));
    query(formClassId, "territoire.name", "territoire.province.name");
    assertThat(column("_id"), hasValues("s0000009001", "s0000009002", "s0000009003"));
    assertThat(column("territoire.name"), hasValues("Bukavu", "Walungu", "Shabunda"));
    assertThat(column("territoire.province.name"), hasValues("Sud Kivu", "Sud Kivu", "Sud Kivu"));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) Test(org.junit.Test)

Example 45 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class MySqlCatalogTest method testAdminTree.

@Test
public void testAdminTree() {
    FormTree formTree = queryFormTree(activityFormClass(1));
    FormTreePrettyPrinter.print(formTree);
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) Test(org.junit.Test)

Aggregations

FormTree (org.activityinfo.model.formTree.FormTree)55 Test (org.junit.Test)35 ResourceId (org.activityinfo.model.resource.ResourceId)12 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)10 FormClass (org.activityinfo.model.form.FormClass)9 ColumnSet (org.activityinfo.model.query.ColumnSet)9 QueryModel (org.activityinfo.model.query.QueryModel)8 ImportModel (org.activityinfo.ui.client.component.importDialog.model.ImportModel)8 PastedTable (org.activityinfo.ui.client.component.importDialog.model.source.PastedTable)8 FormField (org.activityinfo.model.form.FormField)7 RecordRef (org.activityinfo.model.type.RecordRef)7 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)6 FormulaNode (org.activityinfo.model.formula.FormulaNode)6 ReferenceType (org.activityinfo.model.type.ReferenceType)6 LookupKey (org.activityinfo.model.formTree.LookupKey)5 ValidatedRowTable (org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable)5 Optional (com.google.common.base.Optional)4 ColumnView (org.activityinfo.model.query.ColumnView)4 Nullable (javax.annotation.Nullable)3 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)3