use of org.folio.inventory.common.Context in project mod-inventory by folio-org.
the class PrecedingSucceedingTitlesHelper method deletePrecedingSucceedingTitles.
public Future<Void> deletePrecedingSucceedingTitles(Set<String> titlesIds, Context context) {
CollectionResourceClient precedingSucceedingTitlesClient = createPrecedingSucceedingTitlesClient(context);
CollectionResourceRepository precedingSucceedingTitlesRepository = new CollectionResourceRepository(precedingSucceedingTitlesClient);
titlesIds.forEach(id -> precedingSucceedingTitlesRepository.delete(id).whenComplete((v, e) -> {
if (e != null) {
LOGGER.error("Error during deleting PrecedingSucceedingTitles with ids {}", id, e);
LOGGER.info("Error during deleting PrecedingSucceedingTitles retry delete PrecedingSucceedingTitles");
precedingSucceedingTitlesRepository.delete(id);
}
}));
return Future.succeededFuture();
}
use of org.folio.inventory.common.Context in project mod-inventory by folio-org.
the class PrecedingSucceedingTitlesHelper method createPrecedingSucceedingTitles.
public Future<Void> createPrecedingSucceedingTitles(Instance instance, Context context) {
CollectionResourceClient precedingSucceedingTitlesClient = createPrecedingSucceedingTitlesClient(context);
CollectionResourceRepository precedingSucceedingTitlesRepository = new CollectionResourceRepository(precedingSucceedingTitlesClient);
List<PrecedingSucceedingTitle> precedingSucceedingTitles = new ArrayList<>();
preparePrecedingTitles(instance, precedingSucceedingTitles);
prepareSucceedingTitles(instance, precedingSucceedingTitles);
precedingSucceedingTitles.forEach(title -> precedingSucceedingTitlesRepository.post(title).whenComplete((v, e) -> {
if (e != null) {
LOGGER.error("Error during creating PrecedingSucceedingTitle for instance {}", instance.getId(), e);
LOGGER.info("Error during creating PrecedingSucceedingTitles retry creating new PrecedingSucceedingTitles");
precedingSucceedingTitlesRepository.post(title);
}
}));
return Future.succeededFuture();
}
use of org.folio.inventory.common.Context in project mod-inventory by folio-org.
the class UpdateItemEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
dataImportEventPayload.setEventType(DI_INVENTORY_ITEM_UPDATED.value());
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (isNull(payloadContext) || isBlank(payloadContext.get(MARC_BIBLIOGRAPHIC.value())) || isBlank(payloadContext.get(ITEM.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));
}
LOG.info("Processing UpdateItemEventHandler starting with jobExecutionId: {}.", dataImportEventPayload.getJobExecutionId());
AtomicBoolean isProtectedStatusChanged = new AtomicBoolean();
Context context = EventHandlingUtil.constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
String jobExecutionId = dataImportEventPayload.getJobExecutionId();
String recordId = dataImportEventPayload.getContext().get(RECORD_ID_HEADER);
String chunkId = dataImportEventPayload.getContext().get(CHUNK_ID_HEADER);
mappingMetadataCache.get(jobExecutionId, context).map(parametersOptional -> parametersOptional.orElseThrow(() -> new EventProcessingException(format(MAPPING_METADATA_NOT_FOUND_MSG, jobExecutionId, recordId, chunkId)))).compose(mappingMetadataDto -> {
String oldItemStatus = preparePayloadAndGetStatus(dataImportEventPayload, payloadContext, mappingMetadataDto);
JsonObject mappedItemAsJson = new JsonObject(payloadContext.get(ITEM.value()));
mappedItemAsJson = mappedItemAsJson.containsKey(ITEM_PATH_FIELD) ? mappedItemAsJson.getJsonObject(ITEM_PATH_FIELD) : mappedItemAsJson;
List<String> errors = validateItem(mappedItemAsJson, requiredFields);
if (!errors.isEmpty()) {
String msg = format("Mapped Instance is invalid: %s, by jobExecutionId: '%s' and recordId: '%s' and chunkId: '%s' ", errors, jobExecutionId, recordId, chunkId);
LOG.error(msg);
return Future.failedFuture(msg);
}
String newItemStatus = mappedItemAsJson.getJsonObject(STATUS_KEY).getString("name");
isProtectedStatusChanged.set(isProtectedStatusChanged(oldItemStatus, newItemStatus));
if (isProtectedStatusChanged.get()) {
mappedItemAsJson.getJsonObject(STATUS_KEY).put("name", oldItemStatus);
}
ItemCollection itemCollection = storage.getItemCollection(context);
Item itemToUpdate = ItemUtil.jsonToItem(mappedItemAsJson);
return verifyItemBarcodeUniqueness(itemToUpdate, itemCollection).compose(v -> updateItemAndRetryIfOLExists(itemToUpdate, itemCollection, dataImportEventPayload)).onSuccess(updatedItem -> {
if (isProtectedStatusChanged.get()) {
String msg = String.format(STATUS_UPDATE_ERROR_MSG, oldItemStatus, newItemStatus);
LOG.warn(msg);
dataImportEventPayload.getContext().put(ITEM.value(), ItemUtil.mapToJson(updatedItem).encode());
future.completeExceptionally(new EventProcessingException(msg));
} else {
addHoldingToPayloadIfNeeded(dataImportEventPayload, context, updatedItem).onComplete(item -> {
dataImportEventPayload.getContext().put(ITEM.value(), ItemUtil.mapToJson(updatedItem).encode());
future.complete(dataImportEventPayload);
});
}
});
}).onFailure(e -> {
LOG.error("Failed to update inventory Item by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, e);
future.completeExceptionally(e);
});
} catch (Exception e) {
LOG.error("Error updating inventory Item", e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.inventory.common.Context in project mod-inventory by folio-org.
the class AuthorityUpdateDelegate method handle.
public Future<Authority> handle(Map<String, String> eventPayload, Record marcRecord, Context context) {
try {
JsonObject mappingRules = new JsonObject(eventPayload.get(MAPPING_RULES_KEY));
MappingParameters mappingParameters = new JsonObject(eventPayload.get(MAPPING_PARAMS_KEY)).mapTo(MappingParameters.class);
JsonObject parsedRecord = retrieveParsedContent(marcRecord.getParsedRecord());
String authorityId = marcRecord.getExternalIdsHolder().getAuthorityId();
RecordMapper<Authority> recordMapper = RecordMapperBuilder.buildMapper(MARC_FORMAT);
var mappedAuthority = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
AuthorityRecordCollection authorityRecordCollection = storage.getAuthorityRecordCollection(context);
return getAuthorityRecordById(authorityId, authorityRecordCollection).onSuccess(existingAuthorityRecord -> fillVersion(existingAuthorityRecord, eventPayload)).compose(existingAuthorityRecord -> mergeRecords(existingAuthorityRecord, mappedAuthority)).compose(updatedAuthorityRecord -> updateAuthorityRecord(updatedAuthorityRecord, authorityRecordCollection));
} catch (Exception e) {
LOGGER.error("Error updating Authority", e);
return Future.failedFuture(e);
}
}
use of org.folio.inventory.common.Context in project mod-inventory by folio-org.
the class UpdateHoldingsQuickMarcEventHandlerTest method shouldProcessEvent.
@Test
public void shouldProcessEvent() {
List<HoldingsType> holdings = new ArrayList<>();
holdings.add(new HoldingsType().withName("testingnote$a").withId("fe19bae4-da28-472b-be90-d442e2428eadx"));
MappingParameters mappingParameters = new MappingParameters();
mappingParameters.withHoldingsTypes(holdings);
HashMap<String, String> eventPayload = new HashMap<>();
eventPayload.put("RECORD_TYPE", "MARC_HOLDING");
eventPayload.put("MARC_HOLDING", record.encode());
eventPayload.put("MAPPING_RULES", mappingRules.encode());
eventPayload.put("MAPPING_PARAMS", Json.encode(mappingParameters));
eventPayload.put("RELATED_RECORD_VERSION", HOLDINGS_VERSION.toString());
Future<HoldingsRecord> future = updateHoldingsQuickMarcEventHandler.handle(eventPayload);
HoldingsRecord updatedHoldings = future.result();
Assert.assertNotNull(updatedHoldings);
Assert.assertEquals(HOLDINGS_ID, updatedHoldings.getId());
Assert.assertEquals(HOLDINGS_VERSION, updatedHoldings.getVersion());
Assert.assertNull(updatedHoldings.getHoldingsTypeId());
Assert.assertNotNull(updatedHoldings.getHoldingsStatements());
Assert.assertEquals(21, updatedHoldings.getHoldingsStatements().size());
Assert.assertNotNull(updatedHoldings.getNotes());
Assert.assertEquals("testingnote$a ; testingnote$u ; testingnote$3 ; testingnote$5 ; testingnote$6 ; testingnote$8", updatedHoldings.getNotes().get(0).getNote());
ArgumentCaptor<Context> argument = ArgumentCaptor.forClass(Context.class);
verify(holdingsUpdateDelegate).handle(any(), any(), argument.capture());
Assert.assertEquals("token", argument.getValue().getToken());
Assert.assertEquals("dummy", argument.getValue().getTenantId());
Assert.assertEquals("http://localhost", argument.getValue().getOkapiLocation());
}
Aggregations