<repo_name>skyline0613/bank-spring-batch<file_sep>/src/main/java/ma/ensa/bankspringbatch/web/BankRestController.java
package ma.ensa.bankspringbatch.web;

import ma.ensa.bankspringbatch.batch.BankTransactionItemAnalyticsProcessor;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class BankRestController {

    @Autowired
     private JobLauncher jobLauncher;// permet de lancer notre job
    @Autowired
    private Job job;
    //on peut injecter par constructeur ou par @Autowired
    @Autowired
    private BankTransactionItemAnalyticsProcessor itemAnalyticsProcessor;

    /*
    * C'est un lancement de Job parametrer avec une API REST
    * */
    @GetMapping("/startJob")
    public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
            JobRestartException, JobInstanceAlreadyCompleteException {
        //on doit en 1ier parametrer notre Job par un Map de JobParameter
        Map<String, JobParameter> parametersMap = new HashMap<>();
        parametersMap.put("time",new JobParameter(System.currentTimeMillis()));//içi on a utiliser un seul parametre time
        JobParameters jobParameters = new JobParameters(parametersMap);
        JobExecution jobExecution = jobLauncher.run(job,jobParameters);//Creation d'un JoBExecution
        while (jobExecution.isRunning()){// on a utiliser le while car le JobExecution s'execute dans un Thread
            System.out.println("Job is running ...");
        }
        return jobExecution.getStatus();

    }

    @GetMapping("/analytics")
    public Map<String, Double> analytics(){
        Map<String, Double> map = new HashMap<>();
        map.put("Somme Credits", itemAnalyticsProcessor.getTotalCredit());
        map.put("Somme Debits", itemAnalyticsProcessor.getTotalDebit());
        return map;
    }


}
<file_sep>/src/main/java/ma/ensa/bankspringbatch/batch/BankTransactionItemAnalyticsProcessor.java
package ma.ensa.bankspringbatch.batch;

import lombok.Getter;
import ma.ensa.bankspringbatch.entities.BankTransaction;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

//@Component    //on enleve l'annotation car en va faire l'instanciation du bean dans la classe de config
public class BankTransactionItemAnalyticsProcessor implements ItemProcessor<BankTransaction, BankTransaction> {

    @Getter private double totalDebit;
    @Getter private double totalCredit;

    @Override
    public BankTransaction process(BankTransaction bankTransaction) {
        if(bankTransaction.getTransactionType().equals("D")) totalDebit += bankTransaction.getAmount();
        else if (bankTransaction.getTransactionType().equals("C")) totalCredit += bankTransaction.getAmount();
        return bankTransaction;
    }

    /* Maitenant on a 2 processors, Alors on va les enchainer avec le Pattern Composite
    * Donc, on va changer le beans affecter au '.processor()' dans le jobBuildeFactrory. dans la class de config
    * */
}
