Search in sources :

Example 6 with ActivityField

use of org.activityinfo.store.mysql.metadata.ActivityField in project activityinfo by bedatadriven.

the class PivotAdapter method selectedIndicators.

private List<ActivityField> selectedIndicators(Activity activity) {
    if (filter.isRestricted(DimensionType.Activity)) {
        if (!filter.getRestrictions(DimensionType.Activity).contains(activity.getId())) {
            return Collections.emptyList();
        }
    }
    if (filter.isRestricted(DimensionType.Database)) {
        if (!filter.getRestrictions(DimensionType.Database).contains(activity.getDatabaseId())) {
            return Collections.emptyList();
        }
    }
    List<ActivityField> matching = Lists.newArrayList();
    Set<Integer> restrictedIndicatorIds = filter.getRestrictions(DimensionType.Indicator);
    for (ActivityField field : activity.getIndicatorFields()) {
        FieldType type = field.getFormField().getType();
        if (type instanceof QuantityType || type instanceof CalculatedFieldType) {
            if (restrictedIndicatorIds.isEmpty() || restrictedIndicatorIds.contains(field.getId())) {
                matching.add(field);
            }
        }
    }
    return matching;
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) FieldType(org.activityinfo.model.type.FieldType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType)

Example 7 with ActivityField

use of org.activityinfo.store.mysql.metadata.ActivityField 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 8 with ActivityField

use of org.activityinfo.store.mysql.metadata.ActivityField in project activityinfo by bedatadriven.

the class ActivityUpdater method updateFields.

private void updateFields(FormClass formClass) {
    int sortOrder = 1;
    Set<ResourceId> fieldsPresent = Sets.newHashSet();
    for (FormField formField : formClass.getFields()) {
        fieldsPresent.add(formField.getId());
        ActivityField existingField = fieldMap.get(formField.getId());
        if (existingField != null) {
            if (existingField.isIndicator()) {
                updateIndicatorRow(existingField, formField, sortOrder);
            } else if (existingField.isAttributeGroup()) {
                updateAttributeGroup(existingField, formField, sortOrder);
            }
        } else {
            if (isIndicator(formField)) {
                insertIndicatorRow(formField, sortOrder);
            }
            if (isAttributeGroup(formField)) {
                insertAttributeGroup(formField, sortOrder);
            }
        }
        sortOrder++;
    }
    for (ResourceId existingFieldId : fieldMap.keySet()) {
        if (!fieldsPresent.contains(existingFieldId)) {
            if (existingFieldId.getDomain() == CuidAdapter.INDICATOR_DOMAIN) {
                deleteIndicator(CuidAdapter.getLegacyIdFromCuid(existingFieldId));
            } else if (existingFieldId.getDomain() == CuidAdapter.ATTRIBUTE_GROUP_FIELD_DOMAIN) {
                deleteAttributeGroup(CuidAdapter.getLegacyIdFromCuid(existingFieldId));
            }
        }
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) FormField(org.activityinfo.model.form.FormField)

Aggregations

ActivityField (org.activityinfo.store.mysql.metadata.ActivityField)8 ResourceId (org.activityinfo.model.resource.ResourceId)3 Nullable (javax.annotation.Nullable)2 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)2 FormClass (org.activityinfo.model.form.FormClass)2 ColumnModel (org.activityinfo.model.query.ColumnModel)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 EnumType (org.activityinfo.model.type.enumerated.EnumType)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonValue (org.activityinfo.json.JsonValue)1 FormField (org.activityinfo.model.form.FormField)1 FormTree (org.activityinfo.model.formTree.FormTree)1 FieldType (org.activityinfo.model.type.FieldType)1 FieldValue (org.activityinfo.model.type.FieldValue)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1