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

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.SourceColumnsFactory;
import uk.ac.ebi.quickgo.client.service.loader.presets.ff.StringToRawNamedPresetMapper;

import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
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 static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfig.SKIP_LIMIT;
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.ff.ItemProcessorFactory.validatingItemProcessor;
import static uk.ac.ebi.quickgo.client.service.loader.presets.ff.SourceColumnsFactory.Source.EXT_RELATION_COLUMNS;

/**
 * Exposes the {@link Step} bean that is used to read and populate information relating to the annotation extension relations
 * preset data.
 * <p>
 * Created 06/12/16
 *
 * @author TonyW
 */
@Configuration
@Import({PresetsCommonConfig.class})
public class ExtensionRelationsPresetsConfig {
    public static final String EXTENSION_RELATIONS_LOADING_STEP_NAME = "ExtensionRelationsReadingStep";
    private static final String ROOT_RELATION = "_ROOT_AER_";

    @Value("#{'${extrelations.preset.source:}'.split(',')}")
    private Resource[] resources;
    @Value("${extrelations.preset.header.lines:1}")
    private int headerLines;

    @Bean
    public Step extensionRelationsProductStep(
            StepBuilderFactory stepBuilderFactory,
            Integer chunkSize,
            CompositePresetImpl presets) {
        FlatFileItemReader<RawNamedPreset> itemReader = fileReader(rawPresetFieldSetMapper());
        itemReader.setLinesToSkip(headerLines);

        return stepBuilderFactory.get(EXTENSION_RELATIONS_LOADING_STEP_NAME)
                .<RawNamedPreset, RawNamedPreset>chunk(chunkSize)
                .faultTolerant()
                .skipLimit(SKIP_LIMIT)
                .<RawNamedPreset>reader(rawPresetMultiFileReader(resources, itemReader)).processor(
                        validatingItemProcessor())
                .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}
     */
    private ItemWriter<RawNamedPreset> rawPresetWriter(CompositePresetImpl presets) {
        return rawItemList -> rawItemList.forEach(rawItem -> {
            if(!rawItem.id.equals(ROOT_RELATION)) {
                presets.addPreset(PresetType.EXT_RELATIONS,
                                  PresetItem.createWithName(rawItem.name)
                                            .withProperty(PresetItem.Property.ID.getKey(), rawItem.id)
                                            .build());
            }
        });
    }

    private FieldSetMapper<RawNamedPreset> rawPresetFieldSetMapper() {
        return StringToRawNamedPresetMapper.create(SourceColumnsFactory.createFor(EXT_RELATION_COLUMNS));
    }
}

@@ -2919,33 +2919,16 @@
 P_LIMIT)
-%0A                
 .%3CRawNam
@@ -2939,24 +2939,49 @@
 eset%3Ereader(
+%0A                        
 rawPresetMul
@@ -3031,33 +3031,8 @@
 sor(
-%0A                        
 vali

Change layout.