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

        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 uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepository;
        import uk.ac.ebi.quickgo.geneproduct.common.GeneProductRepoConfig;
        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;

/**
 *
 * 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_DB = "UniProtKB";
    private static final String DEFAULT_TYPE_NAME = "protein";

    @Value("${public.geneproduct.source}")
    private String sourceFileDirectory;

    @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(ControllerValidationHelperImpl.MAX_PAGE_RESULTS, idValidator());
    }

    private Predicate<String> idValidator() {
        GeneProductDbXrefIDFormats
                dbXrefEntities = GeneProductDbXrefIDFormats.createWithData(geneProductLoader().load(), DEFAULT_DB,
                DEFAULT_TYPE_NAME);
        return id -> dbXrefEntities.isValidId(id);	}

    private DbXrefLoader geneProductLoader() {
        return new DbXrefLoader(this.sourceFileDirectory);
    }


}

@@ -3065,14 +3065,8 @@
 turn
- id -%3E
  dbX
@@ -3076,17 +3076,18 @@
 Entities
-.
+::
 isValidI
@@ -3091,12 +3091,8 @@
 idId
-(id)
 ;%09%7D%0A

Return method reference from idValidator();