Skip to content

Commit

Permalink
ITEM-388 : FEAT : Ajout statistiques demandes de suppression
Browse files Browse the repository at this point in the history
Refactor batch génération statistiques
  • Loading branch information
pierre-maraval committed Dec 6, 2024
1 parent af6b1a0 commit daae38f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 31 deletions.
8 changes: 5 additions & 3 deletions batch/src/main/java/fr/abes/item/batch/JobConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import fr.abes.item.core.constant.Constant;
import fr.abes.item.core.constant.TYPE_DEMANDE;
import fr.abes.item.core.service.ReferenceService;
import jakarta.persistence.EntityManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
Expand All @@ -38,6 +37,7 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.transaction.PlatformTransactionManager;

Expand All @@ -56,6 +56,7 @@ public class JobConfiguration {
private final StrategyFactory strategyFactory;
private final ProxyRetry proxyRetry;
private final ReferenceService referenceService;
private final JdbcTemplate jdbcTemplate;
@Value("${batch.min.hour}")
int minHour;

Expand All @@ -71,10 +72,11 @@ public class JobConfiguration {
private Integer nbPpnInFileResult;


public JobConfiguration(StrategyFactory strategyFactory, ProxyRetry proxyRetry, ReferenceService referenceService) {
public JobConfiguration(StrategyFactory strategyFactory, ProxyRetry proxyRetry, ReferenceService referenceService, @Qualifier("itemJdbcTemplate") JdbcTemplate jdbcTemplate) {
this.strategyFactory = strategyFactory;
this.proxyRetry = proxyRetry;
this.referenceService = referenceService;
this.jdbcTemplate = jdbcTemplate;
}

@Bean
Expand Down Expand Up @@ -123,7 +125,7 @@ public Tasklet authentifierSurSudocTasklet()

//statistiques application
@Bean
public Tasklet exportStatistiquesTasklet() { return new ExportStatistiquesTasklet(); }
public Tasklet exportStatistiquesTasklet() { return new ExportStatistiquesTasklet(jdbcTemplate); }


//Archivage automatique des demandes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,90 @@
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.FileWriter;
import java.util.Date;
import java.util.List;

@Slf4j
public class ExportStatistiquesTasklet implements Tasklet, StepExecutionListener {
@Autowired
protected DataSource itemDataSource;

@Autowired
protected JdbcTemplate itemJdbcTemplate;
private final JdbcTemplate itemJdbcTemplate;

private Integer annee;
private Integer mois;
private List<NbDemandesTraiteesDto> listeDemandesTraitees;
private List<NbExemplairesTraitesDto> listeExemplairesTraites;

private Date dateDebut;
private Date dateFin;

@Value("${files.upload.statistiques.path}")
private String uploadPath;

public ExportStatistiquesTasklet(JdbcTemplate itemJdbcTemplate) {
this.itemJdbcTemplate = itemJdbcTemplate;
}


@Override
public void beforeStep(StepExecution stepExecution) {
Date dateDebut = (Date) stepExecution.getJobExecution().getExecutionContext().get("dateDebut");
Date dateFin = (Date) stepExecution.getJobExecution().getExecutionContext().get("dateFin");
annee = (int) stepExecution.getJobExecution().getExecutionContext().get("annee");
mois = (int) stepExecution.getJobExecution().getExecutionContext().get("mois");
listeDemandesTraitees = getNbDemandesTraitees(dateDebut, dateFin);
listeExemplairesTraites = getNbExemplairesTraites(dateDebut, dateFin);
this.dateDebut = (Date) stepExecution.getJobExecution().getExecutionContext().get("dateDebut");
this.dateFin = (Date) stepExecution.getJobExecution().getExecutionContext().get("dateFin");
this.annee = (int) stepExecution.getJobExecution().getExecutionContext().get("annee");
this.mois = (int) stepExecution.getJobExecution().getExecutionContext().get("mois");

}

@Override
public RepeatStatus execute(@NonNull StepContribution stepContribution, @NonNull ChunkContext chunkContext) throws Exception {
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBDEMANDESTRAITEES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)){
exportStatistiquesDemandesModif();
exportStatistiquesDemandesSupp();
return RepeatStatus.FINISHED;
}

private void exportStatistiquesDemandesModif() {
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBDEMANDESMODIFTRAITEES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)){
List<NbDemandesTraiteesDto> listeDemandesTraitees = getNbDemandesTraiteesModif(dateDebut, dateFin);
for (NbDemandesTraiteesDto demande : listeDemandesTraitees) {
writer.writeNext(new String[]{demande.getRcr(), demande.getNbDemandesTraitees().toString()});
}
} catch (Exception e) {
log.error(Constant.ERROR_OPENING_FILE_FOR_NUMBER_OF_REQUESTS_PROCESSED_BY_RCR);
log.error(Constant.ERR_FILE_STAT_MODIF_DEMANDES);
}
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBEXEMPLAIRESTRAITES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)) {
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBEXEMPLAIRESMODIFTRAITES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)) {
List<NbExemplairesTraitesDto> listeExemplairesTraites = getNbExemplairesTraitesModif(dateDebut, dateFin);
for (NbExemplairesTraitesDto exemp : listeExemplairesTraites) {
writer.writeNext(new String[]{exemp.getRcr(), exemp.getTypeTraitement().toString(), exemp.getNbExemplaires().toString()});
}
} catch (Exception e) {
log.error(Constant.ERROR_OPENING_FILE_FOR_NUMBER_OF_EXEMPLARIES_PROCESSES_BY_RCR_AND_TREATMENT);
log.error(Constant.ERR_FILE_STAT_MODIF_EXEMP);
}
}

private void exportStatistiquesDemandesSupp() {
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBDEMANDESSUPPTRAITEES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)){
List<NbDemandesTraiteesDto> listeDemandesTraitees = getNbDemandesTraiteesSupp(dateDebut, dateFin);
for (NbDemandesTraiteesDto demande : listeDemandesTraitees) {
writer.writeNext(new String[]{demande.getRcr(), demande.getNbDemandesTraitees().toString()});
}
} catch (Exception e) {
log.error(Constant.ERR_FILE_STAT_SUPP_DEMANDES);
}
try (CSVWriter writer = new CSVWriter(new FileWriter(getFilename(Constant.STAT_NBEXEMPLAIRESSUPPTRAITES_FILENAME)), ';', CSVWriter.NO_QUOTE_CHARACTER)) {
List<NbExemplairesSuppTraitesDto> listeExemplairesTraites = getNbExemplairesTraitesSupp(dateDebut, dateFin);
for (NbExemplairesSuppTraitesDto exemp : listeExemplairesTraites) {
writer.writeNext(new String[]{exemp.getRcr(), exemp.getNbExemplaires().toString()});
}
} catch (Exception e) {
log.error(Constant.ERR_FILE_STAT_SUPP_EXEMP);
}
return RepeatStatus.FINISHED;
}

private List<NbDemandesTraiteesDto> getNbDemandesTraitees(Date dateDebut, Date dateFin) {
String query = "select RCR, count(*) from DEMANDE_MODIF d, JOURNAL_DEMANDE_MODIF j where j.JOU_DEM_ID = d.NUM_DEMANDE and j.JOU_ETA_ID=7 and j.DATE_ENTREE between ? and ? group by d.RCR";
private List<NbDemandesTraiteesDto> getNbDemandesTraiteesModif(Date dateDebut, Date dateFin) {
String query = "select RCR, count(distinct d.NUM_DEMANDE) from DEMANDE_MODIF d join JOURNAL_DEMANDE_MODIF j on j.JOU_DEM_ID = d.NUM_DEMANDE where j.JOU_ETA_ID IN (7, 9) and j.DATE_ENTREE between ? and ? group by d.RCR";
return itemJdbcTemplate.query(query, new Object[] {dateDebut, dateFin}, new NbDemandesTraiteesMapper());
}

private List<NbExemplairesTraitesDto> getNbExemplairesTraites(Date dateDebut, Date dateFin) {
private List<NbExemplairesTraitesDto> getNbExemplairesTraitesModif(Date dateDebut, Date dateFin) {
String query = "select d.DEM_TRAIT_ID, d.RCR, count(*) " +
"from JOURNAL_DEMANDE_MODIF j, DEMANDE_MODIF d, LIGNE_FICHIER_MODIF lf " +
"where j.DATE_ENTREE between ? and ? "+
Expand All @@ -82,6 +104,20 @@ private List<NbExemplairesTraitesDto> getNbExemplairesTraites(Date dateDebut, Da
return itemJdbcTemplate.query(query, new Object[] {dateDebut, dateFin}, new NbExemplairesTraitesMapper());
}

private List<NbDemandesTraiteesDto> getNbDemandesTraiteesSupp(Date dateDebut, Date dateFin) {
String query = "select RCR, count(distinct d.NUM_DEMANDE) from DEMANDE_SUPP d join JOURNAL_DEMANDE_SUPP j on j.JOU_DEM_ID = d.NUM_DEMANDE where j.JOU_ETA_ID IN (7, 9) and j.DATE_ENTREE between ? and ? group by d.RCR";
return itemJdbcTemplate.query(query, new Object[] {dateDebut, dateFin}, new NbDemandesTraiteesMapper());
}

private List<NbExemplairesSuppTraitesDto> getNbExemplairesTraitesSupp(Date dateDebut, Date dateFin) {
String query = "select d.RCR, count(*) " +
"from JOURNAL_DEMANDE_SUPP j, DEMANDE_SUPP d, LIGNE_FICHIER_SUPP lf " +
"where j.DATE_ENTREE between ? and ? "+
"and j.JOU_ETA_ID=6 and j.JOU_DEM_ID = d.NUM_DEMANDE and d.NUM_DEMANDE = lf.REF_DEMANDE and lf.TRAITEE=1 " +
"group by d.RCR";
return itemJdbcTemplate.query(query, new Object[] {dateDebut, dateFin}, new NbExemplairesSuppTraitesMapper());
}

private String getFilename(String filename) {
return uploadPath + annee + ((mois < 10) ? '0' + mois.toString() : mois.toString()) + "_" + filename + Constant.EXTENSIONCSV;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.abes.item.batch.webstats;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@Getter
@Setter
public class NbExemplairesSuppTraitesDto {
private String rcr;
private Integer nbExemplaires;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.abes.item.batch.webstats;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class NbExemplairesSuppTraitesMapper implements RowMapper<NbExemplairesSuppTraitesDto> {
@Override
public NbExemplairesSuppTraitesDto mapRow(ResultSet resultSet, int i) throws SQLException {
NbExemplairesSuppTraitesDto nbExemp = new NbExemplairesSuppTraitesDto();
nbExemp.setRcr(resultSet.getString("RCR"));
nbExemp.setNbExemplaires(resultSet.getInt("count"));
return nbExemp;
}
}
12 changes: 8 additions & 4 deletions core/src/main/java/fr/abes/item/core/constant/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ public class Constant implements Serializable {
public static final int TAILLEMAX = 9;

/**Specific messages on mails sents and stats*/
public static final String STAT_NBDEMANDESTRAITEES_FILENAME = "demandesTraiteesRCR";
public static final String STAT_NBEXEMPLAIRESTRAITES_FILENAME = "exemplairesTraitesTraitementRCR";
public static final String STAT_NBDEMANDESMODIFTRAITEES_FILENAME = "demandesTraiteesModifRCR";
public static final String STAT_NBEXEMPLAIRESMODIFTRAITES_FILENAME = "exemplairesModifTraitesTraitementRCR";
public static final String STAT_NBDEMANDESSUPPTRAITEES_FILENAME = "demandesSuppTraiteesRCR";
public static final String STAT_NBEXEMPLAIRESSUPPTRAITES_FILENAME = "exemplairesSuppTraitesRCR";
public static final String DEMANDE_MODIFICATION_START = "Demande de modification d'exemplaires N° ";
public static final String DEMANDE_SUPPRESSION_START = "Demande de suppression d'exemplaires N° ";
public static final String DEMANDE_EXEMPLARISATION_START = "Votre exemplarisation - ";
Expand Down Expand Up @@ -200,8 +202,10 @@ public class Constant implements Serializable {
public static final String STRATEGY_ANNOTATION_FOR_BEAN_FAILED ="Ne peut résoudre l'annotation strategy pour ce bean.";
public static final String STRATEGY_RETURN_DEFAULT_STRATEGY_NO_TYPE_DEMANDE_SELECTED = "No type_demande selected, returning default strategy";
public static final String STRATEGY_RETURN_DEFAULT_STRATEGY_NO_TYPE_DEMANDE_SPECIFIC_STRATEGY_SELECTED = "No type_demande specific strategy found, returning default strategy";
public static final String ERROR_OPENING_FILE_FOR_NUMBER_OF_REQUESTS_PROCESSED_BY_RCR = "Erreur dans l'ouverture du fichier pour nb demandeModifs traitées par RCR.";
public static final String ERROR_OPENING_FILE_FOR_NUMBER_OF_EXEMPLARIES_PROCESSES_BY_RCR_AND_TREATMENT = "Erreur dans l'ouverture du fichier pour nb exemplaires traites par RCR et Traitement.";
public static final String ERR_FILE_STAT_MODIF_DEMANDES = "Erreur dans l'ouverture du fichier pour nb demande modif traitées par RCR.";
public static final String ERR_FILE_STAT_MODIF_EXEMP = "Erreur dans l'ouverture du fichier pour nb exemplaires de demande de modif traites par RCR et Traitement.";
public static final String ERR_FILE_STAT_SUPP_DEMANDES = "Erreur dans l'ouverture du fichier pour nb demande supp traitées par RCR.";
public static final String ERR_FILE_STAT_SUPP_EXEMP = "Erreur dans l'ouverture du fichier pour nb exemplaires de demande de supp traites par RCR.";
public static final String ERROR_RESPONDING_WITH_UNAUTHORIZED_ERROR = "Erreur : répond avec une erreur d'autorisation : message - {0}";
public static final String ERROR_ACCESS_DATABASE = "Erreur acces a la base";
public static final String ERROR_GENERIC_TECHNICAL_PROBLEMS = "Nous rencontrons actuellement des problèmes techniques.";
Expand Down

0 comments on commit daae38f

Please sign in to comment.