Search in sources :

Example 1 with MetadataItem

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

the class AbstractAnalyticsService method getMetadataItems.

/**
 * Returns a map of metadata item identifiers and {@link MetadataItem}.
 *
 * @param params the data query parameters.
 * @return a map.
 */
private Map<String, MetadataItem> getMetadataItems(EventQueryParams params, List<DimensionItemKeywords.Keyword> periodKeywords, List<Option> itemOptions) {
    Map<String, MetadataItem> metadataItemMap = AnalyticsUtils.getDimensionMetadataItemMap(params);
    boolean includeDetails = params.isIncludeMetadataDetails();
    if (params.hasValueDimension()) {
        DimensionalItemObject value = params.getValue();
        metadataItemMap.put(value.getUid(), new MetadataItem(value.getDisplayProperty(params.getDisplayProperty()), includeDetails ? value.getUid() : null, value.getCode()));
    }
    params.getItemLegends().stream().filter(Objects::nonNull).forEach(legend -> metadataItemMap.put(legend.getUid(), new MetadataItem(legend.getDisplayName(), includeDetails ? legend.getUid() : null, legend.getCode())));
    addMetadataItems(metadataItemMap, params, itemOptions);
    params.getItemsAndItemFilters().stream().filter(Objects::nonNull).forEach(item -> metadataItemMap.put(getItemIdMaybeWithProgramStageIdPrefix(item), new MetadataItem(item.getItem().getDisplayName(), includeDetails ? item.getItem() : null)));
    if (hasPeriodKeywords(periodKeywords)) {
        for (DimensionItemKeywords.Keyword keyword : periodKeywords) {
            if (keyword.getMetadataItem() != null) {
                metadataItemMap.put(keyword.getKey(), new MetadataItem(keyword.getMetadataItem().getName()));
            }
        }
    }
    return metadataItemMap;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) MetadataItem(org.hisp.dhis.common.MetadataItem)

Example 2 with MetadataItem

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

the class EventAnalyticsServiceMetadataTest method testGetQueryItemMetadata.

@Test
@SuppressWarnings("unchecked")
void testGetQueryItemMetadata() {
    DimensionalObject periods = new BaseDimensionalObject(DimensionalObject.PERIOD_DIM_ID, DimensionType.PERIOD, Lists.newArrayList(peA));
    DimensionalObject orgUnits = new BaseDimensionalObject(DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, Lists.newArrayList(ouA));
    QueryItem qiA = new QueryItem(deA, deA.getLegendSet(), deA.getValueType(), deA.getAggregationType(), null);
    QueryItem qiB = new QueryItem(deE, null, deE.getValueType(), deE.getAggregationType(), deE.getOptionSet());
    EventQueryParams params = new EventQueryParams.Builder().withProgram(prA).addDimension(periods).addDimension(orgUnits).addItem(qiA).addItemFilter(qiB).withSkipData(true).withSkipMeta(false).withApiVersion(DhisApiVersion.V29).build();
    Grid grid = eventAnalyticsService.getAggregatedEventData(params);
    Map<String, Object> metadata = grid.getMetaData();
    Map<String, MetadataItem> itemMap = (Map<String, MetadataItem>) metadata.get(AnalyticsMetaDataKey.ITEMS.getKey());
    assertNotNull(itemMap.get(DimensionalObject.PERIOD_DIM_ID));
    assertNotNull(itemMap.get(DimensionalObject.ORGUNIT_DIM_ID));
    for (Legend legend : deA.getLegendSet().getLegends()) {
        assertNotNull(itemMap.get(legend.getUid()));
    }
    for (Option option : deE.getOptionSet().getOptions()) {
        assertNotNull(itemMap.get(option.getUid()));
    }
    assertNotNull(itemMap.get(deA.getUid()));
    assertNotNull(itemMap.get(deE.getUid()));
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) QueryItem(org.hisp.dhis.common.QueryItem) Legend(org.hisp.dhis.legend.Legend) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Grid(org.hisp.dhis.common.Grid) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Option(org.hisp.dhis.option.Option) Map(java.util.Map) MetadataItem(org.hisp.dhis.common.MetadataItem) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with MetadataItem

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

the class DefaultEventAnalyticsService method addEventReportDimensionalItems.

/**
 * Adds dimensional items to the given list of objects. Send in a list of
 * {@link EventAnalyticsDimensionalItem} and add properties from
 * {@link ValueTypedDimensionalItemObject} parameter.
 *
 * @param eventDimensionalItemObject the
 *        {@link ValueTypedDimensionalItemObject} object to get properties
 *        from.
 * @param objects the list of {@link EventAnalyticsDimensionalItem} objects.
 * @param grid the {@link Grid} from the event analytics request.
 * @param dimension the dimension identifier.
 */
@SuppressWarnings("unchecked")
private void addEventReportDimensionalItems(ValueTypedDimensionalItemObject eventDimensionalItemObject, List<EventAnalyticsDimensionalItem> objects, Grid grid, String dimension) {
    Preconditions.checkNotNull(eventDimensionalItemObject, String.format("Data dimension '%s' is invalid", dimension));
    String parentUid = eventDimensionalItemObject.getUid();
    if (eventDimensionalItemObject.getValueType() == BOOLEAN) {
        objects.add(new EventAnalyticsDimensionalItem(OPT_TRUE, parentUid));
        objects.add(new EventAnalyticsDimensionalItem(OPT_FALSE, parentUid));
    }
    if (eventDimensionalItemObject.hasOptionSet()) {
        for (Option option : eventDimensionalItemObject.getOptionSet().getOptions()) {
            objects.add(new EventAnalyticsDimensionalItem(option, parentUid));
        }
    } else if (eventDimensionalItemObject.hasLegendSet()) {
        List<String> legendOptions = (List<String>) ((Map<String, Object>) grid.getMetaData().get(DIMENSIONS.getKey())).get(dimension);
        if (legendOptions.isEmpty()) {
            List<Legend> legends = eventDimensionalItemObject.getLegendSet().getSortedLegends();
            for (Legend legend : legends) {
                for (int i = legend.getStartValue().intValue(); i < legend.getEndValue().intValue(); i++) {
                    objects.add(new EventAnalyticsDimensionalItem(new Option(String.valueOf(i), String.valueOf(i)), parentUid));
                }
            }
        } else {
            for (String legend : legendOptions) {
                MetadataItem metadataItem = (MetadataItem) ((Map<String, Object>) grid.getMetaData().get(ITEMS.getKey())).get(legend);
                objects.add(new EventAnalyticsDimensionalItem(new Option(metadataItem.getName(), legend), parentUid));
            }
        }
    }
}
Also used : EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) Legend(org.hisp.dhis.legend.Legend) Option(org.hisp.dhis.option.Option) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MetadataItem(org.hisp.dhis.common.MetadataItem)

Example 4 with MetadataItem

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

the class DefaultEventAnalyticsService method generateOutputGrid.

/**
 * Generates an output grid for event analytics download based on input
 * parameters.
 *
 * @param grid the result grid.
 * @param params the {@link EventQueryParams}.
 * @param rowPermutations the row permutations
 * @param columnPermutations the column permutations.
 * @param rowDimensions the row dimensions.
 * @return grid with table layout.
 */
@SuppressWarnings("unchecked")
private Grid generateOutputGrid(Grid grid, EventQueryParams params, List<Map<String, EventAnalyticsDimensionalItem>> rowPermutations, List<Map<String, EventAnalyticsDimensionalItem>> columnPermutations, List<String> rowDimensions) {
    Grid outputGrid = new ListGrid();
    outputGrid.setTitle(IdentifiableObjectUtils.join(params.getFilterItems()));
    for (String row : rowDimensions) {
        MetadataItem metadataItem = (MetadataItem) ((Map<String, Object>) grid.getMetaData().get(ITEMS.getKey())).get(row);
        String name = StringUtils.defaultIfEmpty(metadataItem.getName(), row);
        String col = StringUtils.defaultIfEmpty(COLUMN_NAMES.get(row), row);
        outputGrid.addHeader(new GridHeader(name, col, ValueType.TEXT, false, true));
    }
    columnPermutations.forEach(permutation -> {
        StringBuilder builder = new StringBuilder();
        permutation.forEach((key, value) -> {
            if (!key.equals(ORGUNIT_DIM_ID) && !key.equals(PERIOD_DIM_ID)) {
                builder.append(key).append(SPACE);
            }
            builder.append(value.getDisplayProperty(params.getDisplayProperty())).append(DASH_PRETTY_SEPARATOR);
        });
        String display = builder.length() > 0 ? builder.substring(0, builder.lastIndexOf(DASH_PRETTY_SEPARATOR)) : TOTAL_COLUMN_PRETTY_NAME;
        outputGrid.addHeader(new GridHeader(display, display, ValueType.NUMBER, false, false));
    });
    for (Map<String, EventAnalyticsDimensionalItem> rowCombination : rowPermutations) {
        outputGrid.addRow();
        List<List<String>> ids = new ArrayList<>();
        Map<String, EventAnalyticsDimensionalItem> displayObjects = new HashMap<>();
        boolean fillDisplayList = true;
        for (Map<String, EventAnalyticsDimensionalItem> columnCombination : columnPermutations) {
            List<String> idList = new ArrayList<>();
            boolean finalFillDisplayList = fillDisplayList;
            rowCombination.forEach((key, value) -> {
                idList.add(value.toString());
                if (finalFillDisplayList) {
                    displayObjects.put(value.getParentUid(), value);
                }
            });
            columnCombination.forEach((key, value) -> idList.add(value.toString()));
            ids.add(idList);
            fillDisplayList = false;
        }
        rowDimensions.forEach(dimension -> outputGrid.addValue(displayObjects.get(dimension).getDisplayProperty(params.getDisplayProperty())));
        EventAnalyticsUtils.addValues(ids, grid, outputGrid);
    }
    return outputGrid;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) EventAnalyticalObject(org.hisp.dhis.common.EventAnalyticalObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) List(java.util.List) ArrayList(java.util.ArrayList) MetadataItem(org.hisp.dhis.common.MetadataItem)

Example 5 with MetadataItem

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

the class AnalyticsUtils method getDimensionMetadataItemMap.

/**
 * Returns a mapping between identifiers and meta data items for the given
 * query.
 *
 * @param params the data query parameters.
 * @return a mapping between identifiers and meta data items.
 */
public static Map<String, MetadataItem> getDimensionMetadataItemMap(DataQueryParams params) {
    List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
    Map<String, MetadataItem> map = new HashMap<>();
    Calendar calendar = PeriodType.getCalendar();
    boolean includeMetadataDetails = params.isIncludeMetadataDetails();
    for (DimensionalObject dimension : dimensions) {
        for (DimensionalItemObject item : dimension.getItems()) {
            if (DimensionType.PERIOD == dimension.getDimensionType() && !calendar.isIso8601()) {
                Period period = (Period) item;
                DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
                String isoDate = period.getPeriodType().getIsoDate(dateTimeUnit);
                map.put(isoDate, new MetadataItem(period.getDisplayName(), includeMetadataDetails ? period : null));
            } else {
                map.put(item.getDimensionItem(), new MetadataItem(item.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? item : null));
            }
            if (DimensionType.ORGANISATION_UNIT == dimension.getDimensionType() && params.isHierarchyMeta()) {
                OrganisationUnit unit = (OrganisationUnit) item;
                for (OrganisationUnit ancestor : unit.getAncestors()) {
                    map.put(ancestor.getUid(), new MetadataItem(ancestor.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? ancestor : null));
                }
            }
            if (DimensionItemType.DATA_ELEMENT == item.getDimensionItemType()) {
                DataElement dataElement = (DataElement) item;
                for (CategoryOptionCombo coc : dataElement.getCategoryOptionCombos()) {
                    map.put(coc.getUid(), new MetadataItem(coc.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? coc : null));
                }
            }
        }
        map.put(dimension.getDimension(), new MetadataItem(dimension.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? dimension : null));
        if (dimension.getDimensionItemKeywords() != null) {
            dimension.getDimensionItemKeywords().getKeywords().forEach(b -> map.put(b.getKey(), b.getMetadataItem()));
        }
    }
    Program program = params.getProgram();
    ProgramStage stage = params.getProgramStage();
    if (ObjectUtils.allNotNull(program)) {
        map.put(program.getUid(), new MetadataItem(program.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? program : null));
        if (stage != null) {
            map.put(stage.getUid(), new MetadataItem(stage.getDisplayName(), includeMetadataDetails ? stage : null));
        } else {
            for (ProgramStage ps : program.getProgramStages()) {
                map.put(ps.getUid(), new MetadataItem(ps.getDisplayName(), includeMetadataDetails ? ps : null));
            }
        }
    }
    return map;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) HashMap(java.util.HashMap) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DataElement(org.hisp.dhis.dataelement.DataElement) DataDimensionalItemObject(org.hisp.dhis.common.DataDimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) ProgramStage(org.hisp.dhis.program.ProgramStage) MetadataItem(org.hisp.dhis.common.MetadataItem) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Aggregations

MetadataItem (org.hisp.dhis.common.MetadataItem)5 HashMap (java.util.HashMap)3 DimensionalObject (org.hisp.dhis.common.DimensionalObject)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 EventAnalyticsDimensionalItem (org.hisp.dhis.analytics.EventAnalyticsDimensionalItem)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 Grid (org.hisp.dhis.common.Grid)2 Legend (org.hisp.dhis.legend.Legend)2 Option (org.hisp.dhis.option.Option)2 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)1 Calendar (org.hisp.dhis.calendar.Calendar)1 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)1 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)1 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)1 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)1 DataDimensionalItemObject (org.hisp.dhis.common.DataDimensionalItemObject)1