Skip to content

Commit

Permalink
MODNCIP-68 Add option to specify item location in RequestItem call
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Sep 10, 2024
1 parent c21c095 commit 2ca42c3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
38 changes: 25 additions & 13 deletions src/main/java/org/folio/ncip/FolioRemoteServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.extensiblecatalog.ncip.v2.service.FiscalTransactionInformation;
import org.extensiblecatalog.ncip.v2.service.Location;
import org.extensiblecatalog.ncip.v2.service.RemoteServiceManager;
import org.extensiblecatalog.ncip.v2.service.RequestId;
import org.extensiblecatalog.ncip.v2.service.RequestItemInitiationData;
Expand Down Expand Up @@ -590,7 +591,7 @@ public JsonObject acceptItem(AcceptItemInitiationData initData, UserId userId, S
.put("holdings", new JsonObject(holdingsResponse));

addDefaultPatronFee(initData.getFiscalTransactionInformation(), user.getString(Constants.ID), user.getString(Constants.PATRON_GROUP), baseUrl);
addNoteIfNeeded(requesterAgencyId, returnValues.getString(Constants.ID), initData.getRequestId().getRequestIdentifierValue(), baseUrl, false);
addNoteIfNeeded(requesterAgencyId, returnValues.getString(Constants.ID), initData.getRequestId().getRequestIdentifierValue(), baseUrl);
} catch (Exception e) {
// IF ANY OF THE ABOVE FAILED - ATTEMPT TO DELETE THE INSTANCE, HOLDINGS ITEM
// THAT MAY HAVE BEEN CREATED ALONG THE WAY
Expand Down Expand Up @@ -688,21 +689,33 @@ private String getServicePointId(String pickUpLocationCode, String baseUrl) thro
return servicePoints.getJsonArray("servicepoints").getJsonObject(0).getString(Constants.ID);
}
public JsonObject requestItem(RequestItemInitiationData initData) throws Exception {
String baseUrl = okapiHeaders.get(Constants.X_OKAPI_URL);
String agencyId = initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue();
agencyId = agencyId == null ? agencyId : agencyId.toLowerCase();
initProperties(agencyId, baseUrl);
String hrid = initData.getBibliographicId(0).getBibliographicRecordId().getBibliographicRecordIdentifier();
final boolean titleRequest = initData.getRequestScopeType() != null && initData.getRequestScopeType().getValue().toLowerCase().contains("title");
final String requestType = REQUEST_TYPE.getOrDefault(initData.getRequestType().getValue().toLowerCase(), "Page");
String pickUpLocationCode = null;
if (initData.getPickupLocation() != null && StringUtils.isNotBlank(initData.getPickupLocation().getValue())) {
pickUpLocationCode = initData.getPickupLocation().getValue();

}
String locationCode = null;
if (initData.getItemOptionalFields() != null && initData.getItemOptionalFields().getLocations() != null &&
!initData.getItemOptionalFields().getLocations().isEmpty()) {
Location location = initData.getItemOptionalFields().getLocation(0);
if (location.getLocationName() != null && location.getLocationName().getLocationNameInstances() != null &&
!location.getLocationName().getLocationNameInstances().isEmpty()) {
locationCode = location.getLocationName().getLocationNameInstance(0).getLocationNameValue();
}
}

JsonObject returnValues = new JsonObject();
JsonObject user = lookupPatronRecord(initData.getUserId());
if (user == null)
throw new FolioNcipException(Constants.USER_NOT_FOUND);
try {
String baseUrl = okapiHeaders.get(Constants.X_OKAPI_URL);

String searchUrl = baseUrl + (titleRequest ? Constants.INSTANCE_SEARCH_URL : Constants.ITEM_SEARCH_URL)
.replace("$hrid$", hrid);
String responseString = callApiGet(searchUrl);
Expand Down Expand Up @@ -730,17 +743,21 @@ public JsonObject requestItem(RequestItemInitiationData initData) throws Excepti
request.put("fulfillmentPreference", "Delivery");
request.put("requesterId", user.getString(Constants.ID));
request.put("requestDate", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
String servicePointId;
if (pickUpLocationCode != null) {
String servicePointId = getServicePointId(pickUpLocationCode, baseUrl);
request.put("pickupServicePointId", servicePointId);
servicePointId = getServicePointId(pickUpLocationCode, baseUrl);
} else {
servicePointId = ncipProperties.getProperty(agencyId + ".checkout.service.point.id");
}
request.put("pickupServicePointId", servicePointId);
request.put("itemLocationCode", locationCode);

String requestUrl = baseUrl + Constants.REQUEST_URL;
String requestResponse = callApiPost(requestUrl, request);

returnValues.mergeIn(new JsonObject(requestResponse));
addNoteIfNeeded(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue(),
returnValues.getString(Constants.ID), initData.getRequestId().getRequestIdentifierValue(), baseUrl, true);
addNoteIfNeeded(agencyId, returnValues.getString(Constants.ID),
initData.getRequestId().getRequestIdentifierValue(), baseUrl);
} else {
logger.error("Found total of " + totalRecords + " items by hrid " + hrid);
throw new FolioNcipException(Constants.REQUEST_ITEM_MISSING_PROBLEM);
Expand All @@ -753,13 +770,8 @@ public JsonObject requestItem(RequestItemInitiationData initData) throws Excepti
return returnValues;
}

private void addNoteIfNeeded(String agencyId, String requestUuid, String illRequestId, String baseUrl, boolean initProperties) {
private void addNoteIfNeeded(String agencyId, String requestUuid, String illRequestId, String baseUrl) {
try {
agencyId = agencyId == null ? agencyId : agencyId.toLowerCase();
if (initProperties) {
initProperties(agencyId, baseUrl);
}

String noteEnabled = ncipProperties.getProperty(agencyId + ".request.note.enabled");
if (Constants.BOOLEAN_TRUE.equalsIgnoreCase(noteEnabled)) {
JsonObject note = new JsonObject();
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/org/folio/ncip/RequestItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public void callRequestItem() throws MalformedURLException {
assertTrue(body.contains(REQUESTER_ID));
}



@Test
public void callRequestItemTitle() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-requestitem-title.xml");
Expand All @@ -59,4 +57,17 @@ public void callRequestItemTitle() throws MalformedURLException {
assertTrue(body.contains(LOCATION));
assertTrue(body.contains(REQUESTER_ID));
}

@Test
public void callRequestItemTitleLocation() throws MalformedURLException {
Response response = postData("src/test/resources/mockdata/ncip-requestitem-titleLocation.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));
assertTrue(body.contains(REQUESTER_ID));
}
}
39 changes: 39 additions & 0 deletions src/test/resources/mockdata/ncip-requestitem-titleLocation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<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>
<ItemOptionalFields>
<Location>
<LocationType Scheme="Scheme">Item</LocationType>
<LocationName>
<LocationNameInstance>
<LocationNameLevel>3</LocationNameLevel>
<LocationNameValue>Datalogisk Institut</LocationNameValue>
</LocationNameInstance>
</LocationName>
</Location>
</ItemOptionalFields>
</RequestItem>
</NCIPMessage>

0 comments on commit 2ca42c3

Please sign in to comment.