use of org.folio.HoldingsRecord 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.HoldingsRecord in project mod-inventory by folio-org.
the class CreateHoldingEventHandler method fillInstanceIdIfNeeded.
private void fillInstanceIdIfNeeded(DataImportEventPayload dataImportEventPayload, JsonObject holdingAsJson) {
if (isBlank(holdingAsJson.getString(INSTANCE_ID_FIELD))) {
String instanceId = null;
String instanceAsString = dataImportEventPayload.getContext().get(EntityType.INSTANCE.value());
if (isNotEmpty(instanceAsString)) {
JsonObject holdingsRecord = new JsonObject(instanceAsString);
instanceId = holdingsRecord.getString("id");
}
if (isBlank(instanceId)) {
String recordAsString = dataImportEventPayload.getContext().get(EntityType.MARC_BIBLIOGRAPHIC.value());
Record record = Json.decodeValue(recordAsString, Record.class);
instanceId = ParsedRecordUtil.getAdditionalSubfieldValue(record.getParsedRecord(), ParsedRecordUtil.AdditionalSubfields.I);
}
if (isBlank(instanceId)) {
throw new EventProcessingException(PAYLOAD_DATA_HAS_NO_INSTANCE_ID_ERROR_MSG);
}
fillInstanceId(dataImportEventPayload, holdingAsJson, instanceId);
}
}
use of org.folio.HoldingsRecord in project mod-inventory by folio-org.
the class CreateMarcHoldingsEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
dataImportEventPayload.setEventType(DI_INVENTORY_HOLDING_CREATED.value());
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (payloadContext == null || payloadContext.isEmpty() || StringUtils.isEmpty(payloadContext.get(MARC_HOLDINGS.value()))) {
return CompletableFuture.failedFuture(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));
}
Context context = constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
Record targetRecord = new JsonObject(payloadContext.get(EntityType.MARC_HOLDINGS.value())).mapTo(Record.class);
prepareEvent(dataImportEventPayload);
String jobExecutionId = dataImportEventPayload.getJobExecutionId();
String recordId = payloadContext.get(RECORD_ID_HEADER);
String chunkId = payloadContext.get(CHUNK_ID_HEADER);
Future<RecordToEntity> recordToHoldingsFuture = idStorageService.store(targetRecord.getId(), UUID.randomUUID().toString(), dataImportEventPayload.getTenant());
recordToHoldingsFuture.onSuccess(res -> {
String holdingsId = res.getEntityId();
mappingMetadataCache.get(jobExecutionId, context).map(parametersOptional -> parametersOptional.orElseThrow(() -> new EventProcessingException(format(MAPPING_METADATA_NOT_FOUND_MSG, jobExecutionId, recordId, chunkId)))).onSuccess(mappingMetadata -> defaultMapRecordToHoldings(dataImportEventPayload, mappingMetadata)).map(v -> processMappingResult(dataImportEventPayload, holdingsId)).compose(holdingJson -> findInstanceIdByHrid(dataImportEventPayload, holdingJson, context).compose(instanceId -> {
fillInstanceId(dataImportEventPayload, holdingJson, instanceId);
var holdingsRecords = storage.getHoldingsRecordCollection(context);
HoldingsRecord holding = Json.decodeValue(payloadContext.get(HOLDINGS.value()), HoldingsRecord.class);
return addHoldings(holding, holdingsRecords);
})).onSuccess(createdHoldings -> {
LOGGER.info("Created Holding record by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId);
dataImportEventPayload.getContext().put(HOLDINGS.value(), Json.encodePrettily(createdHoldings));
future.complete(dataImportEventPayload);
}).onFailure(e -> {
if (!(e instanceof DuplicateEventException)) {
LOGGER.error("Error creating Holding by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, e);
}
future.completeExceptionally(e);
});
}).onFailure(failure -> {
LOGGER.error(format(CREATING_INVENTORY_RELATIONSHIP_ERROR_MESSAGE, jobExecutionId, recordId, chunkId), failure);
future.completeExceptionally(failure);
});
} catch (Exception e) {
LOGGER.error("Failed to create Holdings", e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.HoldingsRecord 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.HoldingsRecord in project mod-inventory by folio-org.
the class HoldingsUpdateDelegate method mergeRecords.
private Future<HoldingsRecord> mergeRecords(HoldingsRecord existingRecord, Holdings mappedRecord) {
try {
mappedRecord.setId(existingRecord.getId());
JsonObject existing = JsonObject.mapFrom(existingRecord);
JsonObject mapped = JsonObject.mapFrom(mappedRecord);
JsonObject merged = existing.mergeIn(mapped);
HoldingsRecord mergedHoldingsRecord = merged.mapTo(HoldingsRecord.class);
return Future.succeededFuture(mergedHoldingsRecord);
} catch (Exception e) {
LOGGER.error("Error updating holdings", e);
return Future.failedFuture(e);
}
}
Aggregations