-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MSEARCH-681: Return Unified List of Inventory Locations in a Consorti…
…um (#568) * feat(search-locations): search locations in consortium return unified List of Inventory Locations in a Consortium Closes: MSEARCH-681 --------- Co-authored-by: viacheslav_kolesnyk <[email protected]>
- Loading branch information
1 parent
af5a303
commit 7c0eab5
Showing
20 changed files
with
700 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/org/folio/search/repository/ConsortiumLocationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.folio.search.repository; | ||
|
||
import static org.folio.search.utils.SearchUtils.TENANT_ID_FIELD_NAME; | ||
import static org.folio.search.utils.SearchUtils.performExceptionalOperation; | ||
import static org.opensearch.search.sort.SortOrder.ASC; | ||
import static org.opensearch.search.sort.SortOrder.DESC; | ||
|
||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.search.domain.dto.ConsortiumLocation; | ||
import org.folio.search.domain.dto.SortOrder; | ||
import org.folio.search.model.SearchResult; | ||
import org.folio.search.service.converter.ElasticsearchDocumentConverter; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.client.RequestOptions; | ||
import org.opensearch.client.RestHighLevelClient; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearch.search.sort.SortBuilders; | ||
import org.springframework.stereotype.Repository; | ||
|
||
|
||
@Log4j2 | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class ConsortiumLocationRepository { | ||
|
||
public static final String LOCATION_INDEX = "location"; | ||
private static final String OPERATION_TYPE = "searchApi"; | ||
private final IndexNameProvider indexNameProvider; | ||
private final ElasticsearchDocumentConverter documentConverter; | ||
|
||
private final RestHighLevelClient client; | ||
|
||
public SearchResult<ConsortiumLocation> fetchLocations(String tenantHeader, | ||
String tenantId, | ||
Integer limit, | ||
Integer offset, | ||
String sortBy, | ||
SortOrder sortOrder) { | ||
|
||
var sourceBuilder = getSearchSourceBuilder(tenantId, limit, offset, sortBy, sortOrder); | ||
var response = search(sourceBuilder, tenantHeader); | ||
return documentConverter.convertToSearchResult(response, ConsortiumLocation.class); | ||
} | ||
|
||
@NotNull | ||
private static SearchSourceBuilder getSearchSourceBuilder(String tenantId, | ||
Integer limit, | ||
Integer offset, | ||
String sortBy, | ||
SortOrder sortOrder) { | ||
var sourceBuilder = new SearchSourceBuilder(); | ||
Optional.ofNullable(tenantId) | ||
.ifPresent(id -> sourceBuilder | ||
.query(QueryBuilders | ||
.termQuery(TENANT_ID_FIELD_NAME, id))); | ||
|
||
return sourceBuilder | ||
.from(offset) | ||
.sort(SortBuilders | ||
.fieldSort(sortBy) | ||
.order(sortOrder == SortOrder.DESC ? DESC : ASC)) | ||
.size(limit); | ||
} | ||
|
||
private SearchResponse search(SearchSourceBuilder sourceBuilder, String tenantHeader) { | ||
var index = indexNameProvider.getIndexName(LOCATION_INDEX, tenantHeader); | ||
var searchRequest = new SearchRequest(index); | ||
searchRequest.source(sourceBuilder); | ||
return performExceptionalOperation(() -> client.search(searchRequest, | ||
RequestOptions.DEFAULT), index, OPERATION_TYPE); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/folio/search/service/consortium/ConsortiumLocationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.folio.search.service.consortium; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.search.domain.dto.ConsortiumLocation; | ||
import org.folio.search.domain.dto.SortOrder; | ||
import org.folio.search.model.SearchResult; | ||
import org.folio.search.repository.ConsortiumLocationRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Log4j2 | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ConsortiumLocationService { | ||
|
||
public static final String NAME = "name"; | ||
public static final String ID = "id"; | ||
public static final String TENANT_ID = "tenantId"; | ||
private final ConsortiumLocationRepository repository; | ||
|
||
public SearchResult<ConsortiumLocation> fetchLocations(String tenantHeader, | ||
String tenantId, | ||
Integer limit, | ||
Integer offset, | ||
String sortBy, | ||
SortOrder sortOrder) { | ||
log.info("fetching consortium locations for tenant: {}, tenantId: {}, sortBy: {}", | ||
tenantHeader, | ||
tenantId, | ||
sortBy); | ||
validateSortByValue(sortBy); | ||
return repository.fetchLocations(tenantHeader, tenantId, limit, offset, sortBy, sortOrder); | ||
} | ||
|
||
private void validateSortByValue(String sortBy) { | ||
if (!(NAME.equals(sortBy) || ID.equals(sortBy) || TENANT_ID.equals(sortBy))) { | ||
throw new IllegalArgumentException("Invalid sortBy value: " + sortBy); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/resources/swagger.api/parameters/consortium-locations-limit-param.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
in: query | ||
name: limit | ||
description: Limit the number of elements returned in the response. | ||
schema: | ||
type: integer | ||
minimum: 0 | ||
maximum: 1000 | ||
default: 1000 |
12 changes: 12 additions & 0 deletions
12
src/main/resources/swagger.api/parameters/sort-by-location-name-param.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
in: query | ||
name: sortBy | ||
description: | | ||
Defines a field to sort by. | ||
Possible values: | ||
- id | ||
- tenantId | ||
- name | ||
required: false | ||
schema: | ||
type: string | ||
default: name |
24 changes: 24 additions & 0 deletions
24
src/main/resources/swagger.api/paths/search-consortium/search-consortium-locations.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
get: | ||
operationId: getConsortiumLocations | ||
summary: Get Consortium Locations | ||
description: Get a list of locations (only for consortium environment) | ||
tags: | ||
- search-consortium | ||
parameters: | ||
- $ref: '../../parameters/tenant-id-query-param.yaml' | ||
- $ref: '../../parameters/consortium-locations-limit-param.yaml' | ||
- $ref: '../../parameters/offset-param.yaml' | ||
- $ref: '../../parameters/sort-by-location-name-param.yaml' | ||
- $ref: '../../parameters/sort-order-param.yaml' | ||
- $ref: '../../parameters/x-okapi-tenant-header.yaml' | ||
responses: | ||
'200': | ||
description: List of locations | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../../schemas/entity/consortiumLocationCollection.yaml' | ||
'400': | ||
$ref: '../../responses/badRequestResponse.yaml' | ||
'500': | ||
$ref: '../../responses/internalServerErrorResponse.yaml' |
11 changes: 11 additions & 0 deletions
11
src/main/resources/swagger.api/schemas/entity/consortiumLocation.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: object | ||
properties: | ||
id: | ||
description: Location ID | ||
type: string | ||
name: | ||
description: Location name | ||
type: string | ||
tenantId: | ||
description: Tenant ID of the Location | ||
type: string |
8 changes: 8 additions & 0 deletions
8
src/main/resources/swagger.api/schemas/entity/consortiumLocationCollection.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type: object | ||
properties: | ||
locations: | ||
type: array | ||
items: | ||
$ref: './consortiumLocation.yaml' | ||
totalRecords: | ||
type: integer |
Oops, something went wrong.