Search in sources :

Example 11 with GridHeader

use of org.hisp.dhis.common.GridHeader 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 12 with GridHeader

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

the class ReportTable method addHierarchyColumns.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Adds grid columns for each organisation unit level.
     */
@SuppressWarnings("unchecked")
private void addHierarchyColumns(Grid grid, int ouIdColumnIndex) {
    Map<Object, List<?>> ancestorMap = (Map<Object, List<?>>) grid.getInternalMetaData().get(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey());
    Assert.notEmpty(ancestorMap, "Ancestor map cannot be null or empty when show hierarchy is enabled");
    int newColumns = ancestorMap.values().stream().mapToInt(List::size).max().orElseGet(() -> 0);
    List<GridHeader> headers = new ArrayList<>();
    for (int i = 0; i < newColumns; i++) {
        int level = i + 1;
        String name = String.format("Org unit level %d", level);
        String column = String.format("orgunitlevel%d", level);
        headers.add(new GridHeader(name, column, ValueType.TEXT, String.class.getName(), false, true));
    }
    grid.addHeaders(ouIdColumnIndex, headers);
    grid.addAndPopulateColumnsBefore(ouIdColumnIndex, ancestorMap, newColumns);
}
Also used : ArrayList(java.util.ArrayList) 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) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) GridHeader(org.hisp.dhis.common.GridHeader)

Example 13 with GridHeader

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

the class GridTest method testAddHeaderList.

@Test
public void testAddHeaderList() {
    Grid grid = new ListGrid();
    GridHeader headerA = new GridHeader("DataElementA", "Data element A");
    GridHeader headerB = new GridHeader("DataElementB", "Data element B");
    GridHeader headerC = new GridHeader("DataElementC", "Data element C");
    GridHeader headerD = new GridHeader("DataElementC", "Data element D");
    GridHeader headerE = new GridHeader("DataElementC", "Data element E");
    grid.addHeader(headerA);
    grid.addHeader(headerB);
    assertEquals(2, grid.getHeaders().size());
    List<GridHeader> headers = Lists.newArrayList(headerC, headerD, headerE);
    grid.addHeaders(1, headers);
    assertEquals(5, grid.getHeaders().size());
    assertEquals(headerA, grid.getHeaders().get(0));
    assertEquals(headerC, grid.getHeaders().get(1));
    assertEquals(headerD, grid.getHeaders().get(2));
    assertEquals(headerE, grid.getHeaders().get(3));
    assertEquals(headerB, grid.getHeaders().get(4));
}
Also used : Grid(org.hisp.dhis.common.Grid) GridHeader(org.hisp.dhis.common.GridHeader) Test(org.junit.Test)

Example 14 with GridHeader

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

the class AbstractEventService method getEventsGrid.

@Override
public Grid getEventsGrid(EventSearchParams params) {
    List<OrganisationUnit> organisationUnits = getOrganisationUnits(params);
    // ---------------------------------------------------------------------
    if (params.getDataElements().isEmpty() && params.getProgramStage() != null && params.getProgramStage().getProgramStageDataElements() != null) {
        for (ProgramStageDataElement pde : params.getProgramStage().getProgramStageDataElements()) {
            if (pde.getDisplayInReports()) {
                QueryItem qi = new QueryItem(pde.getDataElement(), pde.getDataElement().getLegendSet(), pde.getDataElement().getValueType(), pde.getDataElement().getAggregationType(), pde.getDataElement().hasOptionSet() ? pde.getDataElement().getOptionSet() : null);
                params.getDataElements().add(qi);
            }
        }
    }
    // ---------------------------------------------------------------------
    // Grid headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    for (String col : STATIC_EVENT_COLUMNS) {
        grid.addHeader(new GridHeader(col, col));
    }
    for (QueryItem item : params.getDataElements()) {
        grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
    }
    List<Map<String, String>> events = eventStore.getEventsGrid(params, organisationUnits);
    for (Map<String, String> event : events) {
        grid.addRow();
        for (String col : STATIC_EVENT_COLUMNS) {
            grid.addValue(event.get(col));
        }
        for (QueryItem item : params.getDataElements()) {
            grid.addValue(event.get(item.getItemId()));
        }
    }
    Map<String, Object> metaData = new HashMap<>();
    if (params.isPaging()) {
        int count = 0;
        if (params.isTotalPages()) {
            count = eventStore.getEventCount(params, organisationUnits);
        }
        Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        metaData.put(PAGER_META_KEY, pager);
    }
    grid.setMetaData(metaData);
    return grid;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) 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) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) Pager(org.hisp.dhis.common.Pager) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Map(java.util.Map) HashMap(java.util.HashMap) CachingMap(org.hisp.dhis.commons.collection.CachingMap)

Example 15 with GridHeader

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

the class GridUtils method toPdfInternal.

private static void toPdfInternal(Grid grid, Document document, float spacing) {
    if (grid == null || grid.getVisibleWidth() == 0) {
        return;
    }
    PdfPTable table = new PdfPTable(grid.getVisibleWidth());
    table.setHeaderRows(1);
    table.setWidthPercentage(100F);
    table.setKeepTogether(false);
    table.setSpacingAfter(spacing);
    table.addCell(resetPaddings(getTitleCell(grid.getTitle(), grid.getVisibleWidth()), 0, 30, 0, 0));
    if (StringUtils.isNotEmpty(grid.getSubtitle())) {
        table.addCell(getSubtitleCell(grid.getSubtitle(), grid.getVisibleWidth()));
        table.addCell(getEmptyCell(grid.getVisibleWidth(), 30));
    }
    for (GridHeader header : grid.getVisibleHeaders()) {
        table.addCell(getItalicCell(header.getName()));
    }
    table.addCell(getEmptyCell(grid.getVisibleWidth(), 10));
    for (List<Object> row : grid.getVisibleRows()) {
        for (Object col : row) {
            table.addCell(getTextCell(col));
        }
    }
    addTableToDocument(document, table);
}
Also used : PdfPTable(com.lowagie.text.pdf.PdfPTable) GridHeader(org.hisp.dhis.common.GridHeader)

Aggregations

GridHeader (org.hisp.dhis.common.GridHeader)27 Grid (org.hisp.dhis.common.Grid)14 ArrayList (java.util.ArrayList)9 ListGrid (org.hisp.dhis.system.grid.ListGrid)9 DimensionalObject (org.hisp.dhis.common.DimensionalObject)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)3 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 QueryItem (org.hisp.dhis.common.QueryItem)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)2 MetadataObject (org.hisp.dhis.common.MetadataObject)2 Pager (org.hisp.dhis.common.Pager)2 Period (org.hisp.dhis.period.Period)2 CsvWriter (com.csvreader.CsvWriter)1 PdfPTable (com.lowagie.text.pdf.PdfPTable)1