Search in sources :

Example 41 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class ReportTable method getGrid.

/**
     * Generates a grid for this report table based on the given aggregate value
     * map.
     *
     * @param grid               the grid, should be empty and not null.
     * @param valueMap           the mapping of identifiers to aggregate values.
     * @param displayProperty    the display property to use for meta data.
     * @param reportParamColumns whether to include report parameter columns.
     * @return a grid.
     */
public Grid getGrid(Grid grid, Map<String, Object> valueMap, DisplayProperty displayProperty, boolean reportParamColumns) {
    valueMap = new HashMap<>(valueMap);
    sortKeys(valueMap);
    if (name != null) {
        grid.setTitle(name);
        grid.setSubtitle(gridTitle);
    } else {
        grid.setTitle(gridTitle);
    }
    // ---------------------------------------------------------------------
    // Headers
    // ---------------------------------------------------------------------
    Map<String, String> metaData = getMetaData();
    metaData.putAll(DimensionalObject.PRETTY_NAMES);
    for (String row : rowDimensions) {
        String name = StringUtils.defaultIfEmpty(metaData.get(row), row);
        String col = StringUtils.defaultIfEmpty(COLUMN_NAMES.get(row), row);
        grid.addHeader(new GridHeader(name + " ID", col + "id", ValueType.TEXT, String.class.getName(), true, true));
        grid.addHeader(new GridHeader(name, col + "name", ValueType.TEXT, String.class.getName(), false, true));
        grid.addHeader(new GridHeader(name + " code", col + "code", ValueType.TEXT, String.class.getName(), true, true));
        grid.addHeader(new GridHeader(name + " description", col + "description", ValueType.TEXT, String.class.getName(), true, true));
    }
    if (reportParamColumns) {
        grid.addHeader(new GridHeader("Reporting month", REPORTING_MONTH_COLUMN_NAME, ValueType.TEXT, String.class.getName(), true, true));
        grid.addHeader(new GridHeader("Organisation unit parameter", PARAM_ORGANISATIONUNIT_COLUMN_NAME, ValueType.TEXT, String.class.getName(), true, true));
        grid.addHeader(new GridHeader("Organisation unit is parent", ORGANISATION_UNIT_IS_PARENT_COLUMN_NAME, ValueType.TEXT, String.class.getName(), true, true));
    }
    final int startColumnIndex = grid.getHeaders().size();
    final int numberOfColumns = getGridColumns().size();
    for (List<DimensionalItemObject> column : gridColumns) {
        grid.addHeader(new GridHeader(getPrettyColumnName(column, displayProperty), getColumnName(column), ValueType.NUMBER, Double.class.getName(), false, false));
    }
    for (List<DimensionalItemObject> row : gridRows) {
        grid.addRow();
        for (DimensionalItemObject object : row) {
            grid.addValue(object.getDimensionItem());
            grid.addValue(object.getDisplayProperty(displayProperty));
            grid.addValue(object.getCode());
            grid.addValue(object.getDisplayDescription());
        }
        if (reportParamColumns) {
            grid.addValue(reportingPeriodName);
            grid.addValue(getParentOrganisationUnitName());
            grid.addValue(isCurrentParent(row) ? "Yes" : "No");
        }
        // -----------------------------------------------------------------
        // Row data values
        // -----------------------------------------------------------------
        boolean hasValue = false;
        for (List<DimensionalItemObject> column : gridColumns) {
            String key = getIdentifier(column, row);
            Object value = valueMap.get(key);
            grid.addValue(value);
            hasValue = hasValue || value != null;
        }
        if (hideEmptyRows && !hasValue) {
            grid.removeCurrentWriteRow();
        }
    // TODO hide empty columns
    }
    if (hideEmptyColumns) {
        grid.removeEmptyColumns();
    }
    if (regression) {
        grid.addRegressionToGrid(startColumnIndex, numberOfColumns);
    }
    if (cumulative) {
        grid.addCumulativesToGrid(startColumnIndex, numberOfColumns);
    }
    if (sortOrder != BaseAnalyticalObject.NONE) {
        grid.sortGrid(grid.getWidth(), sortOrder);
    }
    if (topLimit > 0) {
        grid.limitGrid(topLimit);
    }
    if (showHierarchy && rowDimensions.contains(ORGUNIT_DIM_ID) && grid.hasInternalMetaDataKey(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey())) {
        int ouIdColumnIndex = rowDimensions.indexOf(ORGUNIT_DIM_ID) * 4;
        addHierarchyColumns(grid, ouIdColumnIndex);
    }
    return grid;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) MetadataObject(org.hisp.dhis.common.MetadataObject) GridHeader(org.hisp.dhis.common.GridHeader)

Example 42 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class DefaultAnalyticsService method addIndicatorValues.

/**
     * Adds indicator values to the given grid based on the given data query
     * parameters.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     */
private void addIndicatorValues(DataQueryParams params, Grid grid) {
    if (!params.getIndicators().isEmpty() && !params.isSkipData()) {
        DataQueryParams dataSourceParams = DataQueryParams.newBuilder(params).retainDataDimension(DataDimensionItemType.INDICATOR).withIncludeNumDen(false).build();
        List<Indicator> indicators = asTypedList(dataSourceParams.getIndicators());
        Period filterPeriod = dataSourceParams.getFilterPeriod();
        Map<String, Double> constantMap = constantService.getConstantMap();
        // -----------------------------------------------------------------
        // Get indicator values
        // -----------------------------------------------------------------
        Map<String, Map<String, Integer>> permutationOrgUnitTargetMap = getOrgUnitTargetMap(dataSourceParams, indicators);
        List<List<DimensionItem>> dimensionItemPermutations = dataSourceParams.getDimensionItemPermutations();
        Map<String, Map<DimensionalItemObject, Double>> permutationDimensionItemValueMap = getPermutationDimensionItemValueMap(dataSourceParams);
        for (Indicator indicator : indicators) {
            for (List<DimensionItem> dimensionItems : dimensionItemPermutations) {
                String permKey = DimensionItem.asItemKey(dimensionItems);
                Map<DimensionalItemObject, Double> valueMap = permutationDimensionItemValueMap.get(permKey);
                if (valueMap == null) {
                    continue;
                }
                Period period = filterPeriod != null ? filterPeriod : (Period) DimensionItem.getPeriodItem(dimensionItems);
                OrganisationUnit unit = (OrganisationUnit) DimensionItem.getOrganisationUnitItem(dimensionItems);
                String ou = unit != null ? unit.getUid() : null;
                Map<String, Integer> orgUnitCountMap = permutationOrgUnitTargetMap != null ? permutationOrgUnitTargetMap.get(ou) : null;
                IndicatorValue value = expressionService.getIndicatorValueObject(indicator, period, valueMap, constantMap, orgUnitCountMap);
                if (value != null) {
                    List<DimensionItem> row = new ArrayList<>(dimensionItems);
                    row.add(DX_INDEX, new DimensionItem(DATA_X_DIM_ID, indicator));
                    grid.addRow().addValues(DimensionItem.getItemIdentifiers(row)).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getValue()));
                    if (params.isIncludeNumDen()) {
                        grid.addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getNumeratorValue())).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getDenominatorValue())).addValue(AnalyticsUtils.getRoundedValue(dataSourceParams, indicator.getDecimals(), value.getFactorAnnualizedValue()));
                    }
                }
            }
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) DimensionItem(org.hisp.dhis.analytics.DimensionItem) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) Indicator(org.hisp.dhis.indicator.Indicator) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) ArrayList(java.util.ArrayList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) HashMap(java.util.HashMap) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap)

Example 43 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class QueryPlannerUtils method getPeriodTypeDataElementMap.

/**
     * Creates a mapping between the period type and the data element for the
     * given data elements.
     * 
     * @param dataElements list of data elements.
     */
public static ListMap<PeriodType, DimensionalItemObject> getPeriodTypeDataElementMap(Collection<DimensionalItemObject> dataElements) {
    ListMap<PeriodType, DimensionalItemObject> map = new ListMap<>();
    for (DimensionalItemObject element : dataElements) {
        DataElement dataElement = (DataElement) element;
        map.putValue(dataElement.getPeriodType(), element);
    }
    return map;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ListMap(org.hisp.dhis.common.ListMap)

Example 44 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class QueryPlannerUtils method getDaysPeriodMap.

/**
     * Creates a mapping between the number of days in the period interval and period
     * for the given periods.
     * 
     * @param periods
     */
public static ListMap<Integer, DimensionalItemObject> getDaysPeriodMap(List<DimensionalItemObject> periods) {
    ListMap<Integer, DimensionalItemObject> map = new ListMap<>();
    for (DimensionalItemObject period : periods) {
        Period pe = (Period) period;
        int days = pe.getDaysInPeriod();
        map.putValue(days, pe);
    }
    return map;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Period(org.hisp.dhis.period.Period) ListMap(org.hisp.dhis.common.ListMap)

Example 45 with DimensionalItemObject

use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.

the class DefaultQueryPlanner method groupByDataPeriodType.

/**
     * Groups the given query in sub queries based on the period type of its
     * data elements. Sets the data period type on each query. This only applies
     * if the aggregation type of the query involves disaggregation.
     * 
     * @param params the data query parameters.
     * @return a list of {@link DataQueryParams}.
     */
private List<DataQueryParams> groupByDataPeriodType(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.getDataElements().isEmpty() || !params.isDisaggregation()) {
        queries.add(DataQueryParams.newBuilder(params).build());
        return queries;
    }
    ListMap<PeriodType, DimensionalItemObject> periodTypeDataElementMap = QueryPlannerUtils.getPeriodTypeDataElementMap(params.getDataElements());
    for (PeriodType periodType : periodTypeDataElementMap.keySet()) {
        DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(periodTypeDataElementMap.get(periodType)).withDataPeriodType(periodType).build();
        queries.add(query);
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on data period type: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) PeriodType(org.hisp.dhis.period.PeriodType) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList)

Aggregations

DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)55 ArrayList (java.util.ArrayList)15 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)15 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)14 Test (org.junit.Test)12 Period (org.hisp.dhis.period.Period)11 ListMap (org.hisp.dhis.common.ListMap)10 DataElement (org.hisp.dhis.dataelement.DataElement)10 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)7 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)7 HashMap (java.util.HashMap)6 List (java.util.List)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 PeriodType (org.hisp.dhis.period.PeriodType)6 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)5 Matcher (java.util.regex.Matcher)4 Grid (org.hisp.dhis.common.Grid)4 HashSet (java.util.HashSet)3