-
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-772]. Return Unified List of Inventory Libraries in a Consor…
…tium
- Loading branch information
1 parent
7c1baac
commit b298099
Showing
19 changed files
with
726 additions
and
9 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
79 changes: 79 additions & 0 deletions
79
src/main/java/org/folio/search/repository/ConsortiumLibraryRepository.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,79 @@ | ||
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.ConsortiumLibrary; | ||
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 ConsortiumLibraryRepository { | ||
|
||
public static final String LIBRARY_INDEX = "library"; | ||
private static final String OPERATION_TYPE = "searchApi"; | ||
private final IndexNameProvider indexNameProvider; | ||
private final ElasticsearchDocumentConverter documentConverter; | ||
|
||
private final RestHighLevelClient client; | ||
|
||
public SearchResult<ConsortiumLibrary> fetchLibraries(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, ConsortiumLibrary.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(LIBRARY_INDEX, tenantHeader); | ||
var searchRequest = new SearchRequest(index); | ||
|
||
searchRequest.source(sourceBuilder); | ||
|
||
return performExceptionalOperation(() -> client.search(searchRequest, | ||
RequestOptions.DEFAULT), index, OPERATION_TYPE); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/folio/search/service/consortium/ConsortiumLibraryService.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,45 @@ | ||
package org.folio.search.service.consortium; | ||
|
||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.search.domain.dto.ConsortiumLibrary; | ||
import org.folio.search.domain.dto.SortOrder; | ||
import org.folio.search.model.SearchResult; | ||
import org.folio.search.repository.ConsortiumLibraryRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Log4j2 | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ConsortiumLibraryService { | ||
|
||
private static final Set<String> VALID_SORT_BY = Set.of("id", "name", "tenantId"); | ||
private final ConsortiumLibraryRepository repository; | ||
private final ConsortiumTenantExecutor executor; | ||
|
||
public SearchResult<ConsortiumLibrary> fetchLibraries(String tenantHeader, | ||
String tenantId, | ||
Integer limit, | ||
Integer offset, | ||
String sortBy, | ||
SortOrder sortOrder) { | ||
log.info("fetching consortium libraries for tenant: {}, tenantId: {}, sortBy: {}", | ||
tenantHeader, | ||
tenantId, | ||
sortBy); | ||
|
||
validateSortByValue(sortBy); | ||
|
||
return executor.execute( | ||
tenantHeader, | ||
() -> repository.fetchLibraries(tenantHeader, tenantId, limit, offset, sortBy, sortOrder)); | ||
} | ||
|
||
private void validateSortByValue(String sortBy) { | ||
if (!VALID_SORT_BY.contains(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-libraries-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 |
13 changes: 13 additions & 0 deletions
13
src/main/resources/swagger.api/parameters/sort-by-library-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,13 @@ | ||
in: query | ||
name: sortBy | ||
description: | | ||
Defines a field to sort by. | ||
Possible values: | ||
- id | ||
- tenantId | ||
- campusId | ||
- 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-libraries.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: getConsortiumLibraries | ||
summary: Get Consortium Libraries | ||
description: Get a list of libraries (only for consortium environment) | ||
tags: | ||
- search-consortium | ||
parameters: | ||
- $ref: '../../parameters/tenant-id-query-param.yaml' | ||
- $ref: '../../parameters/consortium-libraries-limit-param.yaml' | ||
- $ref: '../../parameters/offset-param.yaml' | ||
- $ref: '../../parameters/sort-by-library-name-param.yaml' | ||
- $ref: '../../parameters/sort-order-param.yaml' | ||
- $ref: '../../parameters/x-okapi-tenant-header.yaml' | ||
responses: | ||
'200': | ||
description: List of libraries | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../../schemas/entity/consortiumLibraryCollection.yaml' | ||
'400': | ||
$ref: '../../responses/badRequestResponse.yaml' | ||
'500': | ||
$ref: '../../responses/internalServerErrorResponse.yaml' |
16 changes: 16 additions & 0 deletions
16
src/main/resources/swagger.api/schemas/entity/consortiumLibrary.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,16 @@ | ||
type: object | ||
properties: | ||
id: | ||
description: Library ID | ||
type: string | ||
tenantId: | ||
description: Tenant ID of the Library | ||
type: string | ||
name: | ||
description: Library name | ||
type: string | ||
campusId: | ||
description: The UUID of the campus, the second-level location unit, this (shelf) library belongs to. | ||
type: string | ||
metadata: | ||
$ref: "../dto/common/metadata.yaml" |
8 changes: 8 additions & 0 deletions
8
src/main/resources/swagger.api/schemas/entity/consortiumLibraryCollection.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: | ||
libraries: | ||
type: array | ||
items: | ||
$ref: './consortiumLibrary.yaml' | ||
totalRecords: | ||
type: integer |
Oops, something went wrong.