VerreSupplementAssoIteStepConfig.java

package com.sintia.ffl.admin.optique.services.batch.refpromoteur.export.ite;

import com.sintia.ffl.admin.optique.dal.entities.VerreSupplementAssoIte;
import com.sintia.ffl.admin.staging.FileUtils;
import com.sintia.ffl.admin.staging.GenerericJDBCItemReadFromDB;
import com.sintia.ffl.admin.staging.step.export.jdbc.AbstractSinglePromoteurJDBCExportStepConfig;
import com.sintia.ffl.adminui.commons.enums.TypeStaging;
import com.sintia.ffl.core.commons.enums.CodePromoteur;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.integration.async.AsyncItemWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Timestamp;

@Configuration
public class VerreSupplementAssoIteStepConfig extends AbstractSinglePromoteurJDBCExportStepConfig<VerreSupplementAssoIte> {

    private static final String QUERY = "select id_verre_supplement_asso_ite, id_modele_verre_ite, " +
            "id_supplement_verre_ite, b_obligatoire, b_inclus, b_saisie, d_creation, d_maj " +
            "from verre_supplement_asso_ite";

    public VerreSupplementAssoIteStepConfig(){
        super(
                TypeStaging.REF_PROMOTEUR,

                (rs, rowNum) -> {
                    Timestamp dCreation = rs.getTimestamp("d_creation");
                    Timestamp dMaj = rs.getTimestamp("d_maj");
                    return new VerreSupplementAssoIte(
                            rs.getInt("id_verre_supplement_asso_ite"),
                            rs.getInt("id_modele_verre_ite"),
                            rs.getInt("id_supplement_verre_ite"),
                            rs.getString("b_obligatoire"),
                            rs.getString("b_inclus"),
                            rs.getString("b_saisie"),
                            dCreation == null ? null : dCreation.toLocalDateTime(),
                            dMaj == null ? null : dMaj.toLocalDateTime()
                    );
                },
                CodePromoteur.ITE
        );
    }
    
    @Bean
    public Step verreSupplementAssoIteStep(StepBuilderFactory stepBuilderFactory, FileUtils fileUtils,
                                           DataSource dataSource) throws IOException {
        return super.buildStep(stepBuilderFactory, fileUtils, dataSource);
    }

    @Bean("verreSupplementAssoIteReader")
    @StepScope
    @Override
    protected GenerericJDBCItemReadFromDB<VerreSupplementAssoIte> reader(
            DataSource dataSource,
            @Value("#{jobParameters['idPromoteur']}") Integer idPromoteur,
            @Value("#{jobParameters['codePromoteur']}") String codePromoteur) throws IOException {
        return super.buildReader(dataSource, QUERY, codePromoteur);
    }

    @Bean("verreSupplementAssoIteWriter")
    @StepScope
    @Override
    public AsyncItemWriter<VerreSupplementAssoIte> writer(
            FileUtils fileUtils,
            @Value("#{jobParameters['envSource'] ?: 'pprod'}") String envSource,
            @Value("#{jobParameters['envCible'] ?: 'pprod'}") String envCible,
            @Value("#{jobParameters['codePromoteur']}") String codePromoteur
    ) throws IOException {
        return super.buildWriter(fileUtils, envSource, envCible, codePromoteur);
    }
}