use of org.folio.inventory.domain.HoldingsRecordCollection in project mod-inventory by folio-org.
the class CreateHoldingEventHandler method addHoldings.
private Future<HoldingsRecord> addHoldings(HoldingsRecord holdings, Context context) {
Promise<HoldingsRecord> promise = Promise.promise();
HoldingsRecordCollection holdingsRecordCollection = storage.getHoldingsRecordCollection(context);
holdingsRecordCollection.add(holdings, success -> promise.complete(success.getResult()), failure -> {
// for now there is a solution via error-message contains. It will be improved via another solution by https://issues.folio.org/browse/RMB-899.
if (isNotBlank(failure.getReason()) && failure.getReason().contains(UNIQUE_ID_ERROR_MESSAGE)) {
LOGGER.info("Duplicated event received by InstanceId: {}. Ignoring...", holdings.getId());
promise.fail(new DuplicateEventException(format("Duplicated event by Holding id: %s", holdings.getId())));
} else {
LOGGER.error(format("Error posting Holdings cause %s, status code %s", failure.getReason(), failure.getStatusCode()));
promise.fail(failure.getReason());
}
});
return promise.future();
}
use of org.folio.inventory.domain.HoldingsRecordCollection in project mod-inventory by folio-org.
the class HoldingsUpdateDelegate method handle.
public Future<HoldingsRecord> 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 holdingsId = marcRecord.getExternalIdsHolder().getHoldingsId();
RecordMapper<Holdings> recordMapper = RecordMapperBuilder.buildMapper(MARC_FORMAT);
var mappedHoldings = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
HoldingsRecordCollection holdingsRecordCollection = storage.getHoldingsRecordCollection(context);
return getHoldingsRecordById(holdingsId, holdingsRecordCollection).onSuccess(existingHoldingsRecord -> fillVersion(existingHoldingsRecord, eventPayload)).compose(existingHoldingsRecord -> mergeRecords(existingHoldingsRecord, mappedHoldings)).compose(updatedHoldingsRecord -> updateHoldingsRecord(updatedHoldingsRecord, holdingsRecordCollection));
} catch (Exception e) {
LOGGER.error("Error updating inventory holdings", e);
return Future.failedFuture(e);
}
}
use of org.folio.inventory.domain.HoldingsRecordCollection in project mod-inventory by folio-org.
the class UpdateHoldingEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
dataImportEventPayload.setEventType(DI_INVENTORY_HOLDING_UPDATED.value());
if (dataImportEventPayload.getContext() == null || isEmpty(dataImportEventPayload.getContext().get(HOLDINGS.value())) || isEmpty(dataImportEventPayload.getContext().get(MARC_BIBLIOGRAPHIC.value()))) {
throw new EventProcessingException(CONTEXT_EMPTY_ERROR_MESSAGE);
}
if (dataImportEventPayload.getCurrentNode().getChildSnapshotWrappers().isEmpty()) {
LOGGER.error(ACTION_HAS_NO_MAPPING_MSG);
return CompletableFuture.failedFuture(new EventProcessingException(ACTION_HAS_NO_MAPPING_MSG));
}
LOGGER.info("Processing UpdateHoldingEventHandler starting with jobExecutionId: {}.", dataImportEventPayload.getJobExecutionId());
HoldingsRecord tmpHoldingsRecord = retrieveHolding(dataImportEventPayload.getContext());
String holdingId = tmpHoldingsRecord.getId();
String hrid = tmpHoldingsRecord.getHrid();
String instanceId = tmpHoldingsRecord.getInstanceId();
String permanentLocationId = tmpHoldingsRecord.getPermanentLocationId();
if (StringUtils.isAnyBlank(hrid, instanceId, permanentLocationId, holdingId)) {
throw new EventProcessingException(EMPTY_REQUIRED_FIELDS_ERROR_MESSAGE);
}
Context context = 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_MESSAGE, jobExecutionId, recordId, chunkId)))).onSuccess(mappingMetadataDto -> {
prepareEvent(dataImportEventPayload);
MappingParameters mappingParameters = Json.decodeValue(mappingMetadataDto.getMappingParams(), MappingParameters.class);
MappingManager.map(dataImportEventPayload, new MappingContext().withMappingParameters(mappingParameters));
HoldingsRecordCollection holdingsRecords = storage.getHoldingsRecordCollection(context);
HoldingsRecord holding = retrieveHolding(dataImportEventPayload.getContext());
holdingsRecords.update(holding, holdingSuccess -> constructDataImportEventPayload(future, dataImportEventPayload, holding), failure -> {
if (failure.getStatusCode() == HttpStatus.SC_CONFLICT) {
processOLError(dataImportEventPayload, future, holdingsRecords, holding, failure);
} else {
dataImportEventPayload.getContext().remove(CURRENT_RETRY_NUMBER);
LOGGER.error(format(CANNOT_UPDATE_HOLDING_ERROR_MESSAGE, holding.getId(), jobExecutionId, recordId, chunkId, failure.getReason(), failure.getStatusCode()));
future.completeExceptionally(new EventProcessingException(format(UPDATE_HOLDING_ERROR_MESSAGE, jobExecutionId, recordId, chunkId)));
}
});
}).onFailure(e -> {
LOGGER.error("Error updating inventory Holdings by jobExecutionId: '{}'", jobExecutionId, e);
future.completeExceptionally(e);
});
} catch (Exception e) {
LOGGER.error("Failed to update Holdings", e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.inventory.domain.HoldingsRecordCollection in project mod-inventory by folio-org.
the class UpdateItemEventHandler method addHoldingToPayloadIfNeeded.
private Future<DataImportEventPayload> addHoldingToPayloadIfNeeded(DataImportEventPayload dataImportEventPayload, Context context, Item updatedItem) {
Promise<DataImportEventPayload> promise = Promise.promise();
if (StringUtils.isBlank(dataImportEventPayload.getContext().get(HOLDINGS.value()))) {
HoldingsRecordCollection holdingsRecordCollection = storage.getHoldingsRecordCollection(context);
holdingsRecordCollection.findById(updatedItem.getHoldingId(), success -> {
LOG.info("Successfully retrieved Holdings for the hotlink by id: {}", updatedItem.getHoldingId());
dataImportEventPayload.getContext().put(HOLDINGS.value(), Json.encodePrettily(success.getResult()));
promise.complete(dataImportEventPayload);
}, failure -> {
LOG.warn("Error retrieving Holdings for the hotlink by id {} cause {}, status code {}", updatedItem.getHoldingId(), failure.getReason(), failure.getStatusCode());
promise.complete(dataImportEventPayload);
});
} else {
LOG.debug("Holdings already exists in payload with for the hotlink with id {}", updatedItem.getHoldingId());
promise.complete(dataImportEventPayload);
}
return promise.future();
}
use of org.folio.inventory.domain.HoldingsRecordCollection in project mod-inventory by folio-org.
the class MoveApi method updateHoldings.
private void updateHoldings(RoutingContext routingContext, WebContext context, List<String> idsToUpdate, List<HoldingsRecord> holdingsToUpdate) {
HoldingsRecordCollection storageHoldingsRecordsCollection = storage.getHoldingsRecordCollection(context);
List<CompletableFuture<HoldingsRecord>> updateFutures = holdingsToUpdate.stream().map(storageHoldingsRecordsCollection::update).collect(Collectors.toList());
CompletableFuture.allOf(updateFutures.toArray(new CompletableFuture[0])).handle((vVoid, throwable) -> updateFutures.stream().filter(future -> !future.isCompletedExceptionally()).map(CompletableFuture::join).map(HoldingsRecord::getId).collect(toList())).thenAccept(updatedIds -> respond(routingContext, idsToUpdate, updatedIds));
}
Aggregations