/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 04-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.hial;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IInputNode;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IOutputNode;
import uk.ac.ic.doc.kevlar.core.framework.IContext;
import uk.ac.ic.doc.kevlar.core.framework.IPipe;
import uk.ac.ic.doc.kevlar.publicInterfaces.INodeID;
import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;

/**
 * @author Daniel Burke
 *
 */
public class HialPipe implements IHialPipe {

	private IPipe pipe;
	private IContext context;
	private HialContext hialContext;
	private long refID;
	
	public HialPipe(HialContext hialContext, IContext context, long refID)
	{
		this.context = context;
		this.hialContext = hialContext;
		this.refID = refID;
		this.pipe = context.getPipe(refID);
	}

	private void check()
	{
	    if(pipe.isDestroyed())
	    {
	        pipe = context.getPipe(refID);
	    }
	    
	    if(pipe == null)
	    {
	        throw new IllegalStateException(
	                "This pipe has been destroyed and may no longer be used");
	    }
	}
	

    public IOutputNode getFromNode()
    {
        synchronized(context)
        {
            check();
            return pipe.getFromNode();
        }
    }
    public INodeID getFromNodeID()
    {
        synchronized(context)
        {
            check();
            return pipe.getFromNodeID();
        }
    }
    
    public IHialProgram getFromProgram()
    {
        synchronized(context)
        {
            check();
            IRefObject fromRef = hialContext.convert(pipe.getFromProgram().getID());
            return hialContext.getProgram( fromRef );
        }
    }
    
    public IInputNode getToNode()
    {
        synchronized(context)
        {
            check();
            return pipe.getToNode();
        }
    }
    
    public INodeID getToNodeID()
    {
        synchronized(context)
        {
            check();
            return pipe.getToNodeID();
        }
    }
    
    public IHialProgram getToProgram()
    {
        synchronized(context)
        {
            check();
            IRefObject toRef = hialContext.convert(pipe.getToProgram().getID());
            return hialContext.getProgram( toRef );
        }
    }
    
    public boolean isDestroyed()
    {
        synchronized(context)
        {
            check();
            return pipe.isDestroyed();
        }
    }
   
	void putPipe(
			StringBuilder text, 
			IGUIContextSaver guiCallback, 
			int depth)
	{
		depth = XMLUtil.putOpenElem(text, "pipe", depth);
		
		depth = XMLUtil.putOpenElem(text, "from", depth);
		
		XMLUtil.putElement(text, "program", Long.toString(pipe.getFromProgram().getID()), depth);
		XMLUtil.putElement(text, "node", pipe.getFromNodeID().getFriendlyName(), depth);
		
		depth = XMLUtil.putCloseElem(text, "from", depth);

		depth = XMLUtil.putOpenElem(text, "to", depth);
		
		XMLUtil.putElement(text, "program", Long.toString(pipe.getToProgram().getID()), depth);
		XMLUtil.putElement(text, "node", pipe.getToNodeID().getFriendlyName(), depth);
		
		depth = XMLUtil.putCloseElem(text, "to", depth);
		
		
		depth = XMLUtil.putOpenElem(text, "gui-pipe", depth);
		guiCallback.putGUIPipe(text, hialContext.convert(refID), depth);
		depth = XMLUtil.putCloseElem(text, "gui-pipe", depth);
		
		depth = XMLUtil.putCloseElem(text, "pipe", depth);
	}
}
