package org.ontoware.rdf2go.impl.jena24;

import org.ontoware.aifbcommons.collection.ClosableIterator;
import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.QueryRow;
import org.ontoware.rdf2go.model.impl.QueryRowImpl;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;

public class QueryIterator implements ClosableIterator<QueryRow> {

	private ResultSet resultSet;

	private QueryResultTableImpl table;

	public QueryIterator(QueryResultTableImpl table, ResultSet results) {
		this.resultSet = results;
		this.table = table;
	}

	public boolean hasNext() {
		return this.resultSet.hasNext();
	}

	public QueryRow next() {
		QuerySolution qs = this.resultSet.nextSolution();
		QueryRowImpl row = new QueryRowImpl();
		for (String v : this.table.getVariables()) {
			RDFNode node = qs.get(v);
			assert node != null : "null node for varname " + v
					+ ". Do you have unbound variables in the query?";
			try {
				row.put(v, TypeConversion.toRDF2Go(node.asNode()));
			} catch (ModelRuntimeException e) {
				throw new ModelRuntimeException( e );
			}
		}
		return row;
	}

	public void remove() {
		this.resultSet.remove();
	}

	public void close() {
	}

}

@@ -726,32 +726,158 @@
 sNext();%0D%0A%09%7D%0D%0A%0D%0A
+%09@SuppressWarnings(%22null%22)%0D%0A%09/*%0D%0A%09 * patch from garpinc against nulls -%0D%0A%09 * http://issues.semweb4j.org/browse/RTGO-65%0D%0A%09 */%0D%0A
 %09public QueryRow
@@ -1221,20 +1221,51 @@
 oRDF2Go(
+(
 node
+ == null ? null : node%0D%0A%09%09%09%09%09%09
 .asNode(
@@ -1267,16 +1267,17 @@
 Node()))
+)
 ;%0D%0A%09%09%09%7D 
@@ -1351,11 +1351,9 @@
 ion(
- e 
+e
 );%0D%0A

patch from garpinc against nulls - http://issues.semweb4j.org/browse/RTGO-65