Skip to content

Commit

Permalink
Merge pull request #5 from indexdata/PR-1709-dev
Browse files Browse the repository at this point in the history
PR-1709 Add option to make title level request
  • Loading branch information
JanisSaldabols authored May 20, 2024
2 parents 823441c + 512370c commit 58df013
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/folio/ncip/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
36 changes: 21 additions & 15 deletions src/main/java/org/folio/ncip/FolioRemoteServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> REQUEST_TYPE = Map.of("page", "Page",
"hold", "Hold",
"recall", "Recall");

@Override
public RequestItemResponseData performService(RequestItemInitiationData initData, ServiceContext serviceContext,
RemoteServiceManager serviceManager) {
Expand All @@ -45,20 +50,24 @@ 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());
return setProblemInResponse(new Problem(new ProblemType(Constants.REQUEST_ITEM_PROBLEM), Constants.REQUEST_ITEM_INPUT_PROBLEM,
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;
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/folio/ncip/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/folio/ncip/RequestItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
94 changes: 94 additions & 0 deletions src/test/resources/mockdata/instancesByHrid-get.json
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions src/test/resources/mockdata/ncip-requestitem-title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<NCIPMessage version="http://www.niso.org/schemas/ncip/v2_02/ncip_v2_02.xsd">
<RequestItem>
<InitiationHeader>
<FromAgencyId>
<AgencyId>Relais</AgencyId>
</FromAgencyId>
<ToAgencyId>
<AgencyId>Lehigh University</AgencyId>
</ToAgencyId>
</InitiationHeader>
<UserId>
<UserIdentifierValue>8377630</UserIdentifierValue>
</UserId>
<RequestId>
<RequestIdentifierValue>requestId1</RequestIdentifierValue>
</RequestId>
<ItemId>
<ItemIdentifierValue>item1</ItemIdentifierValue>
</ItemId>
<RequestType>Hold</RequestType>
<RequestScopeType>Title</RequestScopeType>
<BibliographicId>
<BibliographicRecordId>
<BibliographicRecordIdentifier>instanceHrid1</BibliographicRecordIdentifier>
</BibliographicRecordId>
</BibliographicId>
</RequestItem>
</NCIPMessage>
4 changes: 2 additions & 2 deletions src/test/resources/mockdata/ncip-requestitem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<ItemId>
<ItemIdentifierValue>item1</ItemIdentifierValue>
</ItemId>
<RequestType>Loan</RequestType>
<RequestScopeType>Bibliographic Item</RequestScopeType>
<RequestType>Page</RequestType>
<RequestScopeType>Item</RequestScopeType>
<BibliographicId>
<BibliographicRecordId>
<BibliographicRecordIdentifier>itemHrid1</BibliographicRecordIdentifier>
Expand Down

0 comments on commit 58df013

Please sign in to comment.