package com.k_int.aggr2.mimsy.solr;

import java.util.Properties;

import org.apache.solr.common.SolrInputDocument;

import com.k_int.aggr2.mimsy.data.AttributionDTO;
import com.k_int.aggr2.mimsy.data.MimsyDataDTO;
import com.k_int.mimsy.ref.DataException;
import com.k_int.mimsy.ref.MimsyRefData;

public class SolrHelper {

/**
 * 
 * @param solrProps
 * @param doc
 * @param elementVal
 * @param object
 * @param val
 * @param text  A Boolean, null if not added to a composite field,true if added to text, false if added to ref
 * @throws DataException
 */
	public static void addField(Properties solrProps,
			SolrInputDocument doc, String elementVal, Object object, String val, Boolean text)throws DataException 
		{
	
		if(val==null || val.trim().length()==0)
			return;
	
		String solr_prop = solrProps.getProperty(elementVal);
	
		if(solr_prop!=null)
		{
			//val = MimsyHelper.encodeForXml(val);
			doc.addField(solr_prop, val);
			if(text==null)
			{;}
			else if(text)
				doc.addField("text", val);
			else
				doc.addField("reference", val);
		}
	}

	public static void addConfAndLastUpdate(Properties solrProps,
			SolrInputDocument doc, MimsyDataDTO data) throws DataException{
		
		 addField(solrProps,doc,"mo:conformance",null,data.getConformance().toString(),null);
    	 addField(solrProps,doc,"dcterms:modified",null,MimsyRefData.SOLR_DATE_FORMAT.format(data.getLastUpdated()),null);		
    	 addField(solrProps,doc,"mo:formattedModified", null,MimsyRefData.UI_DATE_FORMAT.format(data.getLastUpdated()),null);
	}

	public static void writeAttribution(Properties solrProps,
			SolrInputDocument doc, String element_name_prefix, AttributionDTO attrib) throws DataException{
		if(attrib==null)
			return;
			
		if(attrib.getAttributor()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"Attributor",null,attrib.getAttributor(),null);
		}
				
		if(attrib.getCertainty()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"AttributionCertainty",null,attrib.getCertainty(),null);			
		}
				
		if(attrib.getComment()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"AttributionComment",null,attrib.getComment(),null);						
		}
				
		if(attrib.getDate()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"AttributionDate",null,attrib.getDate(),null);						
		}
				
		if(attrib.getType()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"AttributionType",null,attrib.getType(),null);									
		}
				
		if(attrib.getSource()!=null)
		{
			addField(solrProps,doc,"mo:"+element_name_prefix+"AttributionSource",null,attrib.getSource(),null);									
		}
	}

}
