From d84a8f1f06e76818e062c342043d69814231f2a7 Mon Sep 17 00:00:00 2001 From: Betty Becuwe Date: Wed, 11 Dec 2024 10:41:18 +0100 Subject: [PATCH] fix: improve search questioning --- .../SearchQuestioningController.java | 2 +- .../repository/QuestioningRepository.java | 60 ++++++++++++------- .../service/impl/QuestioningServiceImpl.java | 19 +++--- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/platine-management-api/src/main/java/fr/insee/survey/datacollectionmanagement/query/controller/SearchQuestioningController.java b/platine-management-api/src/main/java/fr/insee/survey/datacollectionmanagement/query/controller/SearchQuestioningController.java index f22bfe6..639a0ac 100644 --- a/platine-management-api/src/main/java/fr/insee/survey/datacollectionmanagement/query/controller/SearchQuestioningController.java +++ b/platine-management-api/src/main/java/fr/insee/survey/datacollectionmanagement/query/controller/SearchQuestioningController.java @@ -38,7 +38,7 @@ public Page searchQuestionings( Pageable pageable = PageRequest.of(page, pageSize); - return questioningService.searchQuestioning(searchParam.toUpperCase(), pageable); + return questioningService.searchQuestioning(searchParam, pageable); } diff --git a/platine-management-db/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/repository/QuestioningRepository.java b/platine-management-db/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/repository/QuestioningRepository.java index 51a16ba..803bb37 100644 --- a/platine-management-db/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/repository/QuestioningRepository.java +++ b/platine-management-db/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/repository/QuestioningRepository.java @@ -5,7 +5,9 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import java.util.List; import java.util.Set; public interface QuestioningRepository extends JpaRepository { @@ -50,31 +52,45 @@ Questioning findByIdPartitioningAndSurveyUnitIdSu(String idPartitioning, and qa3_0.id_contact=:searchParam) """, nativeQuery = true)*/ @Query(""" - SELECT q FROM Questioning q - LEFT JOIN FETCH q.questioningAccreditations acc - LEFT JOIN FETCH q.questioningEvents evt - LEFT JOIN FETCH q.questioningCommunications comm - WHERE q.surveyUnit.idSu = :searchParam - UNION - SELECT q FROM Questioning q - LEFT JOIN FETCH q.questioningAccreditations acc - LEFT JOIN FETCH q.questioningEvents evt - LEFT JOIN FETCH q.questioningCommunications comm - WHERE q.surveyUnit.identificationName = :searchParam - UNION - SELECT q FROM Questioning q - LEFT JOIN FETCH q.questioningAccreditations acc - LEFT JOIN FETCH q.questioningEvents evt - LEFT JOIN FETCH q.questioningCommunications comm - WHERE EXISTS ( - SELECT 1 FROM QuestioningAccreditation qa - WHERE qa.questioning = q - ) AND acc.idContact = :searchParam -""") - Page findQuestioningByParam(String searchParam, Pageable pageable); + SELECT q FROM Questioning q + LEFT JOIN FETCH q.questioningAccreditations acc + LEFT JOIN FETCH q.questioningEvents evt + LEFT JOIN FETCH q.questioningCommunications comm + WHERE q.surveyUnit.idSu = :searchParam + UNION + SELECT q FROM Questioning q + LEFT JOIN FETCH q.questioningAccreditations acc + LEFT JOIN FETCH q.questioningEvents evt + LEFT JOIN FETCH q.questioningCommunications comm + WHERE q.surveyUnit.identificationName = :searchParam + UNION + SELECT q FROM Questioning q + LEFT JOIN FETCH q.questioningAccreditations acc + LEFT JOIN FETCH q.questioningEvents evt + LEFT JOIN FETCH q.questioningCommunications comm + WHERE EXISTS ( + SELECT 1 FROM QuestioningAccreditation qa + WHERE qa.questioning = q + AND qa.idContact = :searchParam) + """) + List findQuestioningByParam(String searchParam); Set findBySurveyUnitIdSu(String idSu); Page findAll(Pageable pageable); + @Query(""" + SELECT q.id FROM Questioning q + """) + Page findQuestioningIds(Pageable pageable); + + @Query(""" + SELECT q FROM Questioning q + LEFT JOIN FETCH q.questioningAccreditations acc + LEFT JOIN FETCH q.questioningEvents evt + LEFT JOIN FETCH q.questioningCommunications comm + WHERE q.id IN :ids + """) + List findQuestioningsByIds(@Param("ids") List ids); + } diff --git a/platine-management-service/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/service/impl/QuestioningServiceImpl.java b/platine-management-service/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/service/impl/QuestioningServiceImpl.java index b462baa..2cc6612 100644 --- a/platine-management-service/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/service/impl/QuestioningServiceImpl.java +++ b/platine-management-service/src/main/java/fr/insee/survey/datacollectionmanagement/questioning/service/impl/QuestioningServiceImpl.java @@ -141,19 +141,24 @@ public String getAccessUrl(String baseUrl, String typeUrl, String role, Question @Override public Page searchQuestioning(String param, Pageable pageable) { - Page pageQuestionings; if (!StringUtils.isEmpty(param)) { - pageQuestionings = questioningRepository.findQuestioningByParam(param, pageable); + List listQuestionings = questioningRepository.findQuestioningByParam(param.toUpperCase()); + List searchDtos = listQuestionings + .stream().distinct() + .map(this::convertToSearchDto).toList(); + + return new PageImpl<>(searchDtos, pageable, searchDtos.size()); } else { - pageQuestionings = questioningRepository.findAll(pageable); + Page idsPage = questioningRepository.findQuestioningIds(pageable); + List questionings = questioningRepository.findQuestioningsByIds(idsPage.getContent()); + List searchDtos = questionings + .stream() + .map(this::convertToSearchDto).toList(); + return new PageImpl<>(searchDtos, pageable, idsPage.getTotalElements()); } - List searchDtos = pageQuestionings - .stream().distinct() - .map(this::convertToSearchDto).toList(); - return new PageImpl<>(searchDtos, pageable, pageQuestionings.getTotalElements()); } @Override