From 512370c64b953b8b012df788f6d5ca8524fa021f Mon Sep 17 00:00:00 2001 From: Janis Saldabols Date: Mon, 20 May 2024 13:10:30 +0300 Subject: [PATCH] PR-1709 Add option to make title level request --- src/main/java/org/folio/ncip/Constants.java | 1 + .../folio/ncip/FolioRemoteServiceManager.java | 36 ++++--- .../services/FolioRequestItemService.java | 11 ++- src/test/java/org/folio/ncip/MockServer.java | 7 ++ src/test/java/org/folio/ncip/RequestItem.java | 13 +++ .../mockdata/instancesByHrid-get.json | 94 +++++++++++++++++++ .../mockdata/ncip-requestitem-title.xml | 28 ++++++ .../resources/mockdata/ncip-requestitem.xml | 4 +- 8 files changed, 176 insertions(+), 18 deletions(-) create mode 100644 src/test/resources/mockdata/instancesByHrid-get.json create mode 100644 src/test/resources/mockdata/ncip-requestitem-title.xml diff --git a/src/main/java/org/folio/ncip/Constants.java b/src/main/java/org/folio/ncip/Constants.java index ddf7dc8..292010c 100644 --- a/src/main/java/org/folio/ncip/Constants.java +++ b/src/main/java/org/folio/ncip/Constants.java @@ -82,6 +82,7 @@ public class Constants { public static final String HOLDINGS_URL = "/holdings-storage/holdings"; public static final String ITEM_URL = "/inventory/items"; public static final String ITEM_SEARCH_URL = "/inventory/items?limit=1&query=hrid%3D%3D%22$hrid$%22"; + public static final String INSTANCE_SEARCH_URL = "/inventory/instances?limit=1&query=hrid%3D%3D%22$hrid$%22"; public static final String REQUEST_URL = "/circulation/requests"; public static final String ADDRESS_TYPES = "/addresstypes"; diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index c6568a6..c46ded1 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -552,29 +552,35 @@ public JsonObject acceptItem(AcceptItemInitiationData initData, UserId userId, S return returnValues; } - public JsonObject requestItem(String hrid, UserId userId) throws Exception { + public JsonObject requestItem(String hrid, UserId userId, boolean titleRequest, String requestType) throws Exception { JsonObject returnValues = new JsonObject(); try { String baseUrl = okapiHeaders.get(Constants.X_OKAPI_URL); - String itemSearchUrl = baseUrl + Constants.ITEM_SEARCH_URL.replace("$hrid$", hrid); - String itemResponseString = callApiGet(itemSearchUrl); - JsonObject itemResponse = new JsonObject(itemResponseString); + String searchUrl = baseUrl + (titleRequest ? Constants.INSTANCE_SEARCH_URL : Constants.ITEM_SEARCH_URL) + .replace("$hrid$", hrid); + String responseString = callApiGet(searchUrl); + JsonObject response = new JsonObject(responseString); - Integer totalRecords = itemResponse.getInteger("totalRecords"); + Integer totalRecords = response.getInteger("totalRecords"); if (totalRecords == 1) { - JsonObject itemObject = itemResponse.getJsonArray("items").getJsonObject(0); - String holdingsUrl = baseUrl + Constants.HOLDINGS_URL + "/" + itemObject.getString("holdingsRecordId"); - String holdingResponseString = callApiGet(holdingsUrl); - JsonObject holdingResponse = new JsonObject(holdingResponseString); - JsonObject request = new JsonObject(); - request.put("requestType", "Page"); + if (titleRequest) { + JsonObject instanceObject = response.getJsonArray("instances").getJsonObject(0); + request.put("instanceId", instanceObject.getString("id")); + request.put("requestLevel", "Title"); + } else { + JsonObject itemObject = response.getJsonArray("items").getJsonObject(0); + String holdingsUrl = baseUrl + Constants.HOLDINGS_URL + "/" + itemObject.getString("holdingsRecordId"); + String holdingResponseString = callApiGet(holdingsUrl); + JsonObject holdingResponse = new JsonObject(holdingResponseString); + request.put("itemId", itemObject.getString("id")); + request.put("instanceId", holdingResponse.getString("instanceId")); + request.put("holdingsRecordId", holdingResponse.getString("id")); + request.put("requestLevel", "Item"); + } + request.put("requestType", requestType); request.put("fulfillmentPreference", "Delivery"); request.put("requesterId", userId.getUserIdentifierValue()); - request.put("itemId", itemObject.getString("id")); - request.put("instanceId", holdingResponse.getString("instanceId")); - request.put("requestLevel", "Item"); - request.put("holdingsRecordId", holdingResponse.getString("id")); request.put("requestDate", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now())); String requestUrl = baseUrl + Constants.REQUEST_URL; diff --git a/src/main/java/org/folio/ncip/services/FolioRequestItemService.java b/src/main/java/org/folio/ncip/services/FolioRequestItemService.java index 1707a66..3f3c8ff 100644 --- a/src/main/java/org/folio/ncip/services/FolioRequestItemService.java +++ b/src/main/java/org/folio/ncip/services/FolioRequestItemService.java @@ -28,11 +28,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; public class FolioRequestItemService extends FolioNcipService implements RequestItemService { private static final Logger logger = Logger.getLogger(FolioRequestItemService.class); + private static final Map REQUEST_TYPE = Map.of("page", "Page", + "hold", "Hold", + "recall", "Recall"); + @Override public RequestItemResponseData performService(RequestItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) { @@ -45,6 +50,7 @@ public RequestItemResponseData performService(RequestItemInitiationData initData try { validateUserId(userId); validateBibliographicIdIsPresent(bibliographicId); + initData.getRequestType().getValue(); } catch(Exception exception) { logger.error("Failed validating userId and itemId. " + exception.getLocalizedMessage()); @@ -52,13 +58,16 @@ public RequestItemResponseData performService(RequestItemInitiationData initData exception.getMessage(),exception.getMessage())); } + + final boolean titleRequest = initData.getRequestScopeType() != null && initData.getRequestScopeType().getValue().toLowerCase().contains("title"); + final String requestType = REQUEST_TYPE.getOrDefault(initData.getRequestType().getValue().toLowerCase(), "Page"); ItemId itemId = new ItemId(); RequestId ncipRequestId = new RequestId(); ItemDescription itemDescription = new ItemDescription(); LocationNameInstance locationNameInstance = new LocationNameInstance(); try { JsonObject requestItemResponseDetails = ((FolioRemoteServiceManager)serviceManager) - .requestItem(bibliographicId.getBibliographicRecordId().getBibliographicRecordIdentifier(), userId); + .requestItem(bibliographicId.getBibliographicRecordId().getBibliographicRecordIdentifier(), userId, titleRequest, requestType); String assignedRequestId = requestItemResponseDetails.getString("id"); String barcode = null; String callNumber = null; diff --git a/src/test/java/org/folio/ncip/MockServer.java b/src/test/java/org/folio/ncip/MockServer.java index 15238c1..b729135 100644 --- a/src/test/java/org/folio/ncip/MockServer.java +++ b/src/test/java/org/folio/ncip/MockServer.java @@ -104,6 +104,7 @@ private Router defineRoutes() { router.get("/configurations/entries/toolkit").handler(this::getToolkitCofigs); router.get("/configurations/entries").handler(this::getNcipConfigs); router.get("/inventory/items").handler(this::items); + router.get("/inventory/instances").handler(this::instances); router.get("/holdings-storage/holdings/:id").handler(this::holdingsById); router.post("/circulation/requests").handler(this::requestsPost); @@ -175,6 +176,12 @@ private void items(RoutingContext ctx) { serverResponse(ctx,200,APPLICATION_JSON,body); } + private void instances(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instancesByHrid-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx,200,APPLICATION_JSON,body); + } + private void holdingsById(RoutingContext ctx) { String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "holdingsById-get.json"; String body = readLineByLine(mockFileName); diff --git a/src/test/java/org/folio/ncip/RequestItem.java b/src/test/java/org/folio/ncip/RequestItem.java index f7ca38e..6a85abf 100644 --- a/src/test/java/org/folio/ncip/RequestItem.java +++ b/src/test/java/org/folio/ncip/RequestItem.java @@ -43,4 +43,17 @@ public void callRequestItem() throws MalformedURLException { assertTrue(body.contains(LOCATION)); } + + + @Test + public void callRequestItemTitle() throws MalformedURLException { + Response response = postData("src/test/resources/mockdata/ncip-requestitem-title.xml"); + String body = response.getBody().prettyPrint(); + System.out.println(body); + assertEquals(200, response.getStatusCode()); + assertTrue(body.contains(CREATED_REQUEST_ID)); + assertTrue(body.contains(ITEM_IDENTIFIER)); + assertTrue(body.contains(CALL_NUMBER)); + assertTrue(body.contains(LOCATION)); + } } diff --git a/src/test/resources/mockdata/instancesByHrid-get.json b/src/test/resources/mockdata/instancesByHrid-get.json new file mode 100644 index 0000000..7573e7f --- /dev/null +++ b/src/test/resources/mockdata/instancesByHrid-get.json @@ -0,0 +1,94 @@ +{ + "instances": [ + { + "id": "5b1eb450-ff9f-412d-a9e7-887f6eaeb5b4", + "_version": "1", + "hrid": "inst000000000010", + "source": "FOLIO", + "title": "Concepts of fashion 1921 - 1987 microform a study of garments worn by selected winners of the Miss America Pageant Marian Ann J. Matwiejczyk-Montgomery", + "administrativeNotes": [], + "parentInstances": [], + "childInstances": [], + "isBoundWith": false, + "alternativeTitles": [], + "editions": [], + "series": [], + "identifiers": [ + { + "identifierTypeId": "5d164f4b-0b15-4e42-ae75-cfcf85318ad9", + "value": "1008673218" + }, + { + "identifierTypeId": "7e591197-f335-4afb-bc6d-a6d76ca3bace", + "value": "(DE-599)GBV1008673218" + } + ], + "contributors": [ + { + "authorityId": null, + "contributorNameTypeId": "2b94c631-fca9-4892-a730-03ee529ffe2a", + "name": "Matwiejczyk-Montgomery, Marian Ann J", + "contributorTypeId": null, + "contributorTypeText": null, + "primary": null + } + ], + "subjects": [ + { + "authorityId": null, + "value": "Hochschulschrift" + } + ], + "classifications": [], + "publication": [ + { + "publisher": "University Microfims International", + "place": "Ann Arbor, MI", + "dateOfPublication": "1993", + "role": null + } + ], + "publicationFrequency": [], + "publicationRange": [], + "electronicAccess": [], + "instanceTypeId": "6312d172-f0cf-40f6-b27d-9fa8feaf332f", + "instanceFormatIds": [], + "physicalDescriptions": [], + "languages": [ + "eng" + ], + "notes": [ + { + "instanceNoteTypeId": null, + "note": "Dissertation New York University 1993", + "staffOnly": false + }, + { + "instanceNoteTypeId": "6a2533a7-4de2-4e64-8466-074c2fa9308c", + "note": "Mikrofiche-Ausgabe", + "staffOnly": false + } + ], + "previouslyHeld": false, + "discoverySuppress": false, + "statisticalCodeIds": [], + "statusUpdatedDate": "2024-05-16T14:06:53.398+0000", + "metadata": { + "createdDate": "2024-05-16T14:06:53.398+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:53.398+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + }, + "tags": { + "tagList": [] + }, + "natureOfContentTermIds": [], + "publicationPeriod": { + "start": 1993 + }, + "precedingTitles": [], + "succeedingTitles": [] + } + ], + "totalRecords": 1 +} \ No newline at end of file diff --git a/src/test/resources/mockdata/ncip-requestitem-title.xml b/src/test/resources/mockdata/ncip-requestitem-title.xml new file mode 100644 index 0000000..32b71c7 --- /dev/null +++ b/src/test/resources/mockdata/ncip-requestitem-title.xml @@ -0,0 +1,28 @@ + + + + + Relais + + + Lehigh University + + + + 8377630 + + + requestId1 + + + item1 + + Hold + Title + + + instanceHrid1 + + + + diff --git a/src/test/resources/mockdata/ncip-requestitem.xml b/src/test/resources/mockdata/ncip-requestitem.xml index 4cebee5..9bd245b 100644 --- a/src/test/resources/mockdata/ncip-requestitem.xml +++ b/src/test/resources/mockdata/ncip-requestitem.xml @@ -17,8 +17,8 @@ item1 - Loan - Bibliographic Item + Page + Item itemHrid1