use of org.folio.circulation.domain.InstanceRequestRelatedRecords in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method validateRequester.
private Result<Collection<Item>> validateRequester(Map<Item, RequestQueue> itemRequestQueueMap, InstanceRequestRelatedRecords requestPackage) {
if (!itemRequestQueueMap.isEmpty()) {
Collection<RequestQueue> requestQueues = itemRequestQueueMap.values();
String requesterId = requestPackage.getInstanceLevelRequest().getRequesterId().toString();
for (RequestQueue queue : requestQueues) {
final Optional<Request> matchingRequest = queue.getRequests().stream().filter(request -> request.isOpen() && Objects.equals(request.getUserId(), requesterId)).findFirst();
if (matchingRequest.isPresent()) {
Map<String, String> parameters = new HashMap<>();
parameters.put(REQUESTER_ID, requesterId);
parameters.put(ITEM_ID, matchingRequest.get().getItemId());
parameters.put("instanceId", requestPackage.getInstanceLevelRequest().getInstanceId().toString());
String message = "This requester already has an open request for an item of this instance";
return failedValidation(new ValidationError(message, parameters));
}
}
}
return of(itemRequestQueueMap::keySet);
}
use of org.folio.circulation.domain.InstanceRequestRelatedRecords in project mod-circulation by folio-org.
the class RequestByInstanceIdResourceTests method canGetOrderedAvailableItemsListWithoutMatchingLocations.
@Test
void canGetOrderedAvailableItemsListWithoutMatchingLocations() {
UUID bookMaterialTypeId = UUID.randomUUID();
UUID loanTypeId = UUID.randomUUID();
JsonObject location = getLocationWithServicePoints(emptySet(), null, null);
Item item1 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.randomUUID()).create()).withLocation(Location.from(location));
Item item2 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.randomUUID()).create()).withLocation(Location.from(location));
Item item3 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.randomUUID()).create()).withLocation(Location.from(location));
Item item4 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.randomUUID()).create()).withLocation(Location.from(location));
// order added is important so the test deliberately add items in a certain order
List<Item> items = new ArrayList<>();
items.add(item3);
items.add(item2);
items.add(item4);
items.add(item1);
InstanceRequestRelatedRecords records = new InstanceRequestRelatedRecords();
JsonObject requestJson = getJsonInstanceRequest(UUID.randomUUID());
Result<RequestByInstanceIdRequest> request = RequestByInstanceIdRequest.from(requestJson);
records.setUnsortedAvailableItems(items);
records.setInstanceLevelRequest(request.value());
Result<InstanceRequestRelatedRecords> rankResult = rankItemsByMatchingServicePoint(records);
final List<Item> orderedItems = rankResult.value().getCombinedSortedItemsList();
assertEquals(4, orderedItems.size());
assertEquals(item3.getItemId(), orderedItems.get(0).getItemId());
assertEquals(item2.getItemId(), orderedItems.get(1).getItemId());
assertEquals(item4.getItemId(), orderedItems.get(2).getItemId());
assertEquals(item1.getItemId(), orderedItems.get(3).getItemId());
}
use of org.folio.circulation.domain.InstanceRequestRelatedRecords in project mod-circulation by folio-org.
the class RequestByInstanceIdResourceTests method canTransformInstanceToItemRequests.
@Test
void canTransformInstanceToItemRequests() {
UUID loanTypeId = UUID.randomUUID();
RequestByInstanceIdRequest requestByInstanceIdRequest = RequestByInstanceIdRequest.from(getJsonInstanceRequest(null)).value();
List<Item> items = getItems(2, loanTypeId);
InstanceRequestRelatedRecords records = new InstanceRequestRelatedRecords();
records.setSortedAvailableItems(items);
records.setInstanceLevelRequest(requestByInstanceIdRequest);
final Result<List<JsonObject>> collectionResult = RequestByInstanceIdResource.instanceToItemRequests(records);
assertTrue(collectionResult.succeeded());
Collection<JsonObject> requestRepresentations = collectionResult.value();
assertEquals(6, requestRepresentations.size());
int i = 0;
int j = 0;
Item item = items.get(j);
for (JsonObject itemRequestJson : requestRepresentations) {
assertEquals(item.getItemId(), itemRequestJson.getString("itemId"));
if (i == 0)
assertEquals(RequestType.HOLD.getValue(), itemRequestJson.getString("requestType"));
if (i == 1)
assertEquals(RequestType.RECALL.getValue(), itemRequestJson.getString("requestType"));
if (i == 2)
assertEquals(RequestType.PAGE.getValue(), itemRequestJson.getString("requestType"));
i++;
if (i > 2) {
i = 0;
j++;
if (j < 2) {
item = items.get(j);
}
}
}
}
use of org.folio.circulation.domain.InstanceRequestRelatedRecords in project mod-circulation by folio-org.
the class RequestByInstanceIdResourceTests method canGetOrderedAvailableItemsList.
@Test
void canGetOrderedAvailableItemsList() {
UUID primaryServicePointId = servicePointsFixture.cd2().getId();
UUID secondaryServicePointId = UUID.randomUUID();
UUID institutionId = UUID.randomUUID();
HashSet<UUID> servicePoints1 = new HashSet<>();
servicePoints1.add(secondaryServicePointId);
JsonObject location1 = getLocationWithServicePoints(servicePoints1, secondaryServicePointId, institutionId);
// Matching item and servicePoints
HashSet<UUID> servicePoints2 = new HashSet<>();
servicePoints2.add(primaryServicePointId);
servicePoints2.add(secondaryServicePointId);
JsonObject location2 = getLocationWithServicePoints(servicePoints2, primaryServicePointId, institutionId);
JsonObject location3 = getLocationWithServicePoints(emptySet(), null, institutionId);
// Matching item and servicePoints
HashSet<UUID> servicePoints4 = new HashSet<>();
servicePoints4.add(primaryServicePointId);
JsonObject location4 = getLocationWithServicePoints(servicePoints4, primaryServicePointId, institutionId);
UUID bookMaterialTypeId = UUID.randomUUID();
UUID loanTypeId = UUID.randomUUID();
Item item1 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.fromString(location1.getString("id"))).create()).withLocation(Location.from(location1));
Item item2 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.fromString(location2.getString("id"))).create()).withLocation(Location.from(location2));
Item item3 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.fromString(location3.getString("id"))).create()).withLocation(Location.from(location3));
Item item4 = Item.from(ItemExamples.basedUponSmallAngryPlanet(bookMaterialTypeId, loanTypeId).withTemporaryLocation(UUID.fromString(location4.getString("id"))).create()).withLocation(Location.from(location4));
final ArrayList<Item> items = new ArrayList<>();
items.add(item2);
items.add(item3);
items.add(item4);
items.add(item1);
InstanceRequestRelatedRecords records = new InstanceRequestRelatedRecords();
JsonObject requestJson = getJsonInstanceRequest(primaryServicePointId);
Result<RequestByInstanceIdRequest> request = RequestByInstanceIdRequest.from(requestJson);
records.setUnsortedAvailableItems(items);
records.setInstanceLevelRequest(request.value());
Result<InstanceRequestRelatedRecords> rankResult = rankItemsByMatchingServicePoint(records);
final List<Item> orderedItems = rankResult.value().getCombinedSortedItemsList();
assertEquals(4, orderedItems.size());
assertEquals(item2.getItemId(), orderedItems.get(0).getItemId());
assertEquals(item4.getItemId(), orderedItems.get(1).getItemId());
assertEquals(item3.getItemId(), orderedItems.get(2).getItemId());
assertEquals(item1.getItemId(), orderedItems.get(3).getItemId());
}
use of org.folio.circulation.domain.InstanceRequestRelatedRecords in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method rankItemsByMatchingServicePoint.
public static Result<InstanceRequestRelatedRecords> rankItemsByMatchingServicePoint(InstanceRequestRelatedRecords records) {
final Collection<Item> unsortedAvailableItems = records.getUnsortedAvailableItems();
final UUID pickupServicePointId = records.getInstanceLevelRequest().getPickupServicePointId();
return of(() -> {
Map<Boolean, List<Item>> itemsPartitionedByLocationServedByPickupPoint = unsortedAvailableItems.stream().collect(Collectors.partitioningBy(i -> Optional.ofNullable(i.getLocation()).map(location -> location.homeLocationIsServedBy(pickupServicePointId)).orElse(false)));
final List<Item> rankedItems = new LinkedList<>();
// Compose the final list of Items with the matchingItems (items that has matching service pointID) on top.
rankedItems.addAll(itemsPartitionedByLocationServedByPickupPoint.get(true));
rankedItems.addAll(itemsPartitionedByLocationServedByPickupPoint.get(false));
records.setSortedAvailableItems(rankedItems);
return records;
});
}
Aggregations