package com.k_int.aggr2.mimsy.solr;

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.apache.solr.common.SolrInputDocument;

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 SolrSiteConverter extends AbstractSiteFormatConverter {
	private SolrInputDocument doc;
	private Properties solr_props;
	
	public SolrSiteConverter(SolrInputDocument doc, Properties solr_props)
	{
		this.doc=doc;
		this.solr_props=solr_props;
	}
	@Override
	protected void convertExcavations(Set<ExcavationDTO> excavations)
			throws DataException {
		 if(excavations.size()==0)
			 return;
		 try
         {
         	Iterator<ExcavationDTO> i = excavations.iterator();
         	while(i.hasNext())
         	{
         		ExcavationDTO ref = i.next();
         		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);  	       
         	}       	
         }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }


	}

	@Override
	protected void convert(String elementVal, String val) throws DataException {
		if(elementVal.equals("mo:name"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("mo:variation"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("dc:description"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else
			SolrHelper.addField(solr_props,doc,elementVal, null, val,null);

	}

	@Override
	protected void convertConfAndLastUpdate(MimsyDataDTO data)
			throws DataException {
		SolrHelper.addConfAndLastUpdate(solr_props,doc,data);

	}

	@Override
	protected void convertInstructions(Set<InstructionDTO> instructions)
			throws DataException {
		// not used

	}

	@Override
	public void prefix(MimsySiteDataDTO Type) throws DataException {
		// not used

	}

	@Override
	public void suffix(MimsySiteDataDTO Type) throws DataException {
		// not used

	}

}
