Skip to content

Commit

Permalink
Merge pull request #78 from abes-esr/develop
Browse files Browse the repository at this point in the history
Merge develop dans main
  • Loading branch information
EryneKL authored Feb 7, 2024
2 parents ae26eff + f481f19 commit 99ed1b1
Show file tree
Hide file tree
Showing 21 changed files with 501 additions and 30 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@
<artifactId>spring-web</artifactId>
<version>6.1.0</version>
</dependency>
<!-- BDD -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.3.0.0</version>
</dependency>
<dependency>
<groupId>com.oracle.database.xml</groupId>
<artifactId>xdb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- === Logging === -->
<dependency>
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/fr/abes/kbart2kafka/Kbart2kafkaApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package fr.abes.kbart2kafka;

import fr.abes.kbart2kafka.exception.IllegalDateException;
import fr.abes.kbart2kafka.exception.IllegalFileFormatException;
import fr.abes.kbart2kafka.exception.IllegalPackageException;
import fr.abes.kbart2kafka.exception.IllegalProviderException;
import fr.abes.kbart2kafka.repository.ProviderRepository;
import fr.abes.kbart2kafka.service.FileService;
import fr.abes.kbart2kafka.service.ProviderPackageService;
import fr.abes.kbart2kafka.utils.CheckFiles;
import fr.abes.kbart2kafka.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -21,8 +26,14 @@ public class Kbart2kafkaApplication implements CommandLineRunner {
private String kbartHeader;
private final FileService service;

public Kbart2kafkaApplication(FileService service) {
private final ProviderPackageService providerPackageService;

private final ProviderRepository providerRepository;

public Kbart2kafkaApplication(FileService service, ProviderPackageService providerPackageService, ProviderRepository providerRepository) {
this.service = service;
this.providerPackageService = providerPackageService;
this.providerRepository = providerRepository;
}

public static void main(String[] args) {
Expand All @@ -46,10 +57,10 @@ public void run(String... args) throws IOException {
ThreadContext.put("package", args[0]);
// Récupération du chemin d'accès au fichier
File tsvFile = new File(args[0]);
// Appelle du service de vérification de fichier
try {
CheckFiles.verifyFile(tsvFile, kbartHeader);
} catch (IllegalFileFormatException | IllegalProviderException e) {
checkExistingPackage(tsvFile.getName());
} catch (IllegalFileFormatException | IllegalProviderException | IllegalPackageException | IllegalDateException e) {
log.error(e.getMessage());
throw new RuntimeException(e);
}
Expand All @@ -59,4 +70,9 @@ public void run(String... args) throws IOException {
double executionTime = (double) (endTime - startTime) / 1000;
log.info("Temps d'exécution : " + executionTime + " secondes");
}

private void checkExistingPackage(String filename) throws IllegalProviderException, IllegalPackageException, IllegalDateException {
if (providerPackageService.hasMoreRecentPackageInBdd(Utils.extractProvider(filename), Utils.extractPackageName(filename), Utils.extractDate(filename)))
throw new IllegalPackageException("Un package plus récent est déjà présent dans la base");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.abes.kbart2kafka.configuration;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import java.util.HashMap;

public abstract class AbstractConfig {
protected void configHibernate(LocalContainerEntityManagerFactoryBean em, String platform, boolean showsql, String dialect, String ddlAuto, boolean generateDdl, String initMode) {
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(generateDdl);
vendorAdapter.setShowSql(showsql);
vendorAdapter.setDatabasePlatform(platform);
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.format_sql", true);
properties.put("hibernate.hbm2ddl.auto", ddlAuto);
properties.put("hibernate.dialect", dialect);
properties.put("logging.level.org.hibernate", "DEBUG");
properties.put("hibernate.type", "trace");
properties.put("spring.sql.init.mode", initMode);
em.setJpaPropertyMap(properties);
}
}
56 changes: 56 additions & 0 deletions src/main/java/fr/abes/kbart2kafka/configuration/BaconDbConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fr.abes.kbart2kafka.configuration;

import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "baconEntityManager", transactionManagerRef = "baconTransactionManager", basePackages = "fr.abes.kbart2kafka.repository")
@NoArgsConstructor
public class BaconDbConfig extends AbstractConfig {
@Value("${spring.jpa.bacon.show-sql}")
protected boolean showsql;
@Value("${spring.jpa.bacon.properties.hibernate.dialect}")
protected String dialect;
@Value("${spring.jpa.bacon.hibernate.ddl-auto}")
protected String ddlAuto;
@Value("${spring.jpa.bacon.database-platform}")
protected String platform;
@Value("${spring.jpa.bacon.generate-ddl}")
protected boolean generateDdl;
@Value("${spring.sql.bacon.init.mode}")
protected String initMode;

@Bean
@ConfigurationProperties(prefix = "spring.datasource.bacon")
public DataSource baconDataSource() { return DataSourceBuilder.create().build(); }

@Bean
public LocalContainerEntityManagerFactoryBean baconEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(baconDataSource());
em.setPackagesToScan("fr.abes.kbart2kafka.entity");
configHibernate(em, platform, showsql, dialect, ddlAuto, generateDdl, initMode);
return em;
}

@Primary
@Bean
public PlatformTransactionManager baconTransactionManager(@Qualifier("baconEntityManager") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return transactionManager;
}

}
42 changes: 42 additions & 0 deletions src/main/java/fr/abes/kbart2kafka/entity/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.abes.kbart2kafka.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Entity
@Table(name = "PROVIDER")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Provider implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq-provider")
@SequenceGenerator(name = "seq-provider", sequenceName = "PROVIDER_SEQ", allocationSize = 1)
@Column(name = "IDT_PROVIDER")
private Integer idtProvider;
@Column(name = "PROVIDER")
private String provider;
@Column(name = "NOM_CONTACT")
private String nomContact;
@Column(name = "PRENOM_CONTACT")
private String prenomContact;
@Column(name = "MAIL_CONTACT")
private String mailContact;
@Column(name = "DISPLAY_NAME")
private String displayName;


public Provider(String provider) {
this.provider = provider;
//on ne connait pas le display name à l'avance, on l'initialise au provider pour éviter une erreur not null dans la table
this.displayName = provider;
}


}
59 changes: 59 additions & 0 deletions src/main/java/fr/abes/kbart2kafka/entity/ProviderPackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package fr.abes.kbart2kafka.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "PROVIDER_PACKAGE")
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProviderPackage implements Serializable, Comparable<ProviderPackage> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_PROVIDER_PACKAGE")
private Integer idProviderPackage;
@Column(name = "PACKAGE")
private String packageName;
@Column(name = "DATE_P")
private Date dateP;
@Column(name = "PROVIDER_IDT_PROVIDER")
private Integer providerIdtProvider;
@Column(name = "LABEL_ABES")
private char labelAbes;

@ManyToOne
@JoinColumn(referencedColumnName = "IDT_PROVIDER", insertable = false, updatable = false)
private Provider provider;

public ProviderPackage(String packageName, Date dateP, Integer providerIdtProvider, char labelAbes) {
this.packageName = packageName;
this.dateP = dateP;
this.providerIdtProvider = providerIdtProvider;
this.labelAbes = labelAbes;
}

public ProviderPackage(Integer idProviderPackage, String packageName, Date dateP, Integer providerIdtProvider, char labelAbes) {
this.idProviderPackage = idProviderPackage;
this.packageName = packageName;
this.dateP = dateP;
this.providerIdtProvider = providerIdtProvider;
this.labelAbes = labelAbes;
}

@Override
public String toString() {
return "{ id:"+idProviderPackage + ", packageName:"+packageName+", providerIdt:"+providerIdtProvider+" dateP:"+dateP+" }";
}

@Override
public int compareTo(ProviderPackage o) {
return o.getDateP().compareTo(this.getDateP());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.abes.kbart2kafka.exception;

public class IllegalDateException extends Throwable {
public IllegalDateException(Exception e) {
super(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.abes.kbart2kafka.exception;

public class IllegalPackageException extends Throwable {
public IllegalPackageException(Exception e) {
super(e);
}

public IllegalPackageException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.abes.kbart2kafka.exception;

public class IllegalProviderException extends Throwable {
public class IllegalProviderException extends Exception {
public IllegalProviderException(String message) {
super(message);
}

public IllegalProviderException(Exception e) { super(e); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.abes.kbart2kafka.repository;

import fr.abes.kbart2kafka.entity.ProviderPackage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProviderPackageRepository extends JpaRepository<ProviderPackage, Integer> {
List<ProviderPackage> findByPackageNameAndProviderIdtProvider(String packageName, Integer providerIdtProvider);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.abes.kbart2kafka.repository;


import fr.abes.kbart2kafka.entity.Provider;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ProviderRepository extends JpaRepository<Provider, Integer> {
Optional<Provider> findByProvider(String provider);
}
6 changes: 2 additions & 4 deletions src/main/java/fr/abes/kbart2kafka/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -90,8 +89,7 @@ private void executeMultiThread(File fichier, String kbartHeader) throws IOExcep
assert result != null;
result.whenComplete((sr, ex) -> {
try {
// log.debug("Message envoyé : {}", mapper.writeValueAsString(result.get().getProducerRecord().value()));
mapper.writeValueAsString(result.get().getProducerRecord().value());
log.debug(mapper.writeValueAsString(result.get().getProducerRecord().value())); // vérification du résultat de la transaction et log
} catch (JsonProcessingException | InterruptedException | ExecutionException e) {
log.warn("Erreur dans le mapping du résultat de l'envoi dans le topic pour la ligne " + ligneKbartDto);
}
Expand All @@ -112,7 +110,7 @@ private void executeMultiThread(File fichier, String kbartHeader) throws IOExcep
}

private void sendErrorToKafka(String errorMessage, Exception exception, String filename) {
log.debug("Envoi erreur");
log.error(errorMessage + exception + " - " + filename);
kafkaTemplate.send(new ProducerRecord<>(topicErrors, new Random().nextInt(nbThread), filename, errorMessage + exception.getMessage()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.abes.kbart2kafka.service;

import fr.abes.kbart2kafka.entity.Provider;
import fr.abes.kbart2kafka.entity.ProviderPackage;
import fr.abes.kbart2kafka.repository.ProviderPackageRepository;
import fr.abes.kbart2kafka.repository.ProviderRepository;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ProviderPackageService {
private final ProviderPackageRepository repository;

private final ProviderRepository providerRepository;

public ProviderPackageService(ProviderPackageRepository repository, ProviderRepository providerRepository) {
this.repository = repository;
this.providerRepository = providerRepository;
}

public boolean hasMoreRecentPackageInBdd(String provider, String packageName, Date datePackage) {
Optional<Provider> providerBdd = providerRepository.findByProvider(provider);
if (providerBdd.isPresent()) {
List<ProviderPackage> packageList = repository.findByPackageNameAndProviderIdtProvider(packageName, providerBdd.get().getIdtProvider());
Collections.sort(packageList);
return packageList.get(0).getDateP().after(datePackage);
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Boolean detectFileName(File file) throws IllegalFileFormatExceptio
public static void detectProvider(File file) throws IllegalProviderException {
String filename = file.getName();
filename = filename.replace("\\", "/");
if(!filename.contains("_") || filename.substring(0, filename.indexOf('_')).length() <= 0) {
if(!filename.contains("_") || filename.substring(0, filename.indexOf('_')).isEmpty()) {
log.error("Message envoyé : {}", "Le nom du fichier ne contient pas de provider");
throw new IllegalProviderException("Le nom du fichier "+ filename +" ne contient pas de provider");
}
Expand Down
Loading

0 comments on commit 99ed1b1

Please sign in to comment.