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

import uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepoConfig;
import uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepository;
import uk.ac.ebi.quickgo.geneproduct.loader.DbXRefLoader;
import uk.ac.ebi.quickgo.geneproduct.model.GeneProductDbXRefIDFormats;
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 java.util.function.Predicate;
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.common.Definitions.APP_PROPERTIES_DELIMITER;
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 {

    private static final String DEFAULT_ID_VALIDATION_DB = "UniProtKB";
    private static final String DEFAULT_VALIDATION_TYPE_NAME = "protein";
    private static final String COMMA = ",";

    @Value("${geneproduct.db.xref.target.DBs}")
    public String targetDatabases;

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

    @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 Predicate<String> idValidator() {
        GeneProductDbXRefIDFormats
                dbXrefEntities = GeneProductDbXRefIDFormats.createWithData(geneProductLoader().load(),
                targetDatabases.split(APP_PROPERTIES_DELIMITER),
                DEFAULT_VALIDATION_TYPE_NAME);
        return dbXrefEntities::isValidId;
    }

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

}

@@ -1831,284 +1831,8 @@
  %7B%0A%0A
-    private static final String DEFAULT_ID_VALIDATION_DB = %22UniProtKB%22;%0A    private static final String DEFAULT_VALIDATION_TYPE_NAME = %22protein%22;%0A    private static final String COMMA = %22,%22;%0A%0A    @Value(%22$%7Bgeneproduct.db.xref.target.DBs%7D%22)%0A    public String targetDatabases;%0A%0A
     
@@ -2892,119 +2892,8 @@
 ad()
-,%0A                targetDatabases.split(APP_PROPERTIES_DELIMITER),%0A                DEFAULT_VALIDATION_TYPE_NAME
 );%0A 

Remove configuration values for target databases and entity type. No longer used.