diff --git a/src/main/java/org/folio/circulation/infrastructure/storage/ServicePointRepository.java b/src/main/java/org/folio/circulation/infrastructure/storage/ServicePointRepository.java index c7533cd1fc..7225491566 100644 --- a/src/main/java/org/folio/circulation/infrastructure/storage/ServicePointRepository.java +++ b/src/main/java/org/folio/circulation/infrastructure/storage/ServicePointRepository.java @@ -41,7 +41,15 @@ public class ServicePointRepository { private final CollectionResourceClient servicePointsStorageClient; public ServicePointRepository(Clients clients) { - servicePointsStorageClient = clients.servicePointsStorage(); + this(clients, false); + } + + public ServicePointRepository(Clients clients, boolean includeRoutingServicePoints) { + if (includeRoutingServicePoints) { + servicePointsStorageClient = clients.routingServicePointsStorage(); + } else { + servicePointsStorageClient = clients.servicePointsStorage(); + } } public CompletableFuture> getServicePointById(UUID id) { diff --git a/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java b/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java index dbff3faa44..c5ee898a43 100644 --- a/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java +++ b/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java @@ -75,7 +75,7 @@ public AllowedServicePointsService(Clients clients, boolean isEcsRequestRouting) userRepository = new UserRepository(clients); requestRepository = new RequestRepository(clients); requestPolicyRepository = new RequestPolicyRepository(clients); - servicePointRepository = new ServicePointRepository(clients); + servicePointRepository = new ServicePointRepository(clients, isEcsRequestRouting); settingsRepository = new SettingsRepository(clients); instanceRepository = new InstanceRepository(clients); itemFinder = new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository); diff --git a/src/main/java/org/folio/circulation/support/Clients.java b/src/main/java/org/folio/circulation/support/Clients.java index 671cede9f1..0dcc883a34 100644 --- a/src/main/java/org/folio/circulation/support/Clients.java +++ b/src/main/java/org/folio/circulation/support/Clients.java @@ -4,7 +4,9 @@ import org.folio.circulation.rules.CirculationRulesProcessor; import org.folio.circulation.services.PubSubPublishingService; +import org.folio.circulation.support.http.client.IncludeRoutingServicePoints; import org.folio.circulation.support.http.client.OkapiHttpClient; +import org.folio.circulation.support.http.client.QueryParameter; import org.folio.circulation.support.http.server.WebContext; import io.vertx.core.http.HttpClient; @@ -40,6 +42,7 @@ public class Clients { private final CollectionResourceClient circulationRulesStorageClient; private final CollectionResourceClient requestPoliciesStorageClient; private final CollectionResourceClient servicePointsStorageClient; + private final CollectionResourceClient routingServicePointsStorageClient; private final CollectionResourceClient calendarStorageClient; private final CollectionResourceClient patronGroupsStorageClient; private final CollectionResourceClient patronNoticePolicesStorageClient; @@ -113,6 +116,8 @@ private Clients(OkapiHttpClient client, WebContext context) { requestPoliciesStorageClient = createRequestPoliciesStorageClient(client, context); fixedDueDateSchedulesStorageClient = createFixedDueDateSchedulesStorageClient(client, context); servicePointsStorageClient = createServicePointsStorageClient(client, context); + routingServicePointsStorageClient = createServicePointsStorageWithCustomParam(client, + context, IncludeRoutingServicePoints.enabled()); patronGroupsStorageClient = createPatronGroupsStorageClient(client, context); calendarStorageClient = createCalendarStorageClient(client, context); patronNoticePolicesStorageClient = createPatronNoticePolicesStorageClient(client, context); @@ -246,6 +251,10 @@ public CollectionResourceClient servicePointsStorage() { return servicePointsStorageClient; } + public CollectionResourceClient routingServicePointsStorage() { + return routingServicePointsStorageClient; + } + public CollectionResourceClient patronGroupsStorage() { return patronGroupsStorageClient; } @@ -398,6 +407,14 @@ private static CollectionResourceClient getCollectionResourceClient( return new CollectionResourceClient(client, context.getOkapiBasedUrl(path)); } + private static CollectionResourceClient getCollectionResourceClientWithCustomParam( + OkapiHttpClient client, WebContext context, String path, QueryParameter customParam) + throws MalformedURLException { + + return new CustomParamCollectionResourceClient(client, context.getOkapiBasedUrl(path), + customParam); + } + public CollectionResourceClient noticeTemplatesClient() { return noticeTemplatesClient; } @@ -640,6 +657,14 @@ private CollectionResourceClient createServicePointsStorageClient( return getCollectionResourceClient(client, context, "/service-points"); } + private CollectionResourceClient createServicePointsStorageWithCustomParam( + OkapiHttpClient client, WebContext context, QueryParameter customParam) + throws MalformedURLException { + + return getCollectionResourceClientWithCustomParam(client, context, "/service-points", + customParam); + } + private CollectionResourceClient createPatronGroupsStorageClient( OkapiHttpClient client, WebContext context) throws MalformedURLException { diff --git a/src/main/java/org/folio/circulation/support/CollectionResourceClient.java b/src/main/java/org/folio/circulation/support/CollectionResourceClient.java index a5c827108c..0b3db44c9b 100644 --- a/src/main/java/org/folio/circulation/support/CollectionResourceClient.java +++ b/src/main/java/org/folio/circulation/support/CollectionResourceClient.java @@ -18,8 +18,8 @@ import io.vertx.core.json.JsonObject; public class CollectionResourceClient implements GetManyRecordsClient { - private final OkapiHttpClient client; - private final URL collectionRoot; + final OkapiHttpClient client; + final URL collectionRoot; public CollectionResourceClient(OkapiHttpClient client, URL collectionRoot) { this.collectionRoot = collectionRoot; @@ -109,7 +109,7 @@ public CompletableFuture> getMany(CqlQuery cqlQuery, return client.get(collectionRoot, cqlQuery, pageLimit, offset); } - private String individualRecordUrl(String id) { + String individualRecordUrl(String id) { return format("%s/%s", collectionRoot, id); } } diff --git a/src/main/java/org/folio/circulation/support/CustomParamCollectionResourceClient.java b/src/main/java/org/folio/circulation/support/CustomParamCollectionResourceClient.java new file mode 100644 index 0000000000..2c765e3acf --- /dev/null +++ b/src/main/java/org/folio/circulation/support/CustomParamCollectionResourceClient.java @@ -0,0 +1,46 @@ +package org.folio.circulation.support; + +import java.net.URL; +import java.util.concurrent.CompletableFuture; + +import org.folio.circulation.support.http.client.CqlQuery; +import org.folio.circulation.support.http.client.Offset; +import org.folio.circulation.support.http.client.OkapiHttpClient; +import org.folio.circulation.support.http.client.PageLimit; +import org.folio.circulation.support.http.client.QueryParameter; +import org.folio.circulation.support.http.client.Response; +import org.folio.circulation.support.results.Result; + +public class CustomParamCollectionResourceClient extends CollectionResourceClient { + + private QueryParameter customQueryParameter; + + public CustomParamCollectionResourceClient(OkapiHttpClient client, URL collectionRoot, + QueryParameter customQueryParameter) { + + super(client, collectionRoot); + this.customQueryParameter = customQueryParameter; + } + + @Override + public CompletableFuture> get() { + return client.get(collectionRoot.toString(), customQueryParameter); + } + + @Override + public CompletableFuture> get(PageLimit pageLimit) { + return client.get(collectionRoot, pageLimit, customQueryParameter); + } + + @Override + public CompletableFuture> get(String id) { + return client.get(individualRecordUrl(id), customQueryParameter); + } + + @Override + public CompletableFuture> getMany(CqlQuery cqlQuery, + PageLimit pageLimit, Offset offset) { + + return client.get(collectionRoot, cqlQuery, pageLimit, offset, customQueryParameter); + } +} diff --git a/src/main/java/org/folio/circulation/support/http/client/IncludeRoutingServicePoints.java b/src/main/java/org/folio/circulation/support/http/client/IncludeRoutingServicePoints.java new file mode 100644 index 0000000000..ff9d531cad --- /dev/null +++ b/src/main/java/org/folio/circulation/support/http/client/IncludeRoutingServicePoints.java @@ -0,0 +1,33 @@ +package org.folio.circulation.support.http.client; + +import static java.lang.String.format; + +public class IncludeRoutingServicePoints implements QueryParameter { + + private static final String PARAM_NAME = "includeRoutingServicePoints"; + private final Boolean value; + + public static IncludeRoutingServicePoints enabled() { + return new IncludeRoutingServicePoints(true); + } + + private IncludeRoutingServicePoints(Boolean value) { + this.value = value; + } + + @Override + public void consume(QueryStringParameterConsumer consumer) { + if (value != null) { + consumer.consume(PARAM_NAME, value.toString()); + } + } + + @Override + public String toString() { + if (value == null) { + return format("No %s", PARAM_NAME); + } + + return format("%s = \"%s\"", PARAM_NAME, value); + } +} diff --git a/src/test/java/api/support/fakes/FakeCQLToJSONInterpreter.java b/src/test/java/api/support/fakes/FakeCQLToJSONInterpreter.java index 5f9808ea09..8abd49675e 100644 --- a/src/test/java/api/support/fakes/FakeCQLToJSONInterpreter.java +++ b/src/test/java/api/support/fakes/FakeCQLToJSONInterpreter.java @@ -14,13 +14,34 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.folio.circulation.support.http.server.WebContext; import io.vertx.core.json.JsonObject; public class FakeCQLToJSONInterpreter { private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass()); + public List execute(Collection records, String query, + WebContext context) { + + var initiallyFilteredRecords = execute(records, query); + + // Routing SP filtering + String includeRoutingServicePointsParam = context.getStringParameter( + "includeRoutingServicePoints"); + if (Boolean.parseBoolean(includeRoutingServicePointsParam)) { + return records.stream() + .filter(json -> json.containsKey("ecsRequestRouting") + ? json.getBoolean("ecsRequestRouting") + : false) + .toList(); + } + + return initiallyFilteredRecords; + } + public List execute(Collection records, String query) { + final var queryAndSort = splitQueryAndSort(query); final var cqlPredicate = new CqlPredicate(queryAndSort.left); diff --git a/src/test/java/api/support/fakes/FakeOkapi.java b/src/test/java/api/support/fakes/FakeOkapi.java index ae7ab05909..c9b7c96836 100644 --- a/src/test/java/api/support/fakes/FakeOkapi.java +++ b/src/test/java/api/support/fakes/FakeOkapi.java @@ -23,11 +23,11 @@ import java.util.Objects; import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.folio.circulation.support.ValidationErrorFailure; import org.folio.circulation.support.http.client.OkapiHttpClient; import org.folio.circulation.support.results.Result; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; @@ -282,6 +282,7 @@ public void start(Promise startFuture) throws IOException { .withRootPath("/service-points") .withRequiredProperties("name", "code", "discoveryDisplayName") .withUniqueProperties("name") + .withQueryParameters("includeRoutingServicePoints") .withChangeMetadata() .disallowCollectionDelete() .create() diff --git a/src/test/java/api/support/fakes/FakeStorageModule.java b/src/test/java/api/support/fakes/FakeStorageModule.java index 4cf5b18b69..d0193bd02e 100644 --- a/src/test/java/api/support/fakes/FakeStorageModule.java +++ b/src/test/java/api/support/fakes/FakeStorageModule.java @@ -378,8 +378,8 @@ private void getMany(RoutingContext routingContext) { Map resourcesForTenant = getResourcesForTenant(context); - List filteredItems = new FakeCQLToJSONInterpreter() - .execute(resourcesForTenant.values(), query); + List filteredItems = getFakeCQLToJSONInterpreter() + .execute(resourcesForTenant.values(), query, context); List pagedItems = filteredItems.stream() .skip(offset) @@ -408,6 +408,10 @@ private void getMany(RoutingContext routingContext) { response.end(); } + FakeCQLToJSONInterpreter getFakeCQLToJSONInterpreter() { + return new FakeCQLToJSONInterpreter(); + } + private void empty(RoutingContext routingContext) { WebContext context = new WebContext(routingContext); @@ -430,7 +434,7 @@ private void deleteMany(RoutingContext routingContext) { Map resourcesForTenant = getResourcesForTenant(context); - new FakeCQLToJSONInterpreter() + getFakeCQLToJSONInterpreter() .execute(resourcesForTenant.values(), query) .forEach(item -> resourcesForTenant.remove(item.getString("id")));