package uk.ac.ebi.quickgo.client.service.loader.presets.assignedby;

import uk.ac.ebi.quickgo.client.model.presets.PresetItem;
import uk.ac.ebi.quickgo.client.model.presets.PresetType;
import uk.ac.ebi.quickgo.client.model.presets.impl.CompositePresetImpl;
import uk.ac.ebi.quickgo.client.service.loader.presets.LogStepListener;
import uk.ac.ebi.quickgo.client.service.loader.presets.PresetsCommonConfig;
import uk.ac.ebi.quickgo.client.service.loader.presets.ff.RawNamedPreset;
import uk.ac.ebi.quickgo.client.service.loader.presets.ff.RawNamedPresetValidator;
import uk.ac.ebi.quickgo.client.service.loader.presets.ff.SourceColumnsFactory;
import uk.ac.ebi.quickgo.client.service.loader.presets.ff.StringToRawNamedPresetMapper;
import uk.ac.ebi.quickgo.rest.search.RetrievalException;
import uk.ac.ebi.quickgo.rest.search.request.FilterRequest;
import uk.ac.ebi.quickgo.rest.search.request.config.FilterConfigRetrieval;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;
import uk.ac.ebi.quickgo.rest.search.request.converter.RESTFilterConverterFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.web.client.RestOperations;

import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfig.SKIP_LIMIT;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.compositeItemProcessor;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.fileReader;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.rawPresetMultiFileReader;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.topItemsFromRESTReader;
import static uk.ac.ebi.quickgo.client.service.loader.presets.ff.SourceColumnsFactory.Source.DB_COLUMNS;

/**
 * Exposes the {@link Step} bean that is used to read and populate information relating to the assignedBy preset data.
 *
 * Created 01/09/16
 * @author Edd
 */
@Configuration
@Import({PresetsCommonConfig.class})
public class AssignedByPresetsConfig {
    public static final String ASSIGNED_BY_LOADING_STEP_NAME = "AssignedByReadingStep";
    public static final String REST_KEY = "assignedBy";

    @Bean
    public Step assignedByStep(
            StepBuilderFactory stepBuilderFactory,
            Integer chunkSize,
            CompositePresetImpl presets,
            RESTFilterConverterFactory converterFactory) {

        return stepBuilderFactory.get(ASSIGNED_BY_LOADING_STEP_NAME)
                .<RawNamedPreset, RawNamedPreset>chunk(chunkSize)
                .faultTolerant()
                .skipLimit(SKIP_LIMIT)
                .reader(topItemsFromRESTReader(converterFactory, REST_KEY))
                .writer(rawPresetWriter(presets))
                .listener(new LogStepListener())
                .build();
    }

    /**
     * Write the list of {@link RawNamedPreset}s to the {@link CompositePresetImpl}
     * @param presets the presets to write to
     * @return the corresponding {@link ItemWriter}
     */
    ItemWriter<RawNamedPreset> rawPresetWriter(CompositePresetImpl presets) {
        return rawItemList -> {
            rawItemList.forEach(rawItem -> {
                presets.addPreset(PresetType.ASSIGNED_BY,
                        PresetItem.createWithName(rawItem.name)
                                .withRelevancy(rawItem.relevancy)
                                .build());
            });
        };
    }
}

@@ -124,66 +124,8 @@
 em;%0A
-import uk.ac.ebi.quickgo.client.model.presets.PresetType;%0A
 impo
@@ -443,1699 +443,477 @@
 kgo.
-client.service.loader.presets.ff.RawNamedPresetValidator;%0Aimport uk.ac.ebi.quickgo.client.service.loader.presets.ff.SourceColumnsFactory;%0Aimport uk.ac.ebi.quickgo.client.service.loader.presets.ff.StringToRawNamedPresetMapper;%0Aimport uk.ac.ebi.quickgo.rest.search.RetrievalException;%0Aimport uk.ac.ebi.quickgo.rest.search.request.FilterRequest;%0Aimport uk.ac.ebi.quickgo.rest.search.request.config.FilterConfigRetrieval;%0Aimport uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;%0Aimport uk.ac.ebi.quickgo.rest.search.request.converter.RESTFilterConverterFactory;%0A%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport org.slf4j.Logger;%0Aimport org.springframework.batch.core.Step;%0Aimport org.springframework.batch.core.configuration.annotation.StepBuilderFactory;%0Aimport org.springframework.batch.item.ItemProcessor;%0Aimport org.springframework.batch.item.ItemWriter;%0Aimport org.springframework.batch.item.file.FlatFileItemReader;%0Aimport org.springframework.batch.item.file.mapping.FieldSetMapper;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0Aimport org.springframework.context.annotation.Import;%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.web.client.RestOperations;%0A%0Aimport static org.slf4j.LoggerFactory.getLogger;%0Aimport static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfig.SKIP_LIMIT;%0Aimport static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.compositeItemProcessor;%0Aimport static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.fileReader
+rest.search.request.converter.RESTFilterConverterFactory;%0A%0Aimport org.springframework.batch.core.Step;%0Aimport org.springframework.batch.core.configuration.annotation.StepBuilderFactory;%0Aimport org.springframework.batch.item.ItemWriter;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0Aimport org.springframework.context.annotation.Import;%0A%0Aimport static uk.ac.ebi.quickgo.client.model.presets.PresetType.*
 ;%0Aim
@@ -989,39 +989,19 @@
 nfig
-Helper.rawPresetMultiFileReader
+.SKIP_LIMIT
 ;%0Aim
@@ -1107,113 +1107,8 @@
 der;
-%0Aimport static uk.ac.ebi.quickgo.client.service.loader.presets.ff.SourceColumnsFactory.Source.DB_COLUMNS;
 %0A%0A/*
@@ -1480,16 +1480,28 @@
  String 
+ASSIGNED_BY_
 REST_KEY
@@ -2015,16 +2015,28 @@
 actory, 
+ASSIGNED_BY_
 REST_KEY
@@ -2475,30 +2475,16 @@
 mList -%3E
- %7B%0A           
  rawItem
@@ -2521,20 +2521,16 @@
         
-    
 presets.
@@ -2539,27 +2539,16 @@
 dPreset(
-PresetType.
 ASSIGNED
@@ -2552,20 +2552,16 @@
 NED_BY,%0A
-    
         
@@ -2640,20 +2640,16 @@
         
-  
-  
 .withRel
@@ -2694,36 +2694,32 @@
                 
-    
 .build());%0A     
@@ -2725,25 +2725,10 @@
     
- 
-   %7D);%0A        
 %7D
+)
 ;%0A  

Rename the key used for the RESTful communication, clean up the code a little.