use of org.folio.processing.mapping.mapper.MappingContext in project mod-inventory by folio-org.
the class CreateHoldingEventHandler 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_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));
}
Context context = constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
String jobExecutionId = dataImportEventPayload.getJobExecutionId();
String recordId = payloadContext.get(RECORD_ID_HEADER);
String chunkId = payloadContext.get(CHUNK_ID_HEADER);
Future<RecordToEntity> recordToHoldingsFuture = idStorageService.store(recordId, 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)))).map(mappingMetadataDto -> {
prepareEvent(dataImportEventPayload);
MappingParameters mappingParameters = Json.decodeValue(mappingMetadataDto.getMappingParams(), MappingParameters.class);
MappingManager.map(dataImportEventPayload, new MappingContext().withMappingParameters(mappingParameters));
JsonObject holdingAsJson = new JsonObject(payloadContext.get(HOLDINGS.value()));
if (holdingAsJson.getJsonObject(HOLDINGS_PATH_FIELD) != null) {
holdingAsJson = holdingAsJson.getJsonObject(HOLDINGS_PATH_FIELD);
}
holdingAsJson.put("id", holdingsId);
holdingAsJson.put("sourceId", FOLIO_SOURCE_ID);
fillInstanceIdIfNeeded(dataImportEventPayload, holdingAsJson);
checkIfPermanentLocationIdExists(holdingAsJson);
return Json.decodeValue(payloadContext.get(HOLDINGS.value()), HoldingsRecord.class);
}).compose(holdingToCreate -> addHoldings(holdingToCreate, context)).onSuccess(createdHoldings -> {
LOGGER.info("Created Holding record by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}'", jobExecutionId, recordId, chunkId);
payloadContext.put(HOLDINGS.value(), Json.encodePrettily(createdHoldings));
future.complete(dataImportEventPayload);
}).onFailure(e -> {
if (!(e instanceof DuplicateEventException)) {
LOGGER.error("Error creating inventory Holding record by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, e);
}
future.completeExceptionally(e);
});
}).onFailure(failure -> {
LOGGER.error("Error creating inventory recordId and holdingsId relationship by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, failure);
future.completeExceptionally(failure);
});
} catch (Exception e) {
LOGGER.error(CREATE_HOLDING_ERROR_MESSAGE, e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.processing.mapping.mapper.MappingContext 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.processing.mapping.mapper.MappingContext in project mod-source-record-manager by folio-org.
the class EdifactDiErrorPayloadBuilder method mapPayloadWithPopulatingInvoiceDetails.
private DataImportEventPayload mapPayloadWithPopulatingInvoiceDetails(DataImportEventPayload dataImportEventPayload) {
DataImportEventPayload mappedPayload = MappingManager.map(dataImportEventPayload, new MappingContext());
mappedPayload.setEventType(DI_ERROR.value());
JsonObject mappingResult = new JsonObject(mappedPayload.getContext().get(INVOICE.value()));
JsonObject invoiceJson = mappingResult.getJsonObject(INVOICE_FIELD);
JsonObject invoiceLineCollection = new JsonObject().put(INVOICE_LINES_FIELD, new JsonArray(invoiceJson.remove(INVOICE_LINES_FIELD).toString()));
mappedPayload.getContext().put(INVOICE_LINES_KEY, Json.encode(invoiceLineCollection));
mappedPayload.getContext().put(INVOICE.value(), invoiceJson.encode());
return mappedPayload;
}
use of org.folio.processing.mapping.mapper.MappingContext in project mod-invoice by folio-org.
the class CreateInvoiceEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
dataImportEventPayload.setEventType(DI_INVOICE_CREATED.value());
try {
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (payloadContext == null || isBlank(payloadContext.get(EDIFACT_INVOICE.value()))) {
logger.error(PAYLOAD_HAS_NO_DATA_MSG);
return CompletableFuture.failedFuture(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
}
Map<String, String> okapiHeaders = DataImportUtils.getOkapiHeaders(dataImportEventPayload);
CompletableFuture<Map<Integer, PoLine>> poLinesFuture = getAssociatedPoLines(dataImportEventPayload, okapiHeaders);
poLinesFuture.thenAccept(invLineNoToPoLine -> ensureAdditionalData(dataImportEventPayload, invLineNoToPoLine)).thenAccept(v -> prepareEventPayloadForMapping(dataImportEventPayload)).thenAccept(v -> MappingManager.map(dataImportEventPayload, new MappingContext())).thenAccept(v -> prepareMappingResult(dataImportEventPayload)).thenCompose(v -> saveInvoice(dataImportEventPayload, okapiHeaders)).thenApply(savedInvoice -> prepareInvoiceLinesToSave(savedInvoice.getId(), dataImportEventPayload, poLinesFuture.join())).thenCompose(preparedInvoiceLines -> saveInvoiceLines(preparedInvoiceLines, okapiHeaders)).whenComplete((savedInvoiceLines, throwable) -> {
makeLightweightReturnPayload(dataImportEventPayload);
if (throwable == null) {
List<InvoiceLine> invoiceLines = savedInvoiceLines.stream().map(Pair::getLeft).collect(Collectors.toList());
InvoiceLineCollection invoiceLineCollection = new InvoiceLineCollection().withInvoiceLines(invoiceLines).withTotalRecords(invoiceLines.size());
dataImportEventPayload.getContext().put(INVOICE_LINES_KEY, Json.encode(invoiceLineCollection));
Map<Integer, String> invoiceLinesErrors = prepareInvoiceLinesErrors(savedInvoiceLines);
if (!invoiceLinesErrors.isEmpty()) {
dataImportEventPayload.getContext().put(INVOICE_LINES_ERRORS_KEY, Json.encode(invoiceLinesErrors));
future.completeExceptionally(new EventProcessingException("Error during invoice lines creation"));
return;
}
future.complete(dataImportEventPayload);
} else {
preparePayloadWithMappedInvoiceLines(dataImportEventPayload);
logger.error("Error during invoice creation", throwable);
future.completeExceptionally(throwable);
}
});
} catch (Exception e) {
logger.error("Error during creation invoice and invoice lines", e);
future.completeExceptionally(e);
}
return future;
}
use of org.folio.processing.mapping.mapper.MappingContext in project mod-inventory by folio-org.
the class CreateItemEventHandler method handle.
@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
try {
dataImportEventPayload.setEventType(DI_INVENTORY_ITEM_CREATED.value());
HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
if (payloadContext == null || isBlank(payloadContext.get(EntityType.MARC_BIBLIOGRAPHIC.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));
}
dataImportEventPayload.getEventsChain().add(dataImportEventPayload.getEventType());
dataImportEventPayload.setCurrentNode(dataImportEventPayload.getCurrentNode().getChildSnapshotWrappers().get(0));
dataImportEventPayload.getContext().put(ITEM.value(), new JsonObject().encode());
String jobExecutionId = dataImportEventPayload.getJobExecutionId();
String recordId = dataImportEventPayload.getContext().get(RECORD_ID_HEADER);
String chunkId = dataImportEventPayload.getContext().get(CHUNK_ID_HEADER);
Future<RecordToEntity> recordToItemFuture = idStorageService.store(recordId, UUID.randomUUID().toString(), dataImportEventPayload.getTenant());
recordToItemFuture.onSuccess(res -> {
String itemId = res.getEntityId();
Context context = EventHandlingUtil.constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
ItemCollection itemCollection = storage.getItemCollection(context);
mappingMetadataCache.get(jobExecutionId, context).map(parametersOptional -> parametersOptional.orElseThrow(() -> new EventProcessingException(format(MAPPING_METADATA_NOT_FOUND_MSG, jobExecutionId, recordId, chunkId)))).map(mappingMetadataDto -> {
MappingParameters mappingParameters = Json.decodeValue(mappingMetadataDto.getMappingParams(), MappingParameters.class);
MappingManager.map(dataImportEventPayload, new MappingContext().withMappingParameters(mappingParameters));
return processMappingResult(dataImportEventPayload, itemId);
}).compose(mappedItemJson -> {
List<String> errors = validateItem(mappedItemJson, requiredFields);
if (!errors.isEmpty()) {
String msg = format("Mapped Item is invalid: %s, by jobExecutionId: '%s' and recordId: '%s' and chunkId: '%s' ", errors, jobExecutionId, recordId, chunkId);
LOG.error(msg);
return Future.failedFuture(msg);
}
Item mappedItem = ItemUtil.jsonToItem(mappedItemJson);
return isItemBarcodeUnique(mappedItemJson.getString("barcode"), itemCollection).compose(isUnique -> isUnique ? addItem(mappedItem, itemCollection) : Future.failedFuture(format("Barcode must be unique, %s is already assigned to another item", mappedItemJson.getString("barcode"))));
}).onComplete(ar -> {
if (ar.succeeded()) {
dataImportEventPayload.getContext().put(ITEM.value(), Json.encode(ar.result()));
future.complete(dataImportEventPayload);
} else {
if (!(ar.cause() instanceof DuplicateEventException)) {
LOG.error("Error creating inventory Item by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, ar.cause());
}
future.completeExceptionally(ar.cause());
}
});
}).onFailure(failure -> {
LOG.error("Error creating inventory recordId and itemId relationship by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, failure);
future.completeExceptionally(failure);
});
} catch (Exception e) {
LOG.error("Error creating inventory Item", e);
future.completeExceptionally(e);
}
return future;
}
Aggregations