diff --git a/src/main/java/fr/abes/bestppn/controller/BestPpnController.java b/src/main/java/fr/abes/bestppn/controller/BestPpnController.java index c5ca8f4..a4b0a57 100644 --- a/src/main/java/fr/abes/bestppn/controller/BestPpnController.java +++ b/src/main/java/fr/abes/bestppn/controller/BestPpnController.java @@ -17,6 +17,8 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; @Tag(name = "Calcul du meilleur PPN", description = "API de calcul du meilleur PPN pour une ligne tsv") @CrossOrigin(origins = "*") @@ -44,7 +46,7 @@ public PpnControllerDto bestPpn(@RequestParam(name = "provider") String provider @RequestParam(name = "print_identifier", required = false) String printIdentifier, @RequestParam(name = "titleUrl", required = false) String titleUrl, @RequestParam(name = "date_monograph_published_online", required = false) String dateMonographPublishedOnline, @RequestParam(name = "date_monograph_published_print", required = false) String dateMonographPublishedPrint, @RequestParam(name = "first_author", required = false) String firstAuthor, @RequestParam(name = "force", required = false) Boolean force, - @RequestParam(name = "log", required = false) Boolean errors) throws IOException { + @RequestParam(name = "log", required = false) Boolean log) throws IOException { try { LigneKbartDto ligneKbartDto = new LigneKbartDto(); ligneKbartDto.setPublicationType(publicationType); @@ -56,13 +58,15 @@ public PpnControllerDto bestPpn(@RequestParam(name = "provider") String provider ligneKbartDto.setDateMonographPublishedOnline((dateMonographPublishedOnline != null) ? dateMonographPublishedOnline : ""); ligneKbartDto.setFirstAuthor((firstAuthor != null) ? firstAuthor : ""); boolean injectKafka = (force != null) ? force : false; - boolean sendErrors = (errors != null) ? errors : false; - PpnDto ppnDto = service.getBestPpn(ligneKbartDto, provider, injectKafka, sendErrors); - return new PpnControllerDto(ppnDto.getPpn(), ppnDto.getTypeSupport(), ppnDto.getLog()); + boolean sendLog = (log != null) ? log : false; + PpnDto ppnDto = service.getBestPpn(ligneKbartDto, provider, injectKafka, sendLog); + return new PpnControllerDto(ppnDto.getPpn(), ppnDto.getTypeSupport(), ppnDto.getLogs()); } catch (URISyntaxException e) { throw new IllegalArgumentException("Une url dans le champ title_url du kbart n'est pas correcte"); } catch (BestPpnException | RestClientException | IllegalArgumentException | IllegalDoiException e) { - return new PpnControllerDto(e.getMessage()); + List logs = new ArrayList<>(); + logs.add(e.getMessage()); + return new PpnControllerDto(logs); } } } diff --git a/src/main/java/fr/abes/bestppn/dto/PpnControllerDto.java b/src/main/java/fr/abes/bestppn/dto/PpnControllerDto.java index 9a0daff..a969e3c 100644 --- a/src/main/java/fr/abes/bestppn/dto/PpnControllerDto.java +++ b/src/main/java/fr/abes/bestppn/dto/PpnControllerDto.java @@ -7,6 +7,8 @@ import lombok.Setter; import org.springframework.lang.Nullable; +import java.util.List; + @Getter @Setter @NoArgsConstructor @@ -21,15 +23,15 @@ public class PpnControllerDto { @Nullable @JsonInclude(JsonInclude.Include.NON_NULL) - private String log; + private List logs; - public PpnControllerDto (String ppn, TYPE_SUPPORT typeSupport, String log) { + public PpnControllerDto (String ppn, TYPE_SUPPORT typeSupport, List logs) { this.ppn = ppn; this.typeSupport = typeSupport; - this.log = log; + this.logs = logs; } - public PpnControllerDto (String log) { - this.log = log; + public PpnControllerDto (List logs) { + this.logs = logs; } } diff --git a/src/main/java/fr/abes/bestppn/dto/kafka/PpnDto.java b/src/main/java/fr/abes/bestppn/dto/kafka/PpnDto.java index 6118f26..87bd419 100644 --- a/src/main/java/fr/abes/bestppn/dto/kafka/PpnDto.java +++ b/src/main/java/fr/abes/bestppn/dto/kafka/PpnDto.java @@ -7,6 +7,8 @@ import lombok.Setter; import org.springframework.lang.Nullable; +import java.util.List; + @Getter @Setter @NoArgsConstructor @@ -22,7 +24,7 @@ public class PpnDto { private DESTINATION_TOPIC destination; @Nullable - private String log; + private List logs; public PpnDto (String ppn, DESTINATION_TOPIC destination) { this.ppn = ppn; @@ -35,16 +37,16 @@ public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSuppo this.typeSupport = typeSupport; } - public PpnDto (String ppn, DESTINATION_TOPIC destination, String log) { + public PpnDto (String ppn, DESTINATION_TOPIC destination, List logs) { this.ppn = ppn; this.destination = destination; - this.log = log; + this.logs = logs; } - public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSupport, String log) { + public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSupport, List logs) { this.ppn = ppn; this.destination = destination; this.typeSupport = typeSupport; - this.log = log; + this.logs = logs; } } diff --git a/src/main/java/fr/abes/bestppn/service/BestPpnService.java b/src/main/java/fr/abes/bestppn/service/BestPpnService.java index 60dfb05..0e347a0 100644 --- a/src/main/java/fr/abes/bestppn/service/BestPpnService.java +++ b/src/main/java/fr/abes/bestppn/service/BestPpnService.java @@ -11,6 +11,7 @@ import fr.abes.bestppn.kafka.TopicProducer; import fr.abes.bestppn.utils.*; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -18,10 +19,7 @@ import java.io.IOException; import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; @Service @Getter @@ -49,6 +47,10 @@ public class BestPpnService { private final CheckUrlService checkUrlService; + private List kbartLineLogs; + + @Setter + private Boolean sendLogs; public BestPpnService(WsService service, NoticeService noticeService, TopicProducer topicProducer, CheckUrlService checkUrlService) { this.service = service; @@ -57,18 +59,22 @@ public BestPpnService(WsService service, NoticeService noticeService, TopicProdu this.checkUrlService = checkUrlService; } - public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKafka, boolean sendErrors) throws IOException, BestPpnException, URISyntaxException, RestClientException, IllegalArgumentException, IllegalDoiException { + public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKafka, boolean sendLogs) throws IOException, BestPpnException, URISyntaxException, RestClientException, IllegalArgumentException, IllegalDoiException { + kbartLineLogs = new ArrayList<>(); + this.sendLogs = sendLogs; Map ppnElecScoredList = new HashMap<>(); Set ppnPrintResultList = new HashSet<>(); if (!kbart.getPublicationType().isEmpty()) { provider = kbart.getPublicationType().equals(PUBLICATION_TYPE.serial.toString()) ? "" : provider; if (kbart.getOnlineIdentifier() != null && !kbart.getOnlineIdentifier().isEmpty()) { + if (sendLogs) kbartLineLogs.add("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getOnlineIdentifier() + " / provider : " + provider); log.debug("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getOnlineIdentifier() + " / provider : " + provider); feedPpnListFromOnline(kbart, provider, ppnElecScoredList, ppnPrintResultList); } if (kbart.getPrintIdentifier() != null && !kbart.getPrintIdentifier().isEmpty()) { + if (sendLogs) kbartLineLogs.add("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getPrintIdentifier() + " / provider : " + provider); log.debug("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getPrintIdentifier() + " / provider : " + provider); feedPpnListFromPrint(kbart, provider, ppnElecScoredList, ppnPrintResultList); } @@ -82,10 +88,11 @@ public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKaf feedPpnListFromDat(kbart, ppnElecScoredList, ppnPrintResultList); } - return getBestPpnByScore(kbart, ppnElecScoredList, ppnPrintResultList, injectKafka, sendErrors); + return getBestPpnByScore(kbart, ppnElecScoredList, ppnPrintResultList, injectKafka); } private void feedPpnListFromOnline(LigneKbartDto kbart, String provider, Map ppnElecScoredList, Set ppnPrintResultList) throws IOException, URISyntaxException, IllegalArgumentException, BestPpnException { + if (sendLogs) kbartLineLogs.add("Entrée dans onlineId2Ppn"); log.debug("Entrée dans onlineId2Ppn"); try { setScoreToEveryPpnFromResultWS(service.callOnlineId2Ppn(kbart.getPublicationType(), kbart.getOnlineIdentifier(), provider), kbart.getTitleUrl(), this.scoreOnlineId2PpnElect, ppnElecScoredList, ppnPrintResultList); @@ -95,6 +102,7 @@ private void feedPpnListFromOnline(LigneKbartDto kbart, String provider, Map ppnElecScoredList, Set ppnPrintResultList) throws IOException, URISyntaxException, IllegalArgumentException, BestPpnException { + if (sendLogs) kbartLineLogs.add("Entrée dans printId2Ppn"); log.debug("Entrée dans printId2Ppn"); try { ResultWsSudocDto resultCallWs = service.callPrintId2Ppn(kbart.getPublicationType(), kbart.getPrintIdentifier(), provider); @@ -114,14 +122,17 @@ private void feedPpnListFromPrint(LigneKbartDto kbart, String provider, Map ppnElecScoredList, Set ppnPrintResultList) throws IOException { ResultDat2PpnWebDto resultDat2PpnWeb = null; if (!kbart.getAnneeFromDate_monograph_published_online().isEmpty()) { + if (sendLogs) kbartLineLogs.add("Appel dat2ppn : date_monograph_published_online : " + kbart.getAnneeFromDate_monograph_published_online() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor()); log.debug("Appel dat2ppn : date_monograph_published_online : " + kbart.getAnneeFromDate_monograph_published_online() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor()); resultDat2PpnWeb = service.callDat2Ppn(kbart.getAnneeFromDate_monograph_published_online(), kbart.getAuthor(), kbart.getPublicationTitle()); } else if (ppnElecScoredList.isEmpty() && !kbart.getAnneeFromDate_monograph_published_print().isEmpty()) { + if (sendLogs) kbartLineLogs.add("Appel dat2ppn : date_monograph_published_print : " + kbart.getAnneeFromDate_monograph_published_print() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor()); log.debug("Appel dat2ppn : date_monograph_published_print : " + kbart.getAnneeFromDate_monograph_published_print() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor()); resultDat2PpnWeb = service.callDat2Ppn(kbart.getAnneeFromDate_monograph_published_print(), kbart.getAuthor(), kbart.getPublicationTitle()); } if(resultDat2PpnWeb != null && !resultDat2PpnWeb.getPpns().isEmpty()) { for (String ppn : resultDat2PpnWeb.getPpns()) { + if (sendLogs) kbartLineLogs.add("résultat : ppn " + ppn); log.debug("résultat : ppn " + ppn); NoticeXml notice = noticeService.getNoticeByPpn(ppn); if (notice.isNoticeElectronique()) { @@ -140,6 +151,7 @@ private void feedPpnListFromDoi(String doi, String provider, Map ppnWithTypeDto.getTypeSupport().equals(TYPE_SUPPORT.ELECTRONIQUE)).count(); for (PpnWithTypeDto ppn : resultCallWs.getPpns()) { if(ppn.getTypeSupport().equals(TYPE_SUPPORT.IMPRIME)) { + if (sendLogs) kbartLineLogs.add("PPN Imprimé : " + ppn); log.info("PPN Imprimé : " + ppn); ppnPrintResultList.add(ppn.getPpn()); } else if (ppn.getTypeDocument() != TYPE_DOCUMENT.MONOGRAPHIE || ppn.isProviderPresent() || checkUrlService.checkUrlInNotice(ppn.getPpn(), titleUrl)){ setScoreToPpnElect(score, ppnElecResultList, nbPpnElec, ppn); } else { + if (sendLogs) kbartLineLogs.add("Le PPN " + ppn + " n'a pas de provider trouvé"); log.error("Le PPN " + ppn + " n'a pas de provider trouvé"); } } @@ -169,19 +183,21 @@ private void setScoreToPpnElect(int score, Map ppnElecScoredLis } else { ppnElecScoredList.put(ppn.getPpn(), (score / nbPpnElec)); } + if (sendLogs) kbartLineLogs.add("PPN Electronique : " + ppn + " / score : " + ppnElecScoredList.get(ppn.getPpn())); log.info("PPN Electronique : " + ppn + " / score : " + ppnElecScoredList.get(ppn.getPpn())); } - public PpnDto getBestPpnByScore(LigneKbartDto kbart, Map ppnElecResultList, Set ppnPrintResultList, boolean isForced, boolean sendErrors) throws BestPpnException { + public PpnDto getBestPpnByScore(LigneKbartDto kbart, Map ppnElecResultList, Set ppnPrintResultList, boolean isForced) throws BestPpnException { Map ppnElecScore = Utils.getMaxValuesFromMap(ppnElecResultList); return switch (ppnElecScore.size()) { case 0 -> { - log.info("Aucun ppn électronique trouvé." + kbart); + if (sendLogs) kbartLineLogs.add("Aucun ppn électronique trouvé. " + kbart); + log.info("Aucun ppn électronique trouvé. " + kbart); yield switch (ppnPrintResultList.size()) { case 0 -> { kbart.setErrorType("Aucun ppn trouvé"); - if(sendErrors) { - yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC, "Aucun ppn électronique trouvé."); + if(sendLogs) { + yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC, kbartLineLogs); } else { yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC); } @@ -189,8 +205,9 @@ yield switch (ppnPrintResultList.size()) { case 1 -> { kbart.setErrorType("Ppn imprimé trouvé : " + ppnPrintResultList.stream().toList().get(0)); - if(sendErrors) { - yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME, "Ppn imprimé trouvé à la place d'un ppn électronique : " + ppnPrintResultList.stream().toList().get(0)); + if(sendLogs) { + kbartLineLogs.add("Ppn imprimé trouvé : " + ppnPrintResultList.stream().toList().get(0)); + yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME, kbartLineLogs); } else { yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME); } @@ -202,8 +219,9 @@ yield switch (ppnPrintResultList.size()) { // vérification du forçage if (isForced) { log.error(errorString); - if (sendErrors) { - yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON, "Plusieurs ppn imprimés (" + String.join(", ", ppnPrintResultList) + ") ont été trouvés."); + if (sendLogs) { + kbartLineLogs.add("Plusieurs ppn imprimés (" + String.join(", ", ppnPrintResultList) + ") ont été trouvés."); + yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON, kbartLineLogs); } else { yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON); } @@ -213,7 +231,13 @@ yield switch (ppnPrintResultList.size()) { } }; } - case 1 -> new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE); + case 1 -> { + if (sendLogs) { + yield new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE, kbartLineLogs); + } else { + yield new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE); + } + } default -> { String listPpn = String.join(", ", ppnElecScore.keySet()); @@ -222,8 +246,9 @@ yield switch (ppnPrintResultList.size()) { // vérification du forçage if (isForced) { log.error(errorString); - if (sendErrors) { - yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON, errorString); + if (sendLogs) { + kbartLineLogs.add(errorString); + yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON, kbartLineLogs); } else { yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON); } diff --git a/src/test/java/fr/abes/bestppn/service/BestPpnServiceTest.java b/src/test/java/fr/abes/bestppn/service/BestPpnServiceTest.java index ccb7ed5..f0620e8 100644 --- a/src/test/java/fr/abes/bestppn/service/BestPpnServiceTest.java +++ b/src/test/java/fr/abes/bestppn/service/BestPpnServiceTest.java @@ -629,7 +629,8 @@ void bestPpnWithScoreTest1() throws BestPpnException { ppnElecResultList.put("100000001", 10); Set ppnPrintResultList = new HashSet<>(); - PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, false, false); + bestPpnService.setSendLogs(false); + PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, false); Assertions.assertEquals("100000001", result.getPpn()); Assertions.assertEquals(DESTINATION_TOPIC.BEST_PPN_BACON, result.getDestination()); } @@ -643,7 +644,8 @@ void bestPpnWithScoreTest2() throws BestPpnException { ppnElecResultList.put("100000002", 10); Set ppnPrintResultList = new HashSet<>(); - PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, false, false); + bestPpnService.setSendLogs(false); + PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, false); Assertions.assertEquals("100000002", result.getPpn()); Assertions.assertEquals(DESTINATION_TOPIC.BEST_PPN_BACON, result.getDestination()); } @@ -657,7 +659,8 @@ void bestPpnWithScoreTest3() throws BestPpnException { ppnElecResultList.put("100000002", 10); Set ppnPrintResultList = new HashSet<>(); - PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, true, false); + bestPpnService.setSendLogs(false); + PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, true); Assertions.assertEquals("", result.getPpn()); } @@ -672,7 +675,8 @@ void bestPpnWithScoreTest4() throws BestPpnException { ppnPrintResultList.add("100000001"); ppnPrintResultList.add("100000002"); - PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, true, false); + bestPpnService.setSendLogs(false); + PpnDto result = bestPpnService.getBestPpnByScore(kbart, ppnElecResultList, ppnPrintResultList, true); Assertions.assertEquals("", result.getPpn()); }