package uk.ac.ebi.quickgo.index.write.job;

import uk.ac.ebi.quickgo.index.reader.DocumentReaderException;
import uk.ac.ebi.quickgo.index.reader.ODocReader;
import uk.ac.ebi.quickgo.index.write.IndexerProperties;
import uk.ac.ebi.quickgo.index.write.listener.LogJobListener;
import uk.ac.ebi.quickgo.index.write.listener.LogStepListener;
import uk.ac.ebi.quickgo.index.write.writer.SolrCrudRepoWriter;
import uk.ac.ebi.quickgo.ontology.common.OntologyRepository;
import uk.ac.ebi.quickgo.ontology.common.config.RepoConfig;
import uk.ac.ebi.quickgo.ontology.common.document.OntologyDocument;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.File;

/**
 *
 *
 * Created 02/12/15
 * @author Edd
 */
@Configuration
@EnableBatchProcessing
@Import({IndexerProperties.class, RepoConfig.class})
public class IndexingJobConfig {
    // logger
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexingJobConfig.class);

    @Autowired
    private JobBuilderFactory jobBuilders;

    @Autowired
    private StepBuilderFactory stepBuilders;

    @Autowired
    private OntologyRepository ontologyRepository;

    @Autowired
    private IndexerProperties indexerProperties;

    @Bean
    public Job job() {
        return jobBuilders.get("indexingJob")
                .start(step())
                .listener(logJobListener())
                .build();
    }

    @Bean
    public Step step() {
        return stepBuilders
                .get("readThenWriteToRepoStep")
                // read and process items in chunks of the following size
                .<OntologyDocument, OntologyDocument>chunk(indexerProperties.getOntologyChunkSize())
                .reader(reader())
                .faultTolerant()
                .skip(DocumentReaderException.class)
                .skipLimit(indexerProperties.getOntologySkipLimit())
                .writer(writer())
                .listener(logStepListener())
                .build();
    }

    @Bean
    public ItemWriter<OntologyDocument> writer() {
        return new SolrCrudRepoWriter<>(ontologyRepository);
    }

    @Bean
    public LogJobListener logJobListener() {
        return new LogJobListener();
    }

    @Bean
    public LogStepListener logStepListener() {
        return new LogStepListener();
    }

    @Bean
    public ODocReader reader() {
        return new ODocReader(new File(indexerProperties.getOntologySourceFile()));
    }

}

@@ -1093,16 +1093,75 @@
 owired;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import o
@@ -1204,16 +1204,16 @@
 n.Bean;%0A
-
 import o
@@ -1350,14 +1350,8 @@
 /**%0A
- *%0A *%0A
  * C
@@ -1436,33 +1436,8 @@
 rt(%7B
-IndexerProperties.class, 
 Repo
@@ -1488,22 +1488,8 @@
 g %7B%0A
-    // logger%0A
     
@@ -1772,65 +1772,230 @@
    @
-Autowired%0A    private IndexerProperties indexerProperties
+Value(%22$%7Bindexing.ontology.chunk.size:500%7D%22)%0A    private int chunkSize;%0A%0A    @Value(%22$%7Bindexing.ontology.skip.limit:100%7D%22)%0A    private int skipLimit;%0A%0A    @Value(%22$%7Bindexing.ontology.source%7D%22)%0A    private String sourceFile
 ;%0A%0A 
@@ -2428,46 +2428,17 @@
 unk(
-indexerProperties.getOntologyC
+c
 hunkSize
 ())%0A
@@ -2433,18 +2433,16 @@
 hunkSize
-()
 )%0A      
@@ -2586,46 +2586,17 @@
 mit(
-indexerProperties.getOntologyS
+s
 kipLimit
 ())%0A
@@ -2591,18 +2591,16 @@
 kipLimit
-()
 )%0A      
@@ -3121,38 +3121,9 @@
 ile(
-indexerProperties.getOntologyS
+s
 ourc
@@ -3131,19 +3131,17 @@
 File
-(
 ))
-)
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -3137,11 +3137,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Simplify the java config for the indexer.