All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ms.log4jdbc.writer.resultset.ResultSetPrinterIterator Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.writer.resultset;

import java.util.Iterator;

import fr.ms.log4jdbc.message.resultset.Cell;
import fr.ms.log4jdbc.message.resultset.Column;
import fr.ms.log4jdbc.message.resultset.ResultSetCollector;
import fr.ms.log4jdbc.message.resultset.Row;
import fr.ms.log4jdbc.utils.StringUtils;

/**
 * 
 * @see Marco4J
 * 
 * 
 * @author Marco Semiao
 * 
 */
public class ResultSetPrinterIterator implements Iterator {

    private final static String nl = System.getProperty("line.separator");

    private final ResultSetCollector resultSetCollector;

    private final ResultSetPrinterFormatCell formatCell;

    private int maxRow;

    private int[] maxLength;

    private boolean header;

    private boolean allData;

    private boolean next = true;

    private int position = 0;

    private boolean nextFull;

    public ResultSetPrinterIterator(
	    final ResultSetCollector resultSetCollector,
	    final ResultSetPrinterFormatCell formatCell, final int maxRow) {
	this.resultSetCollector = resultSetCollector;
	this.formatCell = formatCell;
	this.maxRow = maxRow;

	final Row[] rows = resultSetCollector != null
		&& resultSetCollector.isClosed() ? resultSetCollector.getRows()
		: null;

	if (rows != null && rows.length != 0) {
	    maxLength = getMaxLength();

	    if (maxRow > rows.length) {
		nextFull = true;
	    } else {
		nextFull = false;
	    }
	} else {
	    next = false;
	}
    }

    public boolean hasNext() {
	return next;
    }

    public Object next() {
	if (nextFull) {
	    return nextFull();
	} else {
	    return nextItr();
	}
    }

    private Object nextFull() {
	final StringBuffer sb = new StringBuffer();

	sb.append(getHeader());
	sb.append(nl);
	final Row[] rows = resultSetCollector.getRows();
	if (rows != null) {
	    sb.append(getData(position, rows.length));
	}
	sb.append(nl);
	sb.append(getFooter());

	next = false;

	return sb.toString();
    }

    private Object nextItr() {
	if (!header) {
	    header = true;
	    return getHeader();
	}

	final Row[] rows = resultSetCollector.getRows();

	if (rows != null && !allData) {

	    final int maxElement = rows.length;
	    if (position + maxRow > maxElement) {
		maxRow = maxElement - position;
		allData = true;
	    }

	    final String data = getData(position, maxRow);

	    position = position + maxRow;
	    return data;
	}

	next = false;
	return getFooter();
    }

    public void remove() {
	throw new UnsupportedOperationException();
    }

    private int[] getMaxLength() {
	final Column[] columnsDetail = resultSetCollector.getColumns();
	final Row[] rowsDetail = resultSetCollector.getRows();

	final int columnCount = columnsDetail.length;
	final int maxLength[] = new int[columnCount];

	for (int column = 1; column <= columnCount; column++) {
	    final Column columnDetail = columnsDetail[column - 1];
	    maxLength[column - 1] = columnDetail.getLabel().length();
	}

	if (rowsDetail != null && rowsDetail.length != 0) {
	    for (int i = 0; i < rowsDetail.length; i++) {

		for (int column = 1; column <= columnCount; column++) {
		    final Cell cell = rowsDetail[i].getValue(column);
		    if (cell != null) {
			final Object obj = cell.getValue();
			final String value = formatCell.formatValue(obj);
			if (value != null) {
			    final int length = value.length();
			    if (length > maxLength[column - 1]) {
				maxLength[column - 1] = length;
			    }
			}
		    }
		}
	    }
	}

	if (columnsDetail != null && columnsDetail.length != 0
		&& rowsDetail != null && rowsDetail.length != 0) {
	    for (int column = 1; column <= columnCount; column++) {
		maxLength[column - 1] = maxLength[column - 1] + 1;
	    }
	}

	return maxLength;
    }

    private String getHeader() {

	final Column[] columnsDetail = resultSetCollector.getColumns();
	final int columnCount = columnsDetail.length;

	final StringBuffer sb = new StringBuffer();

	sb.append(System.getProperty("line.separator"));
	sb.append("|");
	for (int column = 1; column <= columnCount; column++) {
	    sb.append(StringUtils.padRight("-", maxLength[column - 1]) + "|");
	}

	sb.append(System.getProperty("line.separator"));
	sb.append("|");
	for (int column = 1; column <= columnCount; column++) {
	    final Column columnDetail = columnsDetail[column - 1];
	    sb.append(StringUtils.padRight(columnDetail.getLabel(), " ",
		    maxLength[column - 1]) + "|");
	}

	sb.append(System.getProperty("line.separator"));
	sb.append("|");
	for (int column = 1; column <= columnCount; column++) {
	    sb.append(StringUtils.padRight("-", maxLength[column - 1]) + "|");
	}

	return sb.toString();
    }

    private String getData(final int position, final int length) {

	final Row[] rowsDetail = resultSetCollector.getRows();
	final Column[] columnsDetail = resultSetCollector.getColumns();
	final int columnCount = columnsDetail.length;

	final StringBuffer sb = new StringBuffer();

	if (rowsDetail != null && rowsDetail.length != 0) {
	    for (int i = position; i < position + length; i++) {
		int colIndex = 0;
		sb.append("|");
		for (int column = 1; column <= columnCount; column++) {
		    final Cell cell = rowsDetail[i].getValue(column);

		    String value;
		    if (cell == null) {
			value = "UNREAD";
		    } else {
			final Object obj = cell.getValue();
			value = formatCell.formatValue(obj);
		    }
		    sb.append(StringUtils.padRight(value, " ",
			    maxLength[colIndex]) + "|");
		    colIndex++;
		}
		if (i < position + length - 1) {
		    sb.append(System.getProperty("line.separator"));
		}
	    }
	}
	return sb.toString();
    }

    private String getFooter() {

	final Column[] columnsDetail = resultSetCollector.getColumns();
	final int columnCount = columnsDetail.length;

	final StringBuffer sb = new StringBuffer();

	sb.append("|");
	for (int column = 1; column <= columnCount; column++) {
	    sb.append(StringUtils.padRight("-", maxLength[column - 1]) + "|");
	}

	sb.append(System.getProperty("line.separator"));

	return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy