Search in sources :

Example 1 with EventAnalyticsDimensionalItem

use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.

the class DefaultEventAnalyticsService method addEventDataObjects.

/**
 * Puts elements into the mapping table. The elements are fetched from the
 * query parameters.
 *
 * @param grid the {@link Grid}.
 * @param params the {@link EventQueryParams}.
 * @param table the map to add elements to.
 * @param dimension the dimension identifier.
 */
private void addEventDataObjects(Grid grid, EventQueryParams params, Map<String, List<EventAnalyticsDimensionalItem>> table, String dimension) {
    List<EventAnalyticsDimensionalItem> objects = params.getEventReportDimensionalItemArrayExploded(dimension);
    if (objects.isEmpty()) {
        ValueTypedDimensionalItemObject eventDimensionalItemObject = dataElementService.getDataElement(dimension);
        if (eventDimensionalItemObject == null) {
            eventDimensionalItemObject = trackedEntityAttributeService.getTrackedEntityAttribute(dimension);
        }
        addEventReportDimensionalItems(eventDimensionalItemObject, objects, grid, dimension);
        table.put(eventDimensionalItemObject.getDisplayProperty(params.getDisplayProperty()), objects);
    } else {
        table.put(dimension, objects);
    }
}
Also used : EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject)

Example 2 with EventAnalyticsDimensionalItem

use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.

the class EventAnalyticsUtils method recurse.

/**
 * A recursive method which finds all permutations of the elements in map.
 *
 * @param map the map with all values
 * @param iter iterator with keys
 * @param cur the current map
 * @param list the resulting list
 */
public static void recurse(Map<String, List<EventAnalyticsDimensionalItem>> map, ListIterator<String> iter, TreeMap<String, EventAnalyticsDimensionalItem> cur, List<Map<String, EventAnalyticsDimensionalItem>> list) {
    if (!iter.hasNext()) {
        Map<String, EventAnalyticsDimensionalItem> entry = new HashMap<>();
        for (String key : cur.keySet()) {
            entry.put(key, cur.get(key));
        }
        list.add(entry);
    } else {
        String key = iter.next();
        List<EventAnalyticsDimensionalItem> set = map.get(key);
        for (EventAnalyticsDimensionalItem value : set) {
            cur.put(key, value);
            recurse(map, iter, cur, list);
            cur.remove(key);
        }
        iter.previous();
    }
}
Also used : EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) HashMap(java.util.HashMap)

Example 3 with EventAnalyticsDimensionalItem

use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.

the class EventAnalyticsUtilsTest method testGenerateEventDataPermutations.

@Test
void testGenerateEventDataPermutations() {
    Map<String, List<EventAnalyticsDimensionalItem>> tableRows = new LinkedHashMap<>();
    Grid grid = new ListGrid();
    DataElement deA = createDataElement('A');
    deA.setValueType(ValueType.BOOLEAN);
    grid.addMetaData(deA.getUid(), deA);
    TrackedEntityAttribute trackedEntityAttribute = createTrackedEntityAttribute('B');
    OptionSet optionSet = new OptionSet();
    optionSet.addOption(new Option("name", "code"));
    trackedEntityAttribute.setOptionSet(optionSet);
    grid.addMetaData(trackedEntityAttribute.getUid(), trackedEntityAttribute);
    List<EventAnalyticsDimensionalItem> objects = new ArrayList<>();
    Option t = new Option();
    t.setCode("1");
    t.setName("Yes");
    Option f = new Option();
    f.setCode("0");
    f.setName("No");
    objects.add(new EventAnalyticsDimensionalItem(t, deA.getUid()));
    objects.add(new EventAnalyticsDimensionalItem(f, deA.getUid()));
    objects.add(new EventAnalyticsDimensionalItem(new Option("name", "code"), trackedEntityAttribute.getUid()));
    tableRows.put(deA.getUid(), objects);
    tableRows.put(trackedEntityAttribute.getDimensionItem(), objects);
    List<Map<String, EventAnalyticsDimensionalItem>> rowPermutations = EventAnalyticsUtils.generateEventDataPermutations(tableRows);
    assertEquals(9, rowPermutations.size());
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) ListGrid(org.hisp.dhis.system.grid.ListGrid) LinkedHashMap(java.util.LinkedHashMap) DataElement(org.hisp.dhis.dataelement.DataElement) EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) ArrayList(java.util.ArrayList) List(java.util.List) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 4 with EventAnalyticsDimensionalItem

use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem 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 5 with EventAnalyticsDimensionalItem

use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem 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)

Aggregations

EventAnalyticsDimensionalItem (org.hisp.dhis.analytics.EventAnalyticsDimensionalItem)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)2 Grid (org.hisp.dhis.common.Grid)2 MetadataItem (org.hisp.dhis.common.MetadataItem)2 ValueTypedDimensionalItemObject (org.hisp.dhis.common.ValueTypedDimensionalItemObject)2 Option (org.hisp.dhis.option.Option)2 ListGrid (org.hisp.dhis.system.grid.ListGrid)2 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)1 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)1 DimensionalObject (org.hisp.dhis.common.DimensionalObject)1 EventAnalyticalObject (org.hisp.dhis.common.EventAnalyticalObject)1 GridHeader (org.hisp.dhis.common.GridHeader)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 Legend (org.hisp.dhis.legend.Legend)1 OptionSet (org.hisp.dhis.option.OptionSet)1 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)1