package uk.ac.ebi.quickgo.solr.indexing.service.ontology;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.stereotype.Service;

import uk.ac.ebi.quickgo.ontology.generic.GenericTerm;
import uk.ac.ebi.quickgo.solr.indexing.Indexer;
import uk.ac.ebi.quickgo.solr.mapper.SolrMapper;
import uk.ac.ebi.quickgo.solr.model.ontology.SolrTerm;
import uk.ac.ebi.quickgo.solr.model.ontology.SolrTermComparator;
import uk.ac.ebi.quickgo.solr.server.SolrServerProcessor;

/**
 * Index Term objects in Solr
 * @author cbonill
 *
 */
@Service("termIndexer")
public class TermIndexer implements Indexer<GenericTerm> {

	private SolrServerProcessor solrServerProcessor;
	
	private SolrMapper<GenericTerm, SolrTerm> solrMapper;
	
	// Log
	private static final Logger logger = LoggerFactory.getLogger(TermIndexer.class);

	/**
	 * See {@link Indexer#index(List)}
	 */
	public void index(List<GenericTerm> list) {
		Collection<SolrTerm> termBeans = mapBeans(list);
		try {			
			solrServerProcessor.indexBeans(termBeans);
		} catch (SolrServerException | IOException e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * Deletes everything from the schema
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public void deleteAll() throws SolrServerException, IOException{
		solrServerProcessor.deleteAll();
	}
	

	public void setSolrMapper(SolrMapper<GenericTerm, SolrTerm> solrMapper) {
		this.solrMapper = solrMapper;
	}

	/**
     * Indexes GO Terms in SolR
     * @throws SolrServerException 
     */
	private Collection<SolrTerm> mapBeans(List<GenericTerm> terms) {

		// TreeSet to avoid duplicated Relations
		Set<SolrTerm> beans = new TreeSet<SolrTerm>(new SolrTermComparator());
		//  Iterate over all the GO terms and convert them into Solr objects to be indexed
		for (GenericTerm term : terms) {			
			beans.addAll(solrMapper.toSolrObject(term));
    	}
		return beans;
    }

	public void setSolrServerProcessor(SolrServerProcessor solRServerProcessor) {
		this.solrServerProcessor = solRServerProcessor;
	}
}

@@ -104,92 +104,9 @@
 til.
-Collection;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Set;%0D%0Aimport java.util.TreeSet
+*
 ;%0D%0A%0D
@@ -814,19 +814,18 @@
 essor;%0D%0A
-%09
 %0D%0A
+
 %09private
@@ -872,19 +872,18 @@
 apper;%0D%0A
-%09
 %0D%0A
+
 %09// Log%0D
@@ -965,16 +965,49 @@
 class);%0D
+%0A%09private Properties properties;%0D
 %0A%0D%0A%09/**%0D
@@ -1155,19 +1155,16 @@
 %0A%09%09try %7B
-%09%09%09
 %0D%0A%09%09%09sol
@@ -1298,19 +1298,18 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %09/**%0D%0A%09 
@@ -1517,19 +1517,18 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %0D%0A%09publi
@@ -1709,17 +1709,16 @@
 xception
- 
 %0D%0A     *
@@ -2027,19 +2027,16 @@
 terms) %7B
-%09%09%09
 %0D%0A%09%09%09bea
@@ -2188,24 +2188,24 @@
 ocessor) %7B%0D%0A
-
 %09%09this.solrS
@@ -2246,12 +2246,154 @@
 or;%0D%0A%09%7D%0D
+%0A%0D%0A%09public void setProperties(Properties properties) %7B%0D%0A%09%09this.properties = properties;%0D%0A%09%09solrServerProcessor.setProperties(properties);%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Use properties file defined on command line for configuration.