package eu.dnetlib.dlms.view;
import java.util.Vector;

import com.hp.hpl.jena.graph.Node;

public class NodeView {
	private String				relationToArrive;
	private String 				nodeName;
	private Vector<NodeView>	nodeRelations;
	private NodeView			fatherNode;
	

	public NodeView()
	{
		nodeRelations= new Vector<NodeView>();		
	}
	
	public String getNodeName() 
	{
		return nodeName;
	}


	public void setNodeName(String nodeName) 
	{
		this.nodeName = nodeName;
	}
	
	public String getRelationToArrive() 
	{
		return relationToArrive;
	}

	public void setRelationToArrive(String relationToArrive) 
	{
		this.relationToArrive = relationToArrive;
	}
	
	public void addNewRelation (String nameRelation)
	{
		NodeView newNode= new NodeView();
		newNode.setRelationToArrive(nameRelation);
		newNode.setFatherNode(this);
		nodeRelations.add(newNode);
	}
	
	public NodeView getLastNode()
	{
		return (nodeRelations.size()>0) ? nodeRelations.lastElement():null;
	}
	

	public NodeView getFatherNode() {
		return fatherNode;
	}

	public void setFatherNode(NodeView fatherNode) {
		this.fatherNode = fatherNode;
	}
	
	public boolean typeChecking(QueryManager qmanager)
	{
		if(qmanager.existSet(nodeName, null)==false)
			return false;
		if (relationToArrive!=null)
		{
			if(qmanager.relationIsValid(relationToArrive, fatherNode.getNodeName(), nodeName, null)==false)
				return false;
		}
		else
		{
			if(fatherNode!=null)
			{
				NodeView tmp= fatherNode.getFatherNode();
				if((tmp!=null)&&(fatherNode.getRelationToArrive()==null))
				{
					if(qmanager.relationIsValid(fatherNode.nodeName, tmp.getNodeName(), nodeName,null)==false)
						return false;
				}
			}
		}
				
		
		for(int i=0; i<nodeRelations.size();i++)
		{
			if(nodeRelations.elementAt(i).typeChecking(qmanager)==false)
				return false;
		}
		return true;
	}
	
	public void debug()
	{
		System.out.println("*************************");
		System.out.println("Nodo  "+nodeName);
		for(int i=0;i<nodeRelations.size();i++)
		{
			if(nodeRelations.elementAt(i).getRelationToArrive()==null)
				System.out.print("Relazione attiva ");
			else
				System.out.print(nodeRelations.elementAt(i).getRelationToArrive()+" ");
			System.out.println("--->"+nodeRelations.elementAt(i).getNodeName());
		}
		for(int i=0;i<nodeRelations.size();i++)
		{
			nodeRelations.elementAt(i).debug();
		}
		
	}
	
	
	public void generateXML(StringBuffer theOutput, boolean isNode)
	{
		if (isNode) 
		{
			if (nodeRelations.size() == 0)
				theOutput.append("\"/>\n");
			else 
			{
				theOutput.append(">\n");
				for (NodeView i : nodeRelations) 
				{
					if (i.relationToArrive == null) 
					{
						theOutput.append("<visitby label=\"");
						theOutput.append(i.nodeName);
						theOutput.append("\">\n");
						i.generateXML(theOutput, false);
					}
					else 
					{
						
						theOutput.append("<visitby label=\"");
						theOutput.append(i.relationToArrive);
						theOutput.append("\">\n");
						theOutput.append("<node name=\"");
						theOutput.append(i.nodeName);
						theOutput.append("\"");
						i.generateXML(theOutput, true);
						theOutput.append("</visitby>");
					}					

				}
				if(fatherNode!=null)
					theOutput.append("</node>\n");
			}
			
		}
		else
		{
			for(NodeView i:nodeRelations)
			{
				theOutput.append("<node name=\"");
				theOutput.append(i.nodeName);
				i.generateXML(theOutput, true);
				
			}
			theOutput.append("</visitby>");
		}
	}

}
