package uk.ac.ebi.quickgo.annotation.search;

import uk.ac.ebi.quickgo.annotation.common.AnnotationRepoConfig;
import uk.ac.ebi.quickgo.annotation.model.Annotation;
import uk.ac.ebi.quickgo.annotation.service.converter.AnnotationDocConverterImpl;
import uk.ac.ebi.quickgo.rest.controller.ControllerValidationHelper;
import uk.ac.ebi.quickgo.rest.controller.ControllerValidationHelperImpl;
import uk.ac.ebi.quickgo.rest.search.RequestRetrieval;
import uk.ac.ebi.quickgo.rest.search.SearchService;
import uk.ac.ebi.quickgo.rest.search.query.QueryRequestConverter;
import uk.ac.ebi.quickgo.rest.search.query.SolrQueryConverter;
import uk.ac.ebi.quickgo.rest.search.solr.SolrRequestRetrieval;
import uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfig;
import uk.ac.ebi.quickgo.rest.service.ServiceRetrievalConfig;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.solr.core.SolrTemplate;

/**
 * @author Tony Wardell
 * Date: 26/04/2016
 * Time: 15:07
 * Created with IntelliJ IDEA.
 */
@Configuration
@Import({AnnotationRepoConfig.class})
@ComponentScan({"uk.ac.ebi.quickgo.annotation.service.search"})
@PropertySource("classpath:search.properties")
public class SearchServiceConfig {

    public static final int MAX_PAGE_RESULTS = 100;
    private static final String COMMA = ",";
    private static final String DEFAULT_ANNOTATION_SEARCH_RETURN_FIELDS = "id,geneProductId,qualifier,goId," +
            "goEvidence,ecoId,reference,withFrom,taxonId,assignedBy,extension";
    //public static final String SOLR_ANNOTATION_QUERY_REQUEST_HANDLER = "/query";
    public static final String SOLR_ANNOTATION_QUERY_REQUEST_HANDLER = "/filter";

    @Bean
    public SearchService<Annotation> annotationSearchService(
            RequestRetrieval<Annotation> annotationSolrRequestRetrieval) {
        return new SearchServiceImpl(annotationSolrRequestRetrieval);
    }

    @Bean
    public RequestRetrieval<Annotation> annotationSolrRequestRetrieval(
            SolrTemplate annotationTemplate,
            QueryRequestConverter<SolrQuery> queryRequestConverter,
            AnnotationCompositeRetrievalConfig annotationRetrievalConfig) {

        AnnotationSolrQueryResultConverter resultConverter = new AnnotationSolrQueryResultConverter(
                new DocumentObjectBinder(),
                new AnnotationDocConverterImpl());

        return new SolrRequestRetrieval<>(
                annotationTemplate.getSolrServer(),
                queryRequestConverter,
                resultConverter,
                annotationRetrievalConfig);
    }

    @Bean
    public QueryRequestConverter<SolrQuery> annotationSolrQueryRequestConverter() {
        return new AnnotationSolrQueryConverter(SOLR_ANNOTATION_QUERY_REQUEST_HANDLER);
    }

    @Bean
    public AnnotationCompositeRetrievalConfig annotationRetrievalConfig(
            @Value("${search.return.fields:" + DEFAULT_ANNOTATION_SEARCH_RETURN_FIELDS + "}") String
                    annotationSearchSolrReturnedFields) {

        return new AnnotationCompositeRetrievalConfig() {

            //Not called
            @Override public Map<String, String> repo2DomainFieldMap() {
                return null;
            }

            @Override public List<String> getSearchReturnedFields() {
                return Arrays.asList(annotationSearchSolrReturnedFields.split(COMMA));
            }

            //Not called
            @Override public String getHighlightStartDelim() {
                return "";
            }

            //Not called
            @Override public String getHighlightEndDelim() {
                return "";
            }
        };
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public ControllerValidationHelper validationHelper() {
        return new ControllerValidationHelperImpl(MAX_PAGE_RESULTS);
    }

    public interface AnnotationCompositeRetrievalConfig extends SolrRetrievalConfig, ServiceRetrievalConfig {}
}

@@ -559,71 +559,8 @@
 er;%0A
-import uk.ac.ebi.quickgo.rest.search.query.SolrQueryConverter;%0A
 impo
@@ -611,32 +611,32 @@
 questRetrieval;%0A
+
 import uk.ac.ebi
@@ -1176,16 +1176,92 @@
 e;%0A%0A/**%0A
+ *%0A * Spring Configuration facilitating Annotation search functionality.%0A *%0A
  * @auth
@@ -1838,91 +1838,8 @@
 n%22;%0A
-    //public static final String SOLR_ANNOTATION_QUERY_REQUEST_HANDLER = %22/query%22;%0A
     
@@ -2421,26 +2421,16 @@
         
-Annotation
 SolrQuer
@@ -2464,34 +2464,24 @@
 erter = new 
-Annotation
 SolrQueryRes
@@ -2989,32 +2989,193 @@
 ANDLER);%0A    %7D%0A%0A
+    /**%0A     * Annotation retrieval config. Annotations searches don't use highlighting.%0A     * @param annotationSearchSolrReturnedFields%0A     * @return%0A     */%0A
     @Bean%0A    pu

Add javadoc.