/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.prototype.queryresults;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Element;
import reportgen.utils.SupportXMLRootImpl;
import reportgen.utils.ReportException;
/**
*
* @author axe
*/
public class QueryResults implements Serializable {
public static final long serialVersionUID = 1;
public static final String TAG = "results";
private static final String ATTR_COLS = "cols";
private List<ResultsRow> rows;
private int cols;
public QueryResults(int cols, List<ResultsRow> resultRows) throws ReportException {
if(resultRows.size() > 0
&& resultRows.get(0).getWidth() != cols) {
throw new ReportException("Результат отчета не соответствует отчету.");
}
rows = resultRows;
this.cols = cols;
}
public QueryResults(Element element) throws ReportException {
if(!element.getName().equals(TAG)) {
throw new ReportException("Обнаружен некорректный элемент");
}
cols = SupportXMLRootImpl.getIntAttribute(element, ATTR_COLS);
if(cols == 0) {
throw new ReportException("Атрибут " + ATTR_COLS + " имеет некорректное значение: " + cols);
}
rows = new ArrayList<ResultsRow>();
List children = element.getChildren(ResultsRow.ROWTAG);
for(Object child : children) {
ResultsRow row = new ResultsRow((Element) child);
if(row.getWidth() != cols) {
throw new ReportException("Обнаружен некорректный элемент ResultsRow");
}
rows.add(row);
}
}
//@todo
public Element toXML() {
Element root = new Element(TAG);
root.setAttribute(ATTR_COLS, new Integer(cols).toString());
for(int i=0; i<rows.size(); i++) {
ResultsRow rr = rows.get(i);
root.addContent(rr.toXML());
}
return root;
}
public int getRowCount() {
return rows.size();
}
public ResultsRow getRow(int index) {
return rows.get(index);
}
/**
*
* @param selection
*/
public void shrinkTo(ArrayList<Integer> selection) {
List<ResultsRow> selectedRows = new ArrayList<ResultsRow>();
for(int i: selection) {
selectedRows.add(rows.get(i));
}
rows = selectedRows;
}
}