package org.swami.om2.neorepo.sparql;

import java.util.LinkedList;
import java.util.List;
import name.levering.ryan.sparql.common.RdfBindingRow;
import name.levering.ryan.sparql.common.RdfBindingSet;
import name.levering.ryan.sparql.common.Variable;
import org.neo4j.util.matching.PatternElement;
import org.neo4j.util.matching.PatternMatch;
import org.openrdf.model.Value;

public class NeoBindingRow implements RdfBindingRow
{
	private NeoRdfBindingSet bindingSet;
	private PatternMatch match;
	
	NeoBindingRow( NeoRdfBindingSet bindingSet, PatternMatch match )
	{
		this.bindingSet = bindingSet;
		this.match = match;
	}
	
	public RdfBindingSet getParentSet()
	{
		return this.bindingSet;
	}

	public Value getValue( Variable variable )
	{
		NeoVariable neoVariable = ( NeoVariable ) variable;
		for ( PatternElement element : this.match.getElements() )
		{
			if ( element.getPatternNode().equals( neoVariable.getNode() ) )
			{
				return new NeoValue( ( String )
					element.getNode().getProperty(
						neoVariable.getProperty() ) );
			}
		}
		return null;
	}

	public List<Value> getValues()
	{
		List<Value> values = new LinkedList<Value>();
		
		for ( NeoVariable variable :
			( List<NeoVariable> ) this.getVariables() )
		{
			for ( PatternElement element : this.match.getElements() )
			{
				if ( variable.getNode().equals( element.getPatternNode() ) )
				{
					values.add( new NeoValue( ( String )
						element.getNode().getProperty(
							variable.getProperty() ) ) );
					break;
				}
			}
		}
		
		return values;
	}

	public List<? extends Variable> getVariables()
	{
		return this.bindingSet.getVariables();
	}
	
	static class NeoValue implements Value
	{
		String value;
		
		NeoValue( String value )
		{
			this.value = value;
		}

		@Override
		public String toString()
		{
			return value;
		}
	}
}

@@ -947,35 +947,24 @@
 ew NeoValue(
- ( String )
 %0A%09%09%09%09%09elemen
@@ -1392,19 +1392,8 @@
 lue(
- ( String )
 %0A%09%09%09
@@ -1656,22 +1656,22 @@
 ue%0A%09%7B%0A%09%09
-String
+Object
  value;%0A
@@ -1685,22 +1685,22 @@
 oValue( 
-String
+Object
  value )
@@ -1786,23 +1786,34 @@
 return value
+.toString()
 ;%0A%09%09%7D%0A%09%7D%0A%7D%0A

Doesn't assume that property values are Strings