Skip to content

Commit

Permalink
fix: improve search questioning
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Dec 11, 2024
1 parent c9daa6b commit d84a8f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Page<SearchQuestioningDto> searchQuestionings(

Pageable pageable = PageRequest.of(page, pageSize);

return questioningService.searchQuestioning(searchParam.toUpperCase(), pageable);
return questioningService.searchQuestioning(searchParam, pageable);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Questioning, Long> {
Expand Down Expand Up @@ -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<Questioning> 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<Questioning> findQuestioningByParam(String searchParam);

Set<Questioning> findBySurveyUnitIdSu(String idSu);

Page<Questioning> findAll(Pageable pageable);

@Query("""
SELECT q.id FROM Questioning q
""")
Page<Long> 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<Questioning> findQuestioningsByIds(@Param("ids") List<Long> ids);

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,24 @@ public String getAccessUrl(String baseUrl, String typeUrl, String role, Question

@Override
public Page<SearchQuestioningDto> searchQuestioning(String param, Pageable pageable) {
Page<Questioning> pageQuestionings;
if (!StringUtils.isEmpty(param)) {
pageQuestionings = questioningRepository.findQuestioningByParam(param, pageable);
List<Questioning> listQuestionings = questioningRepository.findQuestioningByParam(param.toUpperCase());
List<SearchQuestioningDto> searchDtos = listQuestionings
.stream().distinct()
.map(this::convertToSearchDto).toList();

return new PageImpl<>(searchDtos, pageable, searchDtos.size());
} else {
pageQuestionings = questioningRepository.findAll(pageable);
Page<Long> idsPage = questioningRepository.findQuestioningIds(pageable);
List<Questioning> questionings = questioningRepository.findQuestioningsByIds(idsPage.getContent());
List<SearchQuestioningDto> searchDtos = questionings
.stream()
.map(this::convertToSearchDto).toList();

return new PageImpl<>(searchDtos, pageable, idsPage.getTotalElements());
}

List<SearchQuestioningDto> searchDtos = pageQuestionings
.stream().distinct()
.map(this::convertToSearchDto).toList();

return new PageImpl<>(searchDtos, pageable, pageQuestionings.getTotalElements());
}

@Override
Expand Down

0 comments on commit d84a8f1

Please sign in to comment.