SupplementVerreIteStepConfig.java

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

import com.sintia.ffl.admin.optique.dal.entities.SupplementVerreIte;
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.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 org.springframework.batch.core.repository.JobRepository;
import org.springframework.transaction.PlatformTransactionManager;


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

@Configuration
public class SupplementVerreIteStepConfig extends
        AbstractSinglePromoteurJDBCExportStepConfig<SupplementVerreIte> {


    private static final String QUERY = "select id_supplement_verre_ite, c_supplement_verre, l_supplement_verre, id_type_supplement_ite, id_distributeur_ite, " +
            "id_fabricant_ite, c_type_trait_antireflet, c_procede_special_fabrication, c_type_teinte, b_couche_photochromique, b_antirayure," +
            "b_antisalissure, b_couche_uv, b_saisie, d_creation, d_maj " +
            "from supplement_verre_ite";

    public SupplementVerreIteStepConfig() {
        super(TypeStaging.REF_PROMOTEUR,
                (rs, rowNum) -> {
                    Timestamp dCreation = rs.getTimestamp("d_creation");
                    Timestamp dMaj = rs.getTimestamp("d_maj");
                    return new SupplementVerreIte(
                            rs.getInt("id_supplement_verre_ite"),
                            rs.getString("c_supplement_verre"),
                            rs.getString("l_supplement_verre"),
                            rs.getInt("id_type_supplement_ite"),
                            rs.getInt("id_distributeur_ite"),
                            rs.getInt("id_fabricant_ite"),
                            rs.getString("c_type_trait_antireflet"),
                            rs.getString("c_procede_special_fabrication"),
                            rs.getString("c_type_teinte"),
                            rs.getString("b_couche_photochromique"),
                            rs.getString("b_antirayure"),
                            rs.getString("b_antisalissure"),
                            rs.getString("b_couche_uv"),
                            rs.getString("b_saisie"),
                            dCreation == null ? null : dCreation.toLocalDateTime(),
                            dMaj == null ? null : dMaj.toLocalDateTime(),
                            null
                    );
                },
                CodePromoteur.ITE);
    }

    @Bean
    public Step supplementVerreIteStep(JobRepository jobRepository, PlatformTransactionManager transactionManager, FileUtils fileUtils,
                                       DataSource dataSource) throws IOException {
        return super.buildStep( jobRepository,  transactionManager, fileUtils, dataSource);
    }

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

    @Bean("supplementVerreIteWriter")
    @StepScope
    @Override
    public AsyncItemWriter<SupplementVerreIte> 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);
    }
}