package com.k_int.aggr2.mimsy.rdf;

import java.io.Writer;
import java.util.Iterator;
import java.util.Set;

import com.k_int.aggr2.mimsy.data.ExcavationDTO;
import com.k_int.aggr2.mimsy.data.InstructionDTO;
import com.k_int.aggr2.mimsy.data.MimsyDataDTO;
import com.k_int.aggr2.mimsy.data.MimsySiteDataDTO;
import com.k_int.aggr2.mimsy.data.conversion.AbstractSiteFormatConverter;
import com.k_int.mimsy.ref.DataException;

public class RDFSiteConverter extends AbstractSiteFormatConverter {

	private Writer writer;
	
	public RDFSiteConverter(Writer writer)
	{
		this.writer=writer;
	}
	@Override
	protected void convertExcavations(Set<ExcavationDTO> excavations)
			throws DataException {
		 if(excavations.size()==0)
			 return;
		 try
         {
         	writer.write("<mo:excavationLinks>");
         	Iterator<ExcavationDTO> i = excavations.iterator();
         	while(i.hasNext())
         	{
         		ExcavationDTO ref = i.next();
         		writer.write("<mo:excavationLink>");
         		String role="undefined";
         		if(ref.getRole()!=null)
         			role=ref.getRole();
         		convert("mo:excavationRole",role);
    			String org = "undefined";
    			if(ref.getOrganisation()!=null)
    				org=ref.getOrganisation();
    			convert("mo:excavationOrg",org);
               
    			String start = "undefined";
    			if(ref.getStart()!=null)
    				start=ref.getStart();
    			convert("mo:excavationStart",start);
    			
    			String end = "undefined";
    			if(ref.getEnd()!=null)
    				end=ref.getEnd();
    			convert("mo:excavationEnd",end);
    			writer.write("</mo:excavationLink>");
    	       
         	}
         	writer.write("</mo:excavationLinks>");            	
         }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }

	}

	@Override
	protected void convert(String elementVal, String val) throws DataException {
		try
		{
			RDFHelper.writeElement(writer,elementVal, null, val);
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}

	}

	@Override
	protected void convertConfAndLastUpdate(MimsyDataDTO data)
			throws DataException {
		try
		{
			RDFHelper.writeConfStatusAndLastUpdate(writer,data);
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}

	}

	@Override
	protected void convertInstructions(Set<InstructionDTO> instructions)
			throws DataException {
		try
		{
			RDFHelper.writeInstructions(writer,instructions);
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}

	}

	@Override
	public void prefix(MimsySiteDataDTO data) throws DataException {
		try
        {          
           RDFHelper.writePreamble(writer, "site", data.getMimsyIdentifier());
        }
        catch(Exception ex)
        {
            throw new DataException(ex);
        }      

	}

	@Override
	public void suffix(MimsySiteDataDTO Type) throws DataException {
		try
		{
			RDFHelper.writeEndAndFlush(writer, "site");
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}

	}

}
