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

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.rest.search.request.converter.RESTFilterConverterFactory;

import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfig.SKIP_LIMIT;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.topItemsFromRESTReader;

/**
 * Exposes the {@link Step} bean that is used to read and populate information relating to the with/from preset data.
 *
 * Created 01/09/16
 * @author Edd
 */
@Configuration
@Import({PresetsCommonConfig.class})
public class WithFromPresetsConfig {
    public static final String WITH_FROM_DB_LOADING_STEP_NAME = "WithFromDBReadingStep";
    private static final String REST_KEY = "withFrom";

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

        return stepBuilderFactory.get(WITH_FROM_DB_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.WITH_FROM,
                        PresetItem.createWithName(rawItem.name)
                                .withRelevancy(rawItem.relevancy)
                                .build());
            });
        };
    }
}

@@ -122,66 +122,8 @@
 em;%0A
-import uk.ac.ebi.quickgo.client.model.presets.PresetType;%0A
 impo
@@ -190,16 +190,16 @@
 etImpl;%0A
+
 import u
@@ -841,16 +841,83 @@
 mport;%0A%0A
+import static uk.ac.ebi.quickgo.client.model.presets.PresetType.*;%0A
 import s
@@ -1454,14 +1454,13 @@
    p
-rivate
+ublic
  sta
@@ -1476,16 +1476,26 @@
  String 
+WITH_FROM_
 REST_KEY
@@ -2006,16 +2006,26 @@
 actory, 
+WITH_FROM_
 REST_KEY
@@ -2464,30 +2464,16 @@
 mList -%3E
- %7B%0A           
  rawItem
@@ -2510,20 +2510,16 @@
         
-    
 presets.
@@ -2528,27 +2528,16 @@
 dPreset(
-PresetType.
 WITH_FRO
@@ -2539,20 +2539,16 @@
 H_FROM,%0A
-    
         
@@ -2627,20 +2627,16 @@
         
-  
-  
 .withRel
@@ -2681,36 +2681,32 @@
                 
-    
 .build());%0A     
@@ -2712,25 +2712,10 @@
     
- 
-   %7D);%0A        
 %7D
+)
 ;%0A  

Rename the key used for the RESTful communication, reformat code.