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.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.model.presets.PresetType.*;
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 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 ASSIGNED_BY_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, ASSIGNED_BY_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(ASSIGNED_BY,
                    PresetItem.createWithName(rawItem.name)
                            .withRelevancy(rawItem.relevancy)
                            .build());
        });
    }
}

@@ -319,35 +319,33 @@
 presets.
-PresetsCommonConfig
+ff.RawNamedPreset
 ;%0Aimport
@@ -389,33 +389,41 @@
 der.
-presets.ff.RawNamedPreset
+support.DatabaseDescriptionConfig
 ;%0Aim
@@ -631,16 +631,69 @@
 actory;%0A
+import org.springframework.batch.item.ItemProcessor;%0A
 import o
@@ -1162,16 +1162,125 @@
 TReader;
+%0Aimport static uk.ac.ebi.quickgo.client.service.loader.support.DatabaseDescriptionConfig.DB_DESCRIPTIONS_MAP;
 %0A%0A/**%0A *
@@ -1466,19 +1466,25 @@
 rt(%7B
-PresetsComm
+DatabaseDescripti
 onCo
@@ -2212,16 +2212,61 @@
 T_KEY))%0A
+                .processor(addDescription())%0A
         
@@ -2932,16 +2932,16 @@
 ild());%0A
-
         
@@ -2949,11 +2949,413 @@
 );%0A    %7D
+%0A%0A    private ItemProcessor%3CRawNamedPreset, RawNamedPreset%3E addDescription() %7B%0A        return rawNamedPreset -%3E %7B%0A            if (DB_DESCRIPTIONS_MAP.containsKey(rawNamedPreset.id)) %7B%0A                rawNamedPreset.description = DB_DESCRIPTIONS_MAP.get(rawNamedPreset.id);%0A                return rawNamedPreset;%0A            %7D else %7B%0A                return rawNamedPreset;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

Extend the descriptions loader and use it to find the descripitions for assigned by values.