Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FSADT1-1528): added missing count #1225

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import ca.bc.gov.app.service.ClientSearchService;
import io.micrometer.observation.annotation.Observed;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -143,13 +146,24 @@ public Flux<ForestClientDto> findByClientName(
public Flux<PredictiveSearchResultDto> findByComplexSearch(
@RequestParam(required = false) String value,
@RequestParam(required = false, defaultValue = "0") Integer page,
@RequestParam(required = false, defaultValue = "5") Integer size) {
@RequestParam(required = false, defaultValue = "5") Integer size,
ServerHttpResponse serverResponse) {
if (StringUtils.isNotBlank(value)) {
log.info("Receiving request to do a complex search by {}", value);
return service.complexSearch(value, PageRequest.of(page, size));
return service
.complexSearch(value, PageRequest.of(page, size))
.doOnNext(pair -> serverResponse.getHeaders()
.putIfAbsent("X-Total-Count", List.of(pair.getValue().toString()))
)
.map(Pair::getKey);
} else {
log.info("Receiving request to search the latest entries");
return service.latestEntries(PageRequest.of(page, size));
return service
.latestEntries(PageRequest.of(page, size))
.doOnNext(pair -> serverResponse.getHeaders()
.putIfAbsent("X-Total-Count", List.of(pair.getValue().toString()))
)
.map(Pair::getKey);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ Flux<ForestClientEntity> findClientByIncorporationOrName(
OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY""")
Flux<PredictiveSearchResultDto> findByPredictiveSearch(String value, int limit, long offset);

@Query("""
SELECT
count(c.client_number)
FROM the.forest_client c
LEFT JOIN the.CLIENT_DOING_BUSINESS_AS dba ON c.client_number = dba.client_number
LEFT JOIN the.CLIENT_TYPE_CODE ctc ON c.client_type_code = ctc.client_type_code
LEFT JOIN the.CLIENT_LOCATION cl ON c.client_number = cl.client_number
LEFT JOIN the.CLIENT_STATUS_CODE csc ON c.client_status_code = csc.client_status_code
WHERE
(
c.client_number = :value
OR c.CLIENT_ACRONYM = :value
OR UTL_MATCH.JARO_WINKLER_SIMILARITY(c.client_name,:value) >= 90
OR c.client_name LIKE '%' || :value || '%'
OR UTL_MATCH.JARO_WINKLER_SIMILARITY(c.legal_first_name,:value) >= 90
OR UTL_MATCH.JARO_WINKLER_SIMILARITY(dba.doing_business_as_name,:value) >= 90
OR dba.doing_business_as_name LIKE '%' || :value || '%'
OR c.client_identification = :value
OR UTL_MATCH.JARO_WINKLER_SIMILARITY(c.legal_middle_name,:value) >= 90
OR c.legal_middle_name LIKE '%' || :value || '%'
) AND
cl.CLIENT_LOCN_CODE = '00'""")
Mono<Long> countByPredictiveSearch(String value);

@Query("""
SELECT
c.client_number,
Expand All @@ -128,4 +152,16 @@ Flux<ForestClientEntity> findClientByIncorporationOrName(
OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY""")
Flux<PredictiveSearchResultDto> findByEmptyFullSearch(int limit, long offset);

@Query("""
SELECT
count(c.client_number)
FROM the.forest_client c
LEFT JOIN the.CLIENT_DOING_BUSINESS_AS dba ON c.client_number = dba.client_number
LEFT JOIN the.CLIENT_TYPE_CODE ctc ON c.client_type_code = ctc.client_type_code
LEFT JOIN the.CLIENT_LOCATION cl ON c.client_number = cl.client_number
LEFT JOIN the.CLIENT_STATUS_CODE csc ON c.client_status_code = csc.client_status_code
WHERE
cl.CLIENT_LOCN_CODE = '00'""")
Mono<Long> countByEmptyFullSearch();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -533,22 +534,37 @@ public Flux<ForestClientDto> findByClientName(String clientName) {
);
}

public Flux<PredictiveSearchResultDto> complexSearch(String value, Pageable page) {
public Flux<Pair<PredictiveSearchResultDto, Long>> complexSearch(String value, Pageable page) {
// This condition is for predictive search, and we will stop here if no query param is provided
if (StringUtils.isBlank(value)) {
return Flux.error(new MissingRequiredParameterException("value"));
}
return forestClientRepository
.findByPredictiveSearch(value.toUpperCase(), page.getPageSize(), page.getOffset())
.doOnNext(dto -> log.info("Found complex search for value {} as {} {} with score {}",
value, dto.clientNumber(), dto.name(), dto.score()));
return
forestClientRepository
.countByPredictiveSearch(value.toUpperCase())
.flatMapMany(count ->
forestClientRepository
.findByPredictiveSearch(value.toUpperCase(), page.getPageSize(),
page.getOffset())
.doOnNext(
dto -> log.info("Found complex search for value {} as {} {} with score {}",
value, dto.clientNumber(), dto.name(), dto.score())
)
.map(dto -> Pair.of(dto, count))
);
}

public Flux<PredictiveSearchResultDto> latestEntries(Pageable page) {
return forestClientRepository
.findByEmptyFullSearch(page.getPageSize(), page.getOffset())
.doOnNext(dto -> log.info("Found complex empty search as {} {} with score {}",
dto.clientNumber(), dto.name(), dto.score()));
public Flux<Pair<PredictiveSearchResultDto, Long>> latestEntries(Pageable page) {
return
forestClientRepository
.countByEmptyFullSearch()
.flatMapMany(count ->
forestClientRepository
.findByEmptyFullSearch(page.getPageSize(), page.getOffset())
.doOnNext(dto -> log.info("Found complex empty search as {} {} with score {}",
dto.clientNumber(), dto.name(), dto.score()))
.map(dto -> Pair.of(dto, count))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ void shouldSearchPredicatively(
if (StringUtils.isNotBlank(expectedClientNumber)) {
response
.expectStatus().isOk()
.expectHeader()
.exists("X-Total-Count")
.expectBody()
.jsonPath("$[0].clientNumber").isNotEmpty()
.jsonPath("$[0].clientNumber").isEqualTo(expectedClientNumber)
Expand Down Expand Up @@ -338,6 +340,8 @@ void shouldSearchEmpty() {

response
.expectStatus().isOk()
.expectHeader()
.exists("X-Total-Count")
.expectBody()
.jsonPath("$[0].clientNumber").isNotEmpty()
.jsonPath("$[0].clientName").isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -108,7 +109,7 @@ void shouldSearchWithComplexSearch(
String expectedClientName
) {

FirstStep<PredictiveSearchResultDto> test =
FirstStep<Pair<PredictiveSearchResultDto,Long>> test =
service
.complexSearch(searchValue, PageRequest.of(0, 5))
.as(StepVerifier::create);
Expand All @@ -117,8 +118,10 @@ void shouldSearchWithComplexSearch(
test
.assertNext(dto -> {
assertNotNull(dto);
assertEquals(expectedClientNumber, dto.clientNumber());
assertEquals(expectedClientName, dto.name());
assertEquals(expectedClientNumber, dto.getKey().clientNumber());
assertEquals(expectedClientName, dto.getKey().name());
assertNotNull(dto.getValue());
assertEquals(1, dto.getValue());
});
}
test.verifyComplete();
Expand Down
Loading