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;
}
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++;
}
}
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();
}
}
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;
}
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;
}
Aggregations