Search in sources :

Example 6 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class DefaultAnalyticsService method getAggregatedDataValuesTableLayout.

/**
     * Returns a Grid with aggregated data in table layout.
     *
     * @param params the {@link DataQueryParams}.
     * @param columns the column dimensions.
     * @param rows the row dimensions.
     * @return a Grid with aggregated data in table layout.
     */
private Grid getAggregatedDataValuesTableLayout(DataQueryParams params, List<String> columns, List<String> rows) {
    params.setOutputIdScheme(null);
    Grid grid = getAggregatedDataValues(params);
    ListUtils.removeEmptys(columns);
    ListUtils.removeEmptys(rows);
    queryPlanner.validateTableLayout(params, columns, rows);
    ReportTable reportTable = new ReportTable();
    List<DimensionalItemObject[]> tableColumns = new ArrayList<>();
    List<DimensionalItemObject[]> tableRows = new ArrayList<>();
    if (columns != null) {
        for (String dimension : columns) {
            reportTable.getColumnDimensions().add(dimension);
            tableColumns.add(params.getDimensionItemArrayExplodeCoc(dimension));
        }
    }
    if (rows != null) {
        for (String dimension : rows) {
            reportTable.getRowDimensions().add(dimension);
            tableRows.add(params.getDimensionItemArrayExplodeCoc(dimension));
        }
    }
    reportTable.setGridTitle(IdentifiableObjectUtils.join(params.getFilterItems())).setGridColumns(new CombinationGenerator<>(tableColumns.toArray(IRT2D)).getCombinations()).setGridRows(new CombinationGenerator<>(tableRows.toArray(IRT2D)).getCombinations());
    addListIfEmpty(reportTable.getGridColumns());
    addListIfEmpty(reportTable.getGridRows());
    reportTable.setHideEmptyRows(params.isHideEmptyRows());
    reportTable.setHideEmptyColumns(params.isHideEmptyColumns());
    reportTable.setShowHierarchy(params.isShowHierarchy());
    Map<String, Object> valueMap = AnalyticsUtils.getAggregatedDataValueMapping(grid);
    return reportTable.getGrid(new ListGrid(grid.getMetaData(), grid.getInternalMetaData()), valueMap, params.getDisplayProperty(), false);
}
Also used : CombinationGenerator(org.hisp.dhis.common.CombinationGenerator) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) ReportTable(org.hisp.dhis.reporttable.ReportTable) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) ListGrid(org.hisp.dhis.system.grid.ListGrid)

Example 7 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class AnalyticsUtilsTest method testGetDataValueSetFromGridWithDuplicates.

@Test
public void testGetDataValueSetFromGridWithDuplicates() {
    Grid grid = new ListGrid();
    grid.addHeader(new GridHeader(DimensionalObject.DATA_X_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.ORGUNIT_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.PERIOD_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID));
    grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peA", null, null, 1d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
    // Duplicate
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peA", null, null, 3d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peB", null, null, 4d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", null, null, 5d));
    // Duplicate
    grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", null, null, 5d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peB", null, null, 6d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, null, 7d));
    // Duplicate
    grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, null, 7d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peB", null, null, 8d));
    DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid(DataQueryParams.newBuilder().withDuplicatesOnly(true).build(), grid);
    assertNotNull(dvs);
    assertNotNull(dvs.getDataValues());
    assertEquals(3, dvs.getDataValues().size());
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) ListGrid(org.hisp.dhis.system.grid.ListGrid) ListGrid(org.hisp.dhis.system.grid.ListGrid) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 8 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class AnalyticsUtilsTest method testGetDataValueSetFromGrid.

@Test
public void testGetDataValueSetFromGrid() {
    Grid grid = new ListGrid();
    grid.addHeader(new GridHeader(DimensionalObject.DATA_X_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.ORGUNIT_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.PERIOD_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID));
    grid.addHeader(new GridHeader(DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID));
    grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peA", "coA", "aoA", 1d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peA", null, null, 3d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peB", null, null, 4d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", "coA", null, 5d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peB", "coA", "aoB", 6d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, "aoA", 7));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peB", null, null, 8d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxD", "ouA", "peA", "coB", null, 9d));
    grid.addRow().addValuesAsList(Lists.newArrayList("dxE", "ouA", "peB", null, null, 10));
    DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid(DataQueryParams.newBuilder().build(), grid);
    assertNotNull(dvs);
    assertNotNull(dvs.getDataValues());
    assertEquals(10, dvs.getDataValues().size());
    assertEquals("dxA", dvs.getDataValues().get(1).getDataElement());
    assertEquals("ouA", dvs.getDataValues().get(1).getOrgUnit());
    assertEquals("peB", dvs.getDataValues().get(1).getPeriod());
    assertNull(dvs.getDataValues().get(1).getCategoryOptionCombo());
    assertNull(dvs.getDataValues().get(1).getAttributeOptionCombo());
    assertEquals("2.0", dvs.getDataValues().get(1).getValue());
    assertEquals("dxB", dvs.getDataValues().get(4).getDataElement());
    assertEquals("ouA", dvs.getDataValues().get(4).getOrgUnit());
    assertEquals("peA", dvs.getDataValues().get(4).getPeriod());
    assertEquals("coA", dvs.getDataValues().get(4).getCategoryOptionCombo());
    assertNull(dvs.getDataValues().get(4).getAttributeOptionCombo());
    assertEquals("5.0", dvs.getDataValues().get(4).getValue());
    assertEquals("dxC", dvs.getDataValues().get(6).getDataElement());
    assertEquals("ouA", dvs.getDataValues().get(6).getOrgUnit());
    assertEquals("peA", dvs.getDataValues().get(6).getPeriod());
    assertNull(dvs.getDataValues().get(6).getCategoryOptionCombo());
    assertEquals("aoA", dvs.getDataValues().get(6).getAttributeOptionCombo());
    assertEquals("7", dvs.getDataValues().get(6).getValue());
    assertEquals("dxD", dvs.getDataValues().get(8).getDataElement());
    assertEquals("ouA", dvs.getDataValues().get(8).getOrgUnit());
    assertEquals("peA", dvs.getDataValues().get(8).getPeriod());
    assertEquals("coB", dvs.getDataValues().get(8).getCategoryOptionCombo());
    assertNull(dvs.getDataValues().get(8).getAttributeOptionCombo());
    assertEquals("9.0", dvs.getDataValues().get(8).getValue());
    assertEquals("dxE", dvs.getDataValues().get(9).getDataElement());
    assertEquals("ouA", dvs.getDataValues().get(9).getOrgUnit());
    assertEquals("peB", dvs.getDataValues().get(9).getPeriod());
    assertNull(dvs.getDataValues().get(9).getCategoryOptionCombo());
    assertNull(dvs.getDataValues().get(9).getAttributeOptionCombo());
    assertEquals("10", dvs.getDataValues().get(9).getValue());
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) ListGrid(org.hisp.dhis.system.grid.ListGrid) ListGrid(org.hisp.dhis.system.grid.ListGrid) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 9 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method getTrackedEntityInstancesGrid.

// TODO lower index on attribute value?
@Override
public Grid getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
    decideAccess(params);
    validate(params);
    handleAttributes(params);
    params.setUser(currentUserService.getCurrentUser());
    // ---------------------------------------------------------------------
    // Conform parameters
    // ---------------------------------------------------------------------
    params.conform();
    // ---------------------------------------------------------------------
    // Grid headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    grid.addHeader(new GridHeader(TRACKED_ENTITY_INSTANCE_ID, "Instance"));
    grid.addHeader(new GridHeader(CREATED_ID, "Created"));
    grid.addHeader(new GridHeader(LAST_UPDATED_ID, "Last updated"));
    grid.addHeader(new GridHeader(ORG_UNIT_ID, "Organisation unit"));
    grid.addHeader(new GridHeader(ORG_UNIT_NAME, "Organisation unit name"));
    grid.addHeader(new GridHeader(TRACKED_ENTITY_ID, "Tracked entity"));
    grid.addHeader(new GridHeader(INACTIVE_ID, "Inactive"));
    if (params.isIncludeDeleted()) {
        grid.addHeader(new GridHeader(DELETED, "Deleted", ValueType.BOOLEAN, "boolean", false, false));
    }
    for (QueryItem item : params.getAttributes()) {
        grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
    }
    List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstancesGrid(params);
    // ---------------------------------------------------------------------
    // Grid rows
    // ---------------------------------------------------------------------
    Set<String> tes = new HashSet<>();
    for (Map<String, String> entity : entities) {
        grid.addRow();
        grid.addValue(entity.get(TRACKED_ENTITY_INSTANCE_ID));
        grid.addValue(entity.get(CREATED_ID));
        grid.addValue(entity.get(LAST_UPDATED_ID));
        grid.addValue(entity.get(ORG_UNIT_ID));
        grid.addValue(entity.get(ORG_UNIT_NAME));
        grid.addValue(entity.get(TRACKED_ENTITY_ID));
        grid.addValue(entity.get(INACTIVE_ID));
        if (params.isIncludeDeleted()) {
            grid.addValue(entity.get(DELETED));
        }
        tes.add(entity.get(TRACKED_ENTITY_ID));
        for (QueryItem item : params.getAttributes()) {
            grid.addValue(entity.get(item.getItemId()));
        }
    }
    Map<String, Object> metaData = new HashMap<>();
    if (params.isPaging()) {
        int count = 0;
        if (params.isTotalPages()) {
            count = trackedEntityInstanceStore.getTrackedEntityInstanceCount(params);
        }
        Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        metaData.put(PAGER_META_KEY, pager);
    }
    if (!params.isSkipMeta()) {
        Map<String, String> names = new HashMap<>();
        for (String te : tes) {
            TrackedEntity entity = trackedEntityService.getTrackedEntity(te);
            names.put(te, entity != null ? entity.getDisplayName() : null);
        }
        metaData.put(META_DATA_NAMES_KEY, names);
    }
    grid.setMetaData(metaData);
    return grid;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) HashMap(java.util.HashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) Pager(org.hisp.dhis.common.Pager) DimensionalObject(org.hisp.dhis.common.DimensionalObject) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 10 with ListGrid

use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.

the class ReportTableTest method testGetGridShowHierarchy.

@Test
public void testGetGridShowHierarchy() {
    ReportTable reportTable = new ReportTable("Grid table", dataElements, new ArrayList<>(), new ArrayList<>(), periods, Lists.newArrayList(unitB, unitC), true, true, false, null, null, null);
    reportTable.setShowHierarchy(true);
    reportTable.init(null, null, null, null, null, i18nFormat);
    Map<Object, List<?>> ancestorMap = new HashMap<>();
    ancestorMap.put(unitB.getUid(), unitB.getAncestorNames(null, true));
    ancestorMap.put(unitC.getUid(), unitC.getAncestorNames(null, true));
    Map<String, Object> metaData = new HashMap<>();
    Map<String, Object> internalMetaData = new HashMap<>();
    internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
    Map<String, Object> valueMap = new HashMap<>();
    valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 11);
    valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 21);
    valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 12);
    valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 22);
    valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 13);
    valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 23);
    valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 14);
    valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 24);
    DisplayProperty property = DisplayProperty.NAME;
    Grid grid = reportTable.getGrid(new ListGrid(metaData, internalMetaData), valueMap, property, false);
    assertEquals(10, grid.getWidth());
    assertEquals(10, grid.getHeaders().size());
    assertEquals(2, grid.getHeight());
    assertEquals(unitA.getDisplayName(), grid.getValue(0, 0));
    assertEquals(unitB.getDisplayName(), grid.getValue(0, 1));
    assertEquals(unitB.getUid(), grid.getValue(0, 2));
    assertEquals(unitB.getDisplayProperty(property), grid.getValue(0, 3));
    assertEquals(unitB.getCode(), grid.getValue(0, 4));
    assertEquals(unitA.getDisplayName(), grid.getValue(1, 0));
    assertEquals(unitC.getDisplayName(), grid.getValue(1, 1));
    assertEquals(unitC.getUid(), grid.getValue(1, 2));
    assertEquals(unitC.getDisplayProperty(property), grid.getValue(1, 3));
    assertEquals(unitC.getCode(), grid.getValue(1, 4));
}
Also used : DisplayProperty(org.hisp.dhis.common.DisplayProperty) HashMap(java.util.HashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) ArrayList(java.util.ArrayList) List(java.util.List) ListGrid(org.hisp.dhis.system.grid.ListGrid) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

ListGrid (org.hisp.dhis.system.grid.ListGrid)24 Grid (org.hisp.dhis.common.Grid)16 GridHeader (org.hisp.dhis.common.GridHeader)9 DimensionalObject (org.hisp.dhis.common.DimensionalObject)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)5 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 List (java.util.List)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 DisplayProperty (org.hisp.dhis.common.DisplayProperty)2 Pager (org.hisp.dhis.common.Pager)2 QueryItem (org.hisp.dhis.common.QueryItem)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 DataValueSet (org.hisp.dhis.dxf2.datavalueset.DataValueSet)2