package uk.ac.ebi.quickgo.index.annotation.coterms;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationDocument;
import uk.ac.ebi.quickgo.index.common.writer.ListItemWriter;

import java.util.List;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileHeaderCallback;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
 *
 * Spring Configuration class for Co-occurring terms.
 *
 * @author Tony Wardell
 * Date: 08/09/2016
 * Time: 11:07
 * Created with IntelliJ IDEA.
 */
@Configuration
public class CoTermsConfig {

    public static final String ELECTRONIC = "IEA";
    public static final Predicate<AnnotationDocument>
            EXCLUDE_ANNOTATIONS_PRODUCED_BY_ELECTRONIC_MEANS = annotationDocument -> !ELECTRONIC.equals(annotationDocument.goEvidence);
    public static final Predicate<AnnotationDocument> INCLUDE_ALL_ANNOTATIONS = annotationDocument -> true;
    private final Logger LOGGER = LoggerFactory.getLogger(CoTermsConfig.class);

    public static final String COTERM_MANUAL_COMPLETION_STEP_NAME = "coTermManualSummarizationStep";
    public static final String COTERM_ALL_COMPLETION_STEP_NAME = "cosTermAllSummarizationStep";
    private static final String[] FF_COL_NAMES = {"target", "comparedTerm", "probabilityRatio", "similarityRatio",
            "together", "compared"};
    private static final String DELIMITER = "\t";

    @Value("${indexing.coterms.manual}")
    private String manualCoTermsPath;

    @Value("${indexing.coterms.manual}")
    private String allCoTermsPath;

    @Bean
    public Resource manualCoTermsResource() {
        return new FileSystemResource(manualCoTermsPath);
    }

    @Bean
    public Resource allCoTermsResource() {
        return new FileSystemResource(allCoTermsPath);
    }

    @Bean
    public CoTermsAggregationWriter coTermsManualAggregationWriter() {
        return new CoTermsAggregationWriter(EXCLUDE_ANNOTATIONS_PRODUCED_BY_ELECTRONIC_MEANS);
    }

    @Bean
    public CoTermsAggregationWriter coTermsAllAggregationWriter() {
        return new CoTermsAggregationWriter(INCLUDE_ALL_ANNOTATIONS);
    }

    @Bean
    public StepExecutionListener coTermsEndOfAggregationListener(
            CoTermsAggregationWriter coTermsManualAggregationWriter,
            CoTermsAggregationWriter coTermsAllAggregationWriter) {
        return new CoTermsStepExecutionListener(coTermsManualAggregationWriter,
                coTermsAllAggregationWriter);

    }

    @Bean
    public StatisticsCalculator coTermsManualCalculator(CoTermsAggregationWriter coTermsManualAggregationWriter) {
        return new StatisticsCalculator(coTermsManualAggregationWriter);
    }

    @Bean
    public StatisticsCalculator coTermsAllCalculator(
            CoTermsAggregationWriter coTermsAllAggregationWriter) {
        return new StatisticsCalculator(coTermsAllAggregationWriter);
    }

    @Bean
    public ItemReader<String> coTermsManualReader(
            CoTermsAggregationWriter coTermsManualAggregationWriter) {
        return new CoTermItemReader(coTermsManualAggregationWriter);
    }

    @Bean
    public ItemReader<String> coTermsAllReader(
            CoTermsAggregationWriter coTermsAllAggregationWriter) {
        return new CoTermItemReader(coTermsAllAggregationWriter);
    }

    @Bean
    ItemWriter<List<CoTerm>> coTermsManualStatsWriter() {
        return listItemFlatFileWriter(manualCoTermsResource());
    }

    @Bean
    ItemWriter<List<CoTerm>> coTermsAllStatsWriter() {
        return listItemFlatFileWriter(allCoTermsResource());
    }

    private ListItemWriter<CoTerm> listItemFlatFileWriter(Resource outputFile) {
        ListItemWriter<CoTerm> listWriter = new ListItemWriter<>(flatFileWriter(outputFile));
        listWriter.setLineAggregator(new PassThroughLineAggregator<>());
        return listWriter;
    }

    private FlatFileItemWriter<CoTerm> flatFileWriter(Resource outputFile) {
        FlatFileItemWriter<CoTerm> ffw = new FlatFileItemWriter<>();
        ffw.setLineAggregator(lineAggregator());
        LOGGER.info("Write out co-occurring terms to {}", outputFile.toString());
        ffw.setResource(outputFile);
        FlatFileHeaderCallback headerCallBack = new CoTermsFlatFileHeaderCallBack();
        ffw.setHeaderCallback(headerCallBack);
        return ffw;
    }

    private LineAggregator<CoTerm> lineAggregator() {
        DelimitedLineAggregator<CoTerm> delimitedLineAggregator = new DelimitedLineAggregator<>();
        delimitedLineAggregator.setDelimiter(DELIMITER);
        delimitedLineAggregator.setFieldExtractor(beanWrapperFieldExtractor());
        return delimitedLineAggregator;
    }

    private BeanWrapperFieldExtractor<CoTerm> beanWrapperFieldExtractor() {
        BeanWrapperFieldExtractor<CoTerm> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();
        beanWrapperFieldExtractor.setNames(FF_COL_NAMES);
        return beanWrapperFieldExtractor;
    }
}

@@ -1372,29 +1372,30 @@
 fig %7B%0A%0A    p
-ublic
+rivate
  static fina
@@ -1420,37 +1420,38 @@
 C = %22IEA%22;%0A    p
-ublic
+rivate
  static final Pr
@@ -1615,29 +1615,30 @@
 ence);%0A    p
-ublic
+rivate
  static fina
@@ -1833,24 +1833,25 @@
 al String CO
+_
 TERM_MANUAL_
@@ -1850,22 +1850,25 @@
 _MANUAL_
-COMPLE
+SUMMARIZA
 TION_STE
@@ -1864,29 +1864,24 @@
 IZATION_STEP
-_NAME
  = %22coTermMa
@@ -1937,16 +1937,17 @@
 tring CO
+_
 TERM_ALL
@@ -1951,14 +1951,17 @@
 ALL_
-COMPLE
+SUMMARIZA
 TION
@@ -1969,20 +1969,14 @@
 STEP
-_NAME
  = %22co
-s
 Term
@@ -3300,34 +3300,30 @@
  public 
-StatisticsCalculat
+CoTermsProcess
 or coTer
@@ -3419,34 +3419,30 @@
 urn new 
-StatisticsCalculat
+CoTermsProcess
 or(coTer
@@ -3497,34 +3497,30 @@
  public 
-StatisticsCalculat
+CoTermsProcess
 or coTer
@@ -3627,26 +3627,22 @@
 new 
-StatisticsCalculat
+CoTermsProcess
 or(c

Fix spelling, access and member names.