-
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.
feat(search-instances): implement endpoint for consolidate holdings a…
…ccess in consortium Closes: MSEARCH-692 Signed-off-by: psmagin <[email protected]>
- Loading branch information
Showing
32 changed files
with
1,446 additions
and
464 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
54 changes: 54 additions & 0 deletions
54
src/main/java/org/folio/search/controller/SearchConsortiumController.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,54 @@ | ||
package org.folio.search.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.folio.search.domain.dto.ConsortiumHoldingCollection; | ||
import org.folio.search.domain.dto.SortOrder; | ||
import org.folio.search.exception.RequestValidationException; | ||
import org.folio.search.model.service.ConsortiumSearchContext; | ||
import org.folio.search.model.types.ResourceType; | ||
import org.folio.search.rest.resource.SearchConsortiumApi; | ||
import org.folio.search.service.consortium.ConsortiumInstanceService; | ||
import org.folio.search.service.consortium.ConsortiumTenantService; | ||
import org.folio.spring.integration.XOkapiHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Validated | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/") | ||
public class SearchConsortiumController implements SearchConsortiumApi { | ||
|
||
static final String REQUEST_NOT_ALLOWED_MSG = | ||
"The request allowed only for central tenant of consortium environment"; | ||
|
||
private final ConsortiumTenantService consortiumTenantService; | ||
private final ConsortiumInstanceService instanceService; | ||
|
||
@Override | ||
public ResponseEntity<ConsortiumHoldingCollection> getConsortiumHoldings(String tenantHeader, String instanceId, | ||
String tenantId, Integer limit, | ||
Integer offset, String sortBy, | ||
SortOrder sortOrder) { | ||
checkAllowance(tenantHeader); | ||
var context = ConsortiumSearchContext.builderFor(ResourceType.HOLDINGS) | ||
.filter("instanceId", instanceId) | ||
.filter("tenantId", tenantId) | ||
.limit(limit) | ||
.offset(offset) | ||
.sortBy(sortBy) | ||
.sortOrder(sortOrder) | ||
.build(); | ||
return ResponseEntity.ok(instanceService.fetchHoldings(context)); | ||
} | ||
|
||
private void checkAllowance(String tenantHeader) { | ||
var centralTenant = consortiumTenantService.getCentralTenant(tenantHeader); | ||
if (centralTenant.isEmpty() || !centralTenant.get().equals(tenantHeader)) { | ||
throw new RequestValidationException(REQUEST_NOT_ALLOWED_MSG, XOkapiHeaders.TENANT, tenantHeader); | ||
} | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/org/folio/search/model/service/ConsortiumSearchContext.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,100 @@ | ||
package org.folio.search.model.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.folio.search.domain.dto.SortOrder; | ||
import org.folio.search.exception.RequestValidationException; | ||
import org.folio.search.model.Pair; | ||
import org.folio.search.model.types.ResourceType; | ||
|
||
@Getter | ||
public class ConsortiumSearchContext { | ||
|
||
static final String SORT_NOT_ALLOWED_MSG = "Not allowed sort field for %s"; | ||
static final String FILTER_REQUIRED_MSG = "At least one filter criteria required"; | ||
|
||
private static final Map<ResourceType, List<String>> ALLOWED_SORT_FIELDS = Map.of( | ||
ResourceType.HOLDINGS, List.of("id", "hrid", "tenantId", "instanceId", | ||
"callNumberPrefix", "callNumber", "copyNumber", "permanentLocationId") | ||
); | ||
|
||
private static final Map<ResourceType, String> DEFAULT_SORT_FIELD = Map.of( | ||
ResourceType.HOLDINGS, "id" | ||
); | ||
|
||
private final ResourceType resourceType; | ||
private final List<Pair<String, String>> filters; | ||
private final Integer limit; | ||
private final Integer offset; | ||
private final String sortBy; | ||
private final SortOrder sortOrder; | ||
|
||
ConsortiumSearchContext(ResourceType resourceType, List<Pair<String, String>> filters, Integer limit, Integer offset, | ||
String sortBy, SortOrder sortOrder) { | ||
this.resourceType = resourceType; | ||
this.filters = filters; | ||
if (sortBy != null && !ALLOWED_SORT_FIELDS.get(resourceType).contains(sortBy)) { | ||
throw new RequestValidationException(SORT_NOT_ALLOWED_MSG.formatted(resourceType.getValue()), "sortBy", sortBy); | ||
} | ||
if (sortBy != null && (filters.isEmpty())) { | ||
throw new RequestValidationException(FILTER_REQUIRED_MSG, null, null); | ||
} | ||
this.limit = limit; | ||
this.offset = offset; | ||
this.sortBy = sortBy; | ||
this.sortOrder = sortOrder; | ||
} | ||
|
||
public static ConsortiumSearchContextBuilder builderFor(ResourceType resourceType) { | ||
return new ConsortiumSearchContextBuilder(resourceType); | ||
} | ||
|
||
public static class ConsortiumSearchContextBuilder { | ||
private final ResourceType resourceType; | ||
private List<Pair<String, String>> filters = new ArrayList<>(); | ||
private Integer limit; | ||
private Integer offset; | ||
private String sortBy; | ||
private SortOrder sortOrder; | ||
|
||
ConsortiumSearchContextBuilder(ResourceType resourceType) { | ||
this.resourceType = resourceType; | ||
} | ||
|
||
public ConsortiumSearchContextBuilder filter(String name, String value) { | ||
if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) { | ||
this.filters.add(Pair.pair(name, value)); | ||
} | ||
return this; | ||
} | ||
|
||
public ConsortiumSearchContextBuilder limit(Integer limit) { | ||
this.limit = limit; | ||
return this; | ||
} | ||
|
||
public ConsortiumSearchContextBuilder offset(Integer offset) { | ||
this.offset = offset; | ||
return this; | ||
} | ||
|
||
public ConsortiumSearchContextBuilder sortBy(String sortBy) { | ||
this.sortBy = sortBy; | ||
return this; | ||
} | ||
|
||
public ConsortiumSearchContextBuilder sortOrder(SortOrder sortOrder) { | ||
this.sortOrder = sortOrder; | ||
return this; | ||
} | ||
|
||
public ConsortiumSearchContext build() { | ||
return new ConsortiumSearchContext(this.resourceType, this.filters, this.limit, this.offset, | ||
this.sortBy, this.sortOrder); | ||
} | ||
} | ||
} | ||
|
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
Oops, something went wrong.