Search in sources :

Example 21 with GridHeader

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

the class ListGrid method addRegressionColumn.

@Override
public Grid addRegressionColumn(int columnIndex, boolean addHeader) {
    verifyGridState();
    SimpleRegression regression = new SimpleRegression();
    List<Object> column = getColumn(columnIndex);
    int index = 0;
    for (Object value : column) {
        if (value != null && !MathUtils.isEqual(Double.parseDouble(String.valueOf(value)), 0d)) {
            regression.addData(index++, Double.parseDouble(String.valueOf(value)));
        }
    }
    List<Object> regressionColumn = new ArrayList<>();
    for (int i = 0; i < column.size(); i++) {
        final double predicted = regression.predict(i);
        if (!Double.isNaN(predicted)) {
            regressionColumn.add(Precision.round(predicted, 1));
        } else {
            regressionColumn.add(null);
        }
    }
    addColumn(regressionColumn);
    if (addHeader && columnIndex < headers.size()) {
        GridHeader header = headers.get(columnIndex);
        if (header != null) {
            GridHeader regressionHeader = new GridHeader(header.getName() + REGRESSION_SUFFIX, header.getColumn() + REGRESSION_SUFFIX, header.getValueType(), header.getType(), header.isHidden(), header.isMeta());
            addHeader(regressionHeader);
        }
    }
    return this;
}
Also used : SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) ArrayList(java.util.ArrayList) GridHeader(org.hisp.dhis.common.GridHeader)

Example 22 with GridHeader

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

the class ListGrid method addCumulativeColumn.

@Override
public Grid addCumulativeColumn(int columnIndex, boolean addHeader) {
    verifyGridState();
    List<Object> column = getColumn(columnIndex);
    List<Object> cumulativeColumn = new ArrayList<>();
    double sum = 0d;
    for (Object value : column) {
        double number = value != null ? Double.parseDouble(String.valueOf(value)) : 0d;
        sum += number;
        cumulativeColumn.add(sum);
    }
    addColumn(cumulativeColumn);
    if (addHeader && columnIndex < headers.size()) {
        GridHeader header = headers.get(columnIndex);
        if (header != null) {
            GridHeader regressionHeader = new GridHeader(header.getName() + CUMULATIVE_SUFFIX, header.getColumn() + CUMULATIVE_SUFFIX, header.getValueType(), header.getType(), header.isHidden(), header.isMeta());
            addHeader(regressionHeader);
        }
    }
    return this;
}
Also used : ArrayList(java.util.ArrayList) GridHeader(org.hisp.dhis.common.GridHeader)

Example 23 with GridHeader

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

the class DefaultDataSetReportService method getSectionDataSetReport.

@Override
public List<Grid> getSectionDataSetReport(DataSet dataSet, Period period, OrganisationUnit unit, Set<String> dimensions, boolean selectedUnitOnly, I18nFormat format, I18n i18n) {
    List<Section> sections = new ArrayList<>(dataSet.getSections());
    Collections.sort(sections, new SectionOrderComparator());
    Map<String, Object> valueMap = dataSetReportStore.getAggregatedValues(dataSet, period, unit, dimensions);
    Map<String, Object> subTotalMap = dataSetReportStore.getAggregatedSubTotals(dataSet, period, unit, dimensions);
    Map<String, Object> totalMap = dataSetReportStore.getAggregatedTotals(dataSet, period, unit, dimensions);
    List<Grid> grids = new ArrayList<>();
    for (Section section : sections) {
        for (DataElementCategoryCombo categoryCombo : section.getCategoryCombos()) {
            Grid grid = new ListGrid().setTitle(section.getName() + SPACE + categoryCombo.getName()).setSubtitle(unit.getName() + SPACE + format.formatPeriod(period));
            // -----------------------------------------------------------------
            // Grid headers
            // -----------------------------------------------------------------
            grid.addHeader(new GridHeader(i18n.getString("dataelement"), false, true));
            List<DataElementCategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
            for (DataElementCategoryOptionCombo optionCombo : optionCombos) {
                grid.addHeader(new GridHeader(optionCombo.isDefault() ? DEFAULT_HEADER : optionCombo.getName(), false, false));
            }
            if (// Sub-total
            categoryCombo.doSubTotals() && !selectedUnitOnly) {
                for (DataElementCategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
                    grid.addHeader(new GridHeader(categoryOption.getName(), false, false));
                }
            }
            if (// Total
            categoryCombo.doTotal() && !selectedUnitOnly) {
                grid.addHeader(new GridHeader(TOTAL_HEADER, false, false));
            }
            // -----------------------------------------------------------------
            // Grid values
            // -----------------------------------------------------------------
            List<DataElement> dataElements = new ArrayList<>(section.getDataElementsByCategoryCombo(categoryCombo));
            FilterUtils.filter(dataElements, AggregatableDataElementFilter.INSTANCE);
            for (DataElement dataElement : dataElements) {
                grid.addRow();
                // Data element name
                grid.addValue(new GridValue(dataElement.getFormNameFallback()));
                for (// Values
                DataElementCategoryOptionCombo optionCombo : // Values
                optionCombos) {
                    Map<Object, Object> attributes = new HashMap<>();
                    attributes.put(ATTR_DE, dataElement.getUid());
                    attributes.put(ATTR_CO, optionCombo.getUid());
                    Object value = null;
                    if (selectedUnitOnly) {
                        DataValue dataValue = dataValueService.getDataValue(dataElement, period, unit, optionCombo);
                        value = dataValue != null && dataValue.getValue() != null ? Double.parseDouble(dataValue.getValue()) : null;
                    } else {
                        value = valueMap.get(dataElement.getUid() + SEPARATOR + optionCombo.getUid());
                    }
                    grid.addValue(new GridValue(value, attributes));
                }
                if (// Sub-total
                categoryCombo.doSubTotals() && !selectedUnitOnly) {
                    for (DataElementCategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
                        Object value = subTotalMap.get(dataElement.getUid() + SEPARATOR + categoryOption.getUid());
                        grid.addValue(new GridValue(value));
                    }
                }
                if (// Total
                categoryCombo.doTotal() && !selectedUnitOnly) {
                    Object value = totalMap.get(String.valueOf(dataElement.getUid()));
                    grid.addValue(new GridValue(value));
                }
            }
            grids.add(grid);
        }
    }
    return grids;
}
Also used : DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) HashMap(java.util.HashMap) DataValue(org.hisp.dhis.datavalue.DataValue) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) Section(org.hisp.dhis.dataset.Section) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) DataElement(org.hisp.dhis.dataelement.DataElement) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) GridValue(org.hisp.dhis.common.GridValue) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 24 with GridHeader

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

the class AbstractGridView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Object object = model.get("model");
    List<Grid> grids = new ArrayList<>();
    if (WebMetadata.class.isAssignableFrom(object.getClass())) {
        WebMetadata metadata = (WebMetadata) object;
        Collection<Field> fields = ReflectionUtils.collectFields(WebMetadata.class, PredicateUtils.idObjectCollections);
        for (Field field : fields) {
            List<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod(field.getName(), metadata);
            if (identifiableObjects == null || identifiableObjects.isEmpty()) {
                continue;
            }
            Grid grid = new ListGrid();
            grid.setTitle(identifiableObjects.get(0).getClass().getSimpleName() + "s");
            boolean nameable = false;
            grid.addHeader(new GridHeader("UID", false, false));
            grid.addHeader(new GridHeader("Name", false, false));
            if (NameableObject.class.isAssignableFrom(identifiableObjects.get(0).getClass())) {
                grid.addHeader(new GridHeader("ShortName", false, false));
                nameable = true;
            }
            grid.addHeader(new GridHeader("Code", false, false));
            for (IdentifiableObject identifiableObject : identifiableObjects) {
                grid.addRow();
                grid.addValue(identifiableObject.getUid());
                grid.addValue(identifiableObject.getName());
                if (nameable) {
                    grid.addValue(((NameableObject) identifiableObject).getShortName());
                }
                grid.addValue(identifiableObject.getCode());
            }
            grids.add(grid);
        }
    } else {
        IdentifiableObject identifiableObject = (IdentifiableObject) object;
        Grid grid = new ListGrid();
        grid.setTitle(identifiableObject.getClass().getSimpleName());
        grid.addEmptyHeaders(2);
        grid.addRow().addValue("UID").addValue(identifiableObject.getUid());
        grid.addRow().addValue("Name").addValue(identifiableObject.getName());
        if (NameableObject.class.isAssignableFrom(identifiableObject.getClass())) {
            grid.addRow().addValue("ShortName").addValue(((NameableObject) identifiableObject).getShortName());
            grid.addRow().addValue("Description").addValue(((NameableObject) identifiableObject).getDescription());
        }
        grid.addRow().addValue("Code").addValue(identifiableObject.getCode());
        grids.add(grid);
    }
    renderGrids(grids, response);
}
Also used : Field(java.lang.reflect.Field) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NameableObject(org.hisp.dhis.common.NameableObject) ListGrid(org.hisp.dhis.system.grid.ListGrid) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) GridHeader(org.hisp.dhis.common.GridHeader)

Example 25 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)

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