Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/cleanCode' into CDE-195-creer-co…
Browse files Browse the repository at this point in the history
…nsumer-pour-generer-les-2-fichiers-de-log

# Conflicts:
#	src/main/java/fr/abes/logskbart/kafka/KbartListener.java
#	src/main/java/fr/abes/logskbart/service/KbartService.java
#	src/main/java/fr/abes/logskbart/service/KbartToTsvService.java
  • Loading branch information
SamuelQuetin committed Oct 26, 2023
2 parents bfce834 + cc9ea9c commit 44e6d64
Show file tree
Hide file tree
Showing 9 changed files with 1 addition and 221 deletions.
51 changes: 0 additions & 51 deletions src/main/java/fr/abes/logskbart/kafka/KbartListener.java
Original file line number Diff line number Diff line change
@@ -1,51 +0,0 @@
package fr.abes.logskbart.kafka;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.abes.logskbart.dto.PackageKbartDto;
import fr.abes.logskbart.dto.PackageKbartDtoKafka;
import fr.abes.logskbart.exception.IllegalDateException;
import fr.abes.logskbart.exception.IllegalPackageException;
import fr.abes.logskbart.exception.IllegalProviderException;
import fr.abes.logskbart.service.KbartService;
import fr.abes.logskbart.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class KbartListener {

// TODO Supprimer ce listener

@Autowired
private ObjectMapper mapper;

@Autowired
private KbartService service;

@KafkaListener(topics = {"packageKbart"}, groupId = "lignesKbart", containerFactory = "kafkaKbartListenerContainerFactory")
public void listenKbartFromKafka(ConsumerRecord<String, String> lignesKbart) {
PackageKbartDto packageKbartDto = new PackageKbartDto();
try {
packageKbartDto.setPackageName(Utils.extractPackageName(lignesKbart.key()));
packageKbartDto.setProvider(Utils.extractProvider(lignesKbart.key()));
packageKbartDto.setDatePackage(Utils.extractDate(lignesKbart.key()));
PackageKbartDtoKafka packageFromKafka = mapper.readValue(lignesKbart.value(), PackageKbartDtoKafka.class);
packageKbartDto.setLigneKbartDtos(packageFromKafka.getKbartDtos());

//traitement de la liste de dto à traiter en fonction
service.chargerKbart(packageKbartDto);
} catch (IllegalPackageException | IllegalProviderException | IllegalDateException e) {
log.error("Erreur dans les données en entrée, provider / nom de package ou format de date incorrect");
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
}


}
7 changes: 0 additions & 7 deletions src/main/java/fr/abes/logskbart/service/KbartLoader.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/fr/abes/logskbart/service/KbartLoaderFactory.java

This file was deleted.

26 changes: 0 additions & 26 deletions src/main/java/fr/abes/logskbart/service/KbartService.java
Original file line number Diff line number Diff line change
@@ -1,26 +0,0 @@
package fr.abes.logskbart.service;

import fr.abes.logskbart.dto.PackageKbartDto;
import fr.abes.logskbart.utils.LoaderType;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class KbartService {

// TODO Supprimer cette classe

private final KbartLoaderFactory factory;

private final Environment env;

public KbartService(KbartLoaderFactory factory, Environment env) {
this.factory = factory;
this.env = env;
}

public void chargerKbart(PackageKbartDto packageKbart) {
KbartLoader loader = factory.getLoader(LoaderType.valueOf(env.getProperty("LOADERTYPE")));
loader.chargerPackageKbart(packageKbart);
}
}
58 changes: 0 additions & 58 deletions src/main/java/fr/abes/logskbart/service/KbartToBddService.java

This file was deleted.

50 changes: 0 additions & 50 deletions src/main/java/fr/abes/logskbart/service/KbartToTsvService.java
Original file line number Diff line number Diff line change
@@ -1,50 +0,0 @@
package fr.abes.logskbart.service;

import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.CsvBindByPosition;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import fr.abes.logskbart.dto.LigneKbartDto;
import fr.abes.logskbart.dto.PackageKbartDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

@Service
public class KbartToTsvService implements KbartLoader {

// TODO supprimer cette classe

@Value("${path.tsvFile}")
private String pathToFichier;

@Override
public void chargerPackageKbart(PackageKbartDto packageKbartDto) {
try (Writer writer = new FileWriter(pathToFichier + packageKbartDto.getPackageName())) {
writer.append(buildHeader(LigneKbartDto.class));
StatefulBeanToCsv<LigneKbartDto> sbc = new StatefulBeanToCsvBuilder<LigneKbartDto>(writer).withSeparator('\t').withApplyQuotesToAll(false).build();
sbc.write(packageKbartDto.getLigneKbartDtos());
} catch (IOException | CsvRequiredFieldEmptyException | CsvDataTypeMismatchException e) {
throw new RuntimeException(e);
}

}

private String buildHeader(Class<LigneKbartDto> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.filter(f -> f.getAnnotation(CsvBindByPosition.class) != null
&& f.getAnnotation(CsvBindByName.class) != null)
.sorted(Comparator.comparing(f -> f.getAnnotation(CsvBindByPosition.class).position()))
.map(f -> f.getAnnotation(CsvBindByName.class).column())
.collect(Collectors.joining("\t")) + "\n";
}

}
2 changes: 0 additions & 2 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ spring.jpa.bacon.hibernate.ddl-auto=none
spring.jpa.bacon.show-sql=true
spring.sql.bacon.init.mode=never

path.tsvFile=/tmp/

logging.config=classpath:log4j2-dev.xml
logging.level.root=INFO
logging.level.fr.abes=DEBUG
4 changes: 1 addition & 3 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ spring.jpa.bacon.hibernate.ddl-auto=none
spring.jpa.bacon.show-sql=true
spring.sql.bacon.init.mode=never

path.tsvFile=/tmp/

logging.config=classpath:log4j2-prod.xml
logging.level.root=ERROR
logging.level.fr.abes=ERROR
logging.level.fr.abes=ERROR
2 changes: 0 additions & 2 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ spring.jpa.bacon.hibernate.ddl-auto=none
spring.jpa.bacon.show-sql=true
spring.sql.bacon.init.mode=never

path.tsvFile=/tmp/

logging.config=classpath:log4j2-test.xml
logging.level.root=INFO
logging.level.fr.abes=INFO

0 comments on commit 44e6d64

Please sign in to comment.