package com.k_int.aggr2.mimsy.rdf;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;

import com.k_int.aggr2.mimsy.data.CIIMContextDataDTO;
import com.k_int.aggr2.mimsy.data.CIIMMasterDataDTO;
import com.k_int.aggr2.mimsy.data.DataContextDTO;
import com.k_int.aggr2.mimsy.data.ExtensionFieldDTO;
import com.k_int.aggr2.mimsy.data.InstructionDTO;
import com.k_int.aggr2.mimsy.data.MimsyDataDTO;
import com.k_int.aggr2.mimsy.data.conversion.AbstractDataContextFormatConverter;
import com.k_int.aggr2.mimsy.data.util.MimsyHelper;
import com.k_int.mimsy.ref.DataException;


public class RDFDataContextConverter extends AbstractDataContextFormatConverter {

	private Writer writer;
	
	public RDFDataContextConverter(Writer writer)
	{
		this.writer=writer;
	}
	@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(DataContextDTO data) throws DataException {
		try
        {          
           RDFHelper.writePreamble(writer, "dataContext", data.getMimsyIdentifier());
        }
        catch(Exception ex)
        {
            throw new DataException(ex);
        }      
		
	}

	@Override
	public void suffix(DataContextDTO data) throws DataException {
		try
		{
			RDFHelper.writeEndAndFlush(writer, "dataContext");
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}
		
	}
	@Override
	protected void convertCIIMData(CIIMContextDataDTO data) throws DataException {
		if(data==null)
			return;
		 convert("mo:boost",Integer.toString(data.getBoostValue()));
		 if(data.getFeatured()!=null)
			 convert("mo:featured",Boolean.toString(data.getFeatured()));
		 convertExtensionFields(data.getExtensionFields());	
	}
	
	@Override
	protected void convertMasterData(CIIMMasterDataDTO data) throws DataException {
		if(data==null)// || data.getMaster()==null)
			return;
		 
		try
		{
			writer.write("<mo:dataReference ");
			writer.write("rdf:resource=\"urn://mol/");
			writer.write(MimsyHelper.encodeForXml(data.getMaster().getMimsyIdentifier()));
			writer.write("\"/>");
		}
		catch(IOException ioe)
		{
			throw new DataException(ioe);
		}		
	}
	
	protected void convertExtensionFields(Set<ExtensionFieldDTO> extension_fields)
	throws DataException 
	{
		if(extension_fields == null || extension_fields.size()==0)
			return;
        
		try
		{
			writer.write("<mo:extensions>");
			
			for(ExtensionFieldDTO field : extension_fields)
			{
				if(field.getValues() != null && field.getValues().size() > 0)
				{
					writer.write("<mo:extension>");
					for(String value : field.getValues())
					{
						convert("mo:"+field.getIndexName(), value);
					}
					writer.write("</mo:extension>");
				}
			}
			
			writer.write("</mo:extensions>");
		}
		catch(Exception e)
		{
			throw new DataException(e);
		}
	}

}
