use of org.folio.inventory.domain.HoldingCollection in project mod-inventory by folio-org.
the class MarcBibMatchedPostProcessingEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (isNull(payloadContext)) {
LOGGER.error(PAYLOAD_HAS_NO_DATA_MSG);
future.completeExceptionally(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
return future;
}
if (isBlank(payloadContext.get(MATCHED_MARC_BIB_KEY))) {
LOGGER.info(MATCHED_RECORD_NOT_EXISTS_MSG);
future.complete(dataImportEventPayload);
return future;
}
Record matchedRecord = new JsonObject(payloadContext.get(MATCHED_MARC_BIB_KEY)).mapTo(Record.class);
String instanceId = ParsedRecordUtil.getAdditionalSubfieldValue(matchedRecord.getParsedRecord(), ParsedRecordUtil.AdditionalSubfields.I);
Context context = EventHandlingUtil.constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
InstanceCollection instanceCollection = storage.getInstanceCollection(context);
HoldingCollection holdingCollection = storage.getHoldingCollection(context);
if (isBlank(instanceId)) {
future.complete(dataImportEventPayload);
return future;
}
instanceCollection.findById(instanceId).whenComplete((v, t) -> {
if (t == null && v != null) {
dataImportEventPayload.getContext().put(INSTANCE.value(), Json.encode(v));
try {
holdingCollection.findByCql(format("instanceId=%s", v.getId()), PagingParameters.defaults(), findResult -> {
if (findResult.getResult() != null && findResult.getResult().totalRecords == 1) {
dataImportEventPayload.getContext().put(HOLDINGS.value(), Json.encode(findResult.getResult().records.get(0)));
}
future.complete(dataImportEventPayload);
}, failure -> {
LOGGER.error(ERROR_HOLDING_MSG + format(". StatusCode: %s. Message: %s", failure.getStatusCode(), failure.getReason()));
future.complete(dataImportEventPayload);
});
} catch (UnsupportedEncodingException e) {
LOGGER.error(ERROR_HOLDING_MSG, e);
future.complete(dataImportEventPayload);
}
} else {
LOGGER.error(ERROR_INSTANCE_MSG, t);
future.complete(dataImportEventPayload);
}
});
} catch (Exception e) {
LOGGER.error(ERROR_INSTANCE_MSG, e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.inventory.domain.HoldingCollection in project mod-inventory by folio-org.
the class IngestMessageProcessor method processRecordsMessage.
private void processRecordsMessage(Message<JsonObject> message, final EventBus eventBus) {
final CollectAll<Item> allItems = new CollectAll<>();
final CollectAll<Instance> allInstances = new CollectAll<>();
final CollectAll<Holding> allHoldings = new CollectAll<>();
final MessagingContext context = new MessagingContext(message.headers());
final JsonObject body = message.body();
IngestMessages.completed(context.getJobId(), context).send(eventBus);
final List<JsonObject> records = JsonArrayHelper.toList(body.getJsonArray("records"));
final JsonObject materialTypes = body.getJsonObject("materialTypes");
final JsonObject loanTypes = body.getJsonObject("loanTypes");
final JsonObject locations = body.getJsonObject("locations");
final JsonObject instanceTypes = body.getJsonObject("instanceTypes");
final JsonObject identifierTypes = body.getJsonObject("identifierTypes");
final JsonObject contributorNameTypes = body.getJsonObject("contributorNameTypes");
final InstanceCollection instanceCollection = storage.getInstanceCollection(context);
final ItemCollection itemCollection = storage.getItemCollection(context);
final HoldingCollection holdingCollection = storage.getHoldingCollection(context);
records.stream().map(record -> {
List<JsonObject> identifiersJson = JsonArrayHelper.toList(record.getJsonArray("identifiers"));
List<Identifier> identifiers = identifiersJson.stream().map(identifier -> new Identifier(identifierTypes.getString("ISBN"), identifier.getString("value"))).collect(Collectors.toList());
List<JsonObject> contributorsJson = JsonArrayHelper.toList(record.getJsonArray("contributors"));
List<Contributor> contributors = contributorsJson.stream().map(contributor -> new Contributor(contributorNameTypes.getString("Personal name"), contributor.getString("name"), "", "", null)).collect(Collectors.toList());
if (contributors.isEmpty()) {
contributors.add(new Contributor(contributorNameTypes.getString("Personal name"), "Unknown contributor", "", "", null));
}
return new Instance(UUID.randomUUID().toString(), "3", null, "Local: MODS", record.getString(TITLE_PROPERTY), instanceTypes.getString("text")).setIdentifiers(identifiers).setContributors(contributors);
}).forEach(instance -> instanceCollection.add(instance, allInstances.receive(), failure -> log.error("Instance processing failed: " + failure.getReason())));
allInstances.collect(instances -> {
instances.stream().map(instance -> new Holding(UUID.randomUUID().toString(), instance.getId(), locations.getString("Main Library"))).forEach(holding -> holdingCollection.add(holding, allHoldings.receive(), failure -> log.error("Holding processing failed: " + failure.getReason())));
allHoldings.collect(holdings -> records.stream().map(record -> {
// Will fail if have multiple instances with exactly the same title
Optional<Instance> possibleInstance = instances.stream().filter(instance -> StringUtils.equals(instance.getTitle(), record.getString(TITLE_PROPERTY))).findFirst();
String instanceId = possibleInstance.map(Instance::getId).orElse(null);
Optional<Holding> possibleHolding = holdings.stream().filter(holding -> StringUtils.equals(instanceId, holding.instanceId)).findFirst();
String holdingId = possibleHolding.map(holding -> holding.id).orElse(null);
return new Item(null, null, holdingId, new Status(ItemStatusName.AVAILABLE), materialTypes.getString("Book") != null ? materialTypes.getString("Book") : materialTypes.getString("book"), loanTypes.getString("Can Circulate") != null ? loanTypes.getString("Can Circulate") : loanTypes.getString("Can circulate"), null).withBarcode(record.getString("barcode"));
}).forEach(item -> itemCollection.add(item, allItems.receive(), failure -> log.error("Item processing failed: " + failure.getReason()))));
});
allItems.collect(items -> IngestMessages.completed(context.getJobId(), context).send(eventBus));
}
Aggregations