package uk.ac.ebi.quickgo.geneproduct.service;

import uk.ac.ebi.quickgo.common.loader.DbXRefLoader;
import uk.ac.ebi.quickgo.common.validator.DbXRefEntityValidation;
import uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepoConfig;
import uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepository;
import uk.ac.ebi.quickgo.geneproduct.service.converter.GeneProductDocConverter;
import uk.ac.ebi.quickgo.geneproduct.service.converter.GeneProductDocConverterImpl;
import uk.ac.ebi.quickgo.rest.controller.ControllerValidationHelper;
import uk.ac.ebi.quickgo.rest.controller.ControllerValidationHelperImpl;
import uk.ac.ebi.quickgo.rest.search.QueryStringSanitizer;
import uk.ac.ebi.quickgo.rest.search.SolrQueryStringSanitizer;
import uk.ac.ebi.quickgo.rest.service.ServiceHelper;
import uk.ac.ebi.quickgo.rest.service.ServiceHelperImpl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import static uk.ac.ebi.quickgo.rest.controller.ControllerValidationHelperImpl.MAX_PAGE_RESULTS;

/**
 *
 * Spring configuration for the service layer, which depends on the repositories
 * made available by {@link GeneProductRepoConfig}. Services
 * to additionally make accessible are defined in specified the {@link ComponentScan} packages.
 *
 * @author Tony Wardell
 * Date: 04/04/2016
 * Time: 11:42
 * Created with IntelliJ IDEA.
 */
@Configuration
@ComponentScan({"uk.ac.ebi.quickgo.geneproduct.service"})
@Import({GeneProductRepoConfig.class})
public class ServiceConfig {

    @Value("${geneproduct.db.xref.valid.regexes}")
    private String xrefValidationRegexFile;

    private static final boolean DEFAULT_XREF_VALIDATION_IS_CASE_SENSITIVE = true;

    @Value("${geneproduct.db.xref.valid.casesensitive:"+DEFAULT_XREF_VALIDATION_IS_CASE_SENSITIVE+"}")
    boolean xrefValidationCaseSensitive;

    @Bean
    public GeneProductService goGeneProductService(GeneProductRepository geneProductRepository) {
        return new GeneProductServiceImpl(
                serviceHelper(),
                geneProductRepository,
                geneProductDocConverter());
    }

    private ServiceHelper serviceHelper() {
        return new ServiceHelperImpl(queryStringSanitizer());
    }

    private GeneProductDocConverter geneProductDocConverter() {
        return new GeneProductDocConverterImpl();
    }

    private QueryStringSanitizer queryStringSanitizer() {
        return new SolrQueryStringSanitizer();
    }

    @Bean
    public ControllerValidationHelper geneProductValidator() {
        return new ControllerValidationHelperImpl(MAX_PAGE_RESULTS, idValidator());
    }

    private DbXRefEntityValidation idValidator() {
        return DbXRefEntityValidation.createWithData(geneProductLoader().load());
    }

    private DbXRefLoader geneProductLoader() {
        return new DbXRefLoader(this.xrefValidationRegexFile, xrefValidationCaseSensitive);
    }

}

@@ -1707,104 +1707,8 @@
  %7B%0A%0A
-    @Value(%22$%7Bgeneproduct.db.xref.valid.regexes%7D%22)%0A    private String xrefValidationRegexFile;%0A%0A
     
@@ -1786,17 +1786,16 @@
 = true;%0A
-%0A
     @Val
@@ -1929,16 +1929,111 @@
 nsitive;
+%0A    @Value(%22$%7Bgeneproduct.db.xref.valid.regexes%7D%22)%0A    private String xrefValidationRegexFile;
 %0A%0A    @B
@@ -2292,32 +2292,196 @@
 rter());%0A    %7D%0A%0A
+    @Bean%0A    public ControllerValidationHelper geneProductValidator() %7B%0A        return new ControllerValidationHelperImpl(MAX_PAGE_RESULTS, idValidator());%0A    %7D%0A%0A
     private Serv
@@ -2752,24 +2752,24 @@
 nitizer() %7B%0A
+
         retu
@@ -2814,172 +2814,8 @@
  %7D%0A%0A
-    @Bean%0A    public ControllerValidationHelper geneProductValidator() %7B%0A        return new ControllerValidationHelperImpl(MAX_PAGE_RESULTS, idValidator());%0A    %7D%0A%0A
     

Order methods public then private.