use of org.folio.inventory.domain.items.ItemCollection in project mod-inventory by folio-org.
the class MoveApi method updateItems.
private void updateItems(RoutingContext routingContext, WebContext context, List<String> idsToUpdate, List<Item> itemsToUpdate) {
ItemCollection storageItemCollection = storage.getItemCollection(context);
List<CompletableFuture<Item>> updates = itemsToUpdate.stream().map(storageItemCollection::update).collect(Collectors.toList());
CompletableFuture.allOf(updates.toArray(new CompletableFuture[0])).handle((vVoid, throwable) -> updates.stream().filter(future -> !future.isCompletedExceptionally()).map(CompletableFuture::join).map(Item::getId).collect(toList())).thenAccept(updatedIds -> respond(routingContext, idsToUpdate, updatedIds));
}
use of org.folio.inventory.domain.items.ItemCollection in project mod-inventory by folio-org.
the class MatchItemEventHandlerUnitTest method shouldPutMultipleMatchResultToPayloadOnHandleEventPayload.
@Test
public void shouldPutMultipleMatchResultToPayloadOnHandleEventPayload(TestContext testContext) throws UnsupportedEncodingException {
Async async = testContext.async();
List<Item> matchedItems = List.of(new Item(ITEM_ID, "1", HOLDING_ID, new Status(ItemStatusName.AVAILABLE), UUID.randomUUID().toString(), UUID.randomUUID().toString(), new JsonObject()), new Item(UUID.randomUUID().toString(), "1", HOLDING_ID, new Status(ItemStatusName.AVAILABLE), UUID.randomUUID().toString(), UUID.randomUUID().toString(), new JsonObject()));
doAnswer(invocation -> {
Consumer<Success<MultipleRecords<Item>>> callback = invocation.getArgument(2);
Success<MultipleRecords<Item>> result = new Success<>(new MultipleRecords<>(matchedItems, 2));
callback.accept(result);
return null;
}).when(itemCollection).findByCql(eq(format("hrid == \"%s\"", ITEM_HRID)), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
EventHandler eventHandler = new MatchItemEventHandler(mappingMetadataCache);
HashMap<String, String> context = new HashMap<>();
context.put(MAPPING_PARAMS, LOCATIONS_PARAMS);
context.put(RELATIONS, MATCHING_RELATIONS);
DataImportEventPayload eventPayload = createEventPayload().withContext(context);
eventPayload.getCurrentNode().setChildSnapshotWrappers(List.of(new ProfileSnapshotWrapper().withContent(new MatchProfile().withExistingRecordType(ITEM).withIncomingRecordType(MARC_BIBLIOGRAPHIC)).withContentType(MATCH_PROFILE).withReactTo(MATCH)));
eventHandler.handle(eventPayload).whenComplete((processedPayload, throwable) -> testContext.verify(v -> {
testContext.assertNull(throwable);
testContext.assertEquals(1, processedPayload.getEventsChain().size());
testContext.assertEquals(DI_INVENTORY_ITEM_MATCHED.value(), processedPayload.getEventType());
assertThat(new JsonArray(processedPayload.getContext().get(MULTI_MATCH_IDS)), hasItems(matchedItems.get(0).getId(), matchedItems.get(1).getId()));
async.complete();
}));
}
use of org.folio.inventory.domain.items.ItemCollection in project mod-inventory by folio-org.
the class ExternalItemCollectionFailureExamples method serverErrorWhenGettingAnItemByIdTriggersFailureCallback.
@Test
public void serverErrorWhenGettingAnItemByIdTriggersFailureCallback() throws InterruptedException, ExecutionException, TimeoutException {
ItemCollection collection = createCollection();
CompletableFuture<Failure> failureCalled = new CompletableFuture<>();
collection.findById(UUID.randomUUID().toString(), success -> fail("Completion callback should not be called"), failureCalled::complete);
Failure failure = failureCalled.get(1000, TimeUnit.MILLISECONDS);
check(failure);
}
use of org.folio.inventory.domain.items.ItemCollection in project mod-inventory by folio-org.
the class Items method addItem.
private void addItem(RoutingContext routingContext, WebContext webContext, Item newItem, User user, ItemCollection itemCollection) {
List<CirculationNote> notes = newItem.getCirculationNotes().stream().map(note -> note.withId(UUID.randomUUID().toString())).map(note -> note.withSource(user)).map(note -> note.withDate(dateTimeFormatter.format(ZonedDateTime.now()))).collect(Collectors.toList());
itemCollection.add(newItem.withCirculationNotes(notes), success -> {
Item item = success.getResult();
respondWithItemRepresentation(item, STATUS_CREATED, routingContext, webContext);
}, FailureResponseConsumer.serverError(routingContext.response()));
}
use of org.folio.inventory.domain.items.ItemCollection in project mod-inventory by folio-org.
the class CreateItemEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
dataImportEventPayload.setEventType(DI_INVENTORY_ITEM_CREATED.value());
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (payloadContext == null || isBlank(payloadContext.get(EntityType.MARC_BIBLIOGRAPHIC.value()))) {
LOG.error(PAYLOAD_HAS_NO_DATA_MSG);
return CompletableFuture.failedFuture(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
}
if (dataImportEventPayload.getCurrentNode().getChildSnapshotWrappers().isEmpty()) {
LOG.error(ACTION_HAS_NO_MAPPING_MSG);
return CompletableFuture.failedFuture(new EventProcessingException(ACTION_HAS_NO_MAPPING_MSG));
}
dataImportEventPayload.getEventsChain().add(dataImportEventPayload.getEventType());
dataImportEventPayload.setCurrentNode(dataImportEventPayload.getCurrentNode().getChildSnapshotWrappers().get(0));
dataImportEventPayload.getContext().put(ITEM.value(), new JsonObject().encode());
String jobExecutionId = dataImportEventPayload.getJobExecutionId();
String recordId = dataImportEventPayload.getContext().get(RECORD_ID_HEADER);
String chunkId = dataImportEventPayload.getContext().get(CHUNK_ID_HEADER);
Future<RecordToEntity> recordToItemFuture = idStorageService.store(recordId, UUID.randomUUID().toString(), dataImportEventPayload.getTenant());
recordToItemFuture.onSuccess(res -> {
String itemId = res.getEntityId();
Context context = EventHandlingUtil.constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
ItemCollection itemCollection = storage.getItemCollection(context);
mappingMetadataCache.get(jobExecutionId, context).map(parametersOptional -> parametersOptional.orElseThrow(() -> new EventProcessingException(format(MAPPING_METADATA_NOT_FOUND_MSG, jobExecutionId, recordId, chunkId)))).map(mappingMetadataDto -> {
MappingParameters mappingParameters = Json.decodeValue(mappingMetadataDto.getMappingParams(), MappingParameters.class);
MappingManager.map(dataImportEventPayload, new MappingContext().withMappingParameters(mappingParameters));
return processMappingResult(dataImportEventPayload, itemId);
}).compose(mappedItemJson -> {
List<String> errors = validateItem(mappedItemJson, requiredFields);
if (!errors.isEmpty()) {
String msg = format("Mapped Item is invalid: %s, by jobExecutionId: '%s' and recordId: '%s' and chunkId: '%s' ", errors, jobExecutionId, recordId, chunkId);
LOG.error(msg);
return Future.failedFuture(msg);
}
Item mappedItem = ItemUtil.jsonToItem(mappedItemJson);
return isItemBarcodeUnique(mappedItemJson.getString("barcode"), itemCollection).compose(isUnique -> isUnique ? addItem(mappedItem, itemCollection) : Future.failedFuture(format("Barcode must be unique, %s is already assigned to another item", mappedItemJson.getString("barcode"))));
}).onComplete(ar -> {
if (ar.succeeded()) {
dataImportEventPayload.getContext().put(ITEM.value(), Json.encode(ar.result()));
future.complete(dataImportEventPayload);
} else {
if (!(ar.cause() instanceof DuplicateEventException)) {
LOG.error("Error creating inventory Item by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, ar.cause());
}
future.completeExceptionally(ar.cause());
}
});
}).onFailure(failure -> {
LOG.error("Error creating inventory recordId and itemId relationship by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, failure);
future.completeExceptionally(failure);
});
} catch (Exception e) {
LOG.error("Error creating inventory Item", e);
future.completeExceptionally(e);
}
return future;
}
Aggregations