Search in sources :

Example 16 with GridHeader

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

the class GridUtils method fromHtml.

/**
     * Creates a list of Grids based on the given HTML string. This works only
     * for table-based HTML documents.
     *
     * @param html the HTML string.
     * @param title the title to use for the grids.
     * @return a list of Grids.
     */
public static List<Grid> fromHtml(String html, String title) throws Exception {
    if (html == null || html.trim().isEmpty()) {
        return null;
    }
    List<Grid> grids = new ArrayList<>();
    Parser parser = Parser.createParser(html, "UTF-8");
    Node[] tables = parser.extractAllNodesThatMatch(new TagNameFilter("table")).toNodeArray();
    for (Node t : tables) {
        Grid grid = new ListGrid();
        grid.setTitle(title);
        TableTag table = (TableTag) t;
        TableRow[] rows = table.getRows();
        Integer firstColumnCount = null;
        for (TableRow row : rows) {
            if (// Ignore if no cells
            getColumnCount(row) == 0) {
                log.warn("Ignoring row with no columns");
                continue;
            }
            Node[] cells = row.getChildren().extractAllNodesThatMatch(HTML_ROW_FILTER).toNodeArray();
            if (// First row becomes header
            firstColumnCount == null) {
                firstColumnCount = getColumnCount(row);
                for (Node c : cells) {
                    TagNode cell = (TagNode) c;
                    grid.addHeader(new GridHeader(getValue(cell), false, false));
                    Integer colSpan = MathUtils.parseInt(cell.getAttribute("colspan"));
                    if (colSpan != null && colSpan > 1) {
                        grid.addEmptyHeaders((colSpan - 1));
                    }
                }
            } else // Rest becomes rows
            {
                if (// Ignore
                firstColumnCount != getColumnCount(row)) {
                    log.warn("Ignoring row which has " + row.getColumnCount() + " columns since table has " + firstColumnCount + " columns");
                    continue;
                }
                grid.addRow();
                for (Node c : cells) {
                    // TODO row span
                    TagNode cell = (TagNode) c;
                    grid.addValue(getValue(cell));
                    Integer colSpan = MathUtils.parseInt(cell.getAttribute("colspan"));
                    if (colSpan != null && colSpan > 1) {
                        grid.addEmptyValues((colSpan - 1));
                    }
                }
            }
        }
        grids.add(grid);
    }
    return grids;
}
Also used : Grid(org.hisp.dhis.common.Grid) TagNode(org.htmlparser.nodes.TagNode) Node(org.htmlparser.Node) ArrayList(java.util.ArrayList) Parser(org.htmlparser.Parser) GridHeader(org.hisp.dhis.common.GridHeader) TableTag(org.htmlparser.tags.TableTag) TableRow(org.htmlparser.tags.TableRow) TagNameFilter(org.htmlparser.filters.TagNameFilter) TagNode(org.htmlparser.nodes.TagNode)

Example 17 with GridHeader

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

the class GridUtils method toXlsInternal.

private static void toXlsInternal(Grid grid, WritableWorkbook workbook, String sheetName, int sheetNo) throws Exception {
    if (grid == null) {
        return;
    }
    int cols = grid.getVisibleHeaders().size();
    if (cols > JXL_MAX_COLS) {
        log.warn("Grid will be truncated, no of columns is greater than JXL max limit: " + cols + "/" + JXL_MAX_COLS);
    }
    WritableSheet sheet = workbook.createSheet(sheetName, sheetNo);
    int rowNumber = 0;
    int columnIndex = 0;
    if (StringUtils.isNotEmpty(grid.getTitle())) {
        sheet.addCell(new Label(0, rowNumber++, grid.getTitle(), XLS_FORMAT_TTTLE));
        rowNumber++;
    }
    if (StringUtils.isNotEmpty(grid.getSubtitle())) {
        sheet.addCell(new Label(0, rowNumber++, grid.getSubtitle(), XLS_FORMAT_TTTLE));
        rowNumber++;
    }
    List<GridHeader> headers = ListUtils.subList(grid.getVisibleHeaders(), 0, JXL_MAX_COLS);
    for (GridHeader header : headers) {
        sheet.addCell(new Label(columnIndex++, rowNumber, header.getName(), XLS_FORMAT_LABEL));
    }
    rowNumber++;
    for (List<Object> row : grid.getVisibleRows()) {
        columnIndex = 0;
        List<Object> colums = ListUtils.subList(row, 0, JXL_MAX_COLS);
        for (Object column : colums) {
            if (column != null && MathUtils.isNumeric(String.valueOf(column))) {
                sheet.addCell(new Number(columnIndex++, rowNumber, Double.valueOf(String.valueOf(column)), XLS_FORMAT_TEXT));
            } else {
                String content = column != null ? String.valueOf(column) : EMPTY;
                sheet.addCell(new Label(columnIndex++, rowNumber, content, XLS_FORMAT_TEXT));
            }
        }
        rowNumber++;
    }
}
Also used : Number(jxl.write.Number) Label(jxl.write.Label) WritableSheet(jxl.write.WritableSheet) JasperPrint(net.sf.jasperreports.engine.JasperPrint) GridHeader(org.hisp.dhis.common.GridHeader)

Example 18 with GridHeader

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

the class GridUtils method toCsv.

/**
     * Writes a CSV representation of the given Grid to the given OutputStream.
     */
public static void toCsv(Grid grid, Writer writer) throws IOException {
    if (grid == null) {
        return;
    }
    CsvWriter csvWriter = new CsvWriter(writer, CSV_DELIMITER);
    Iterator<GridHeader> headers = grid.getHeaders().iterator();
    if (!grid.getHeaders().isEmpty()) {
        while (headers.hasNext()) {
            csvWriter.write(headers.next().getColumn());
        }
        csvWriter.endRecord();
    }
    for (List<Object> row : grid.getRows()) {
        Iterator<Object> columns = row.iterator();
        while (columns.hasNext()) {
            Object value = columns.next();
            csvWriter.write(value != null ? String.valueOf(value) : StringUtils.EMPTY);
        }
        csvWriter.endRecord();
    }
}
Also used : CsvWriter(com.csvreader.CsvWriter) GridHeader(org.hisp.dhis.common.GridHeader)

Example 19 with GridHeader

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

the class ListGrid method addHeaders.

@Override
public Grid addHeaders(SqlRowSet rs) {
    SqlRowSetMetaData rsmd = rs.getMetaData();
    int columnNo = rsmd.getColumnCount();
    for (int i = 1; i <= columnNo; i++) {
        addHeader(new GridHeader(rsmd.getColumnLabel(i), false, false));
    }
    return this;
}
Also used : SqlRowSetMetaData(org.springframework.jdbc.support.rowset.SqlRowSetMetaData) GridHeader(org.hisp.dhis.common.GridHeader)

Example 20 with GridHeader

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

the class ListGrid method addHeaders.

// -------------------------------------------------------------------------
// SQL utility methods
// -------------------------------------------------------------------------
@Override
public Grid addHeaders(ResultSet rs) {
    try {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnNo = rsmd.getColumnCount();
        for (int i = 1; i <= columnNo; i++) {
            addHeader(new GridHeader(rsmd.getColumnLabel(i), false, false));
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
    return this;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) 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