use of org.folio.inventory.common.MessagingContext in project mod-inventory by folio-org.
the class IngestMessageProcessor method markIngestCompleted.
private void markIngestCompleted(Message<JsonObject> message) {
final MessagingContext context = new MessagingContext(message.headers());
storage.getIngestJobCollection(context).update(new IngestJob(context.getJobId(), IngestJobState.COMPLETED), v -> log.info(String.format("Ingest job %s completed", context.getJobId())), failure -> log.error(String.format("Updating ingest job failed: %s", failure.getReason())));
}
use of org.folio.inventory.common.MessagingContext 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