From 06f2c2a2d056cdfc9721b4a8f434fbe155c64ca1 Mon Sep 17 00:00:00 2001 From: pbobylev Date: Mon, 10 Jun 2024 18:40:15 +0500 Subject: [PATCH] MODINV-986: minor hotfix --- .../InstanceIngressEventConsumer.java | 13 ++++++++++++- .../handler/CreateInstanceIngressEventHandler.java | 14 +++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/folio/inventory/instanceingress/InstanceIngressEventConsumer.java b/src/main/java/org/folio/inventory/instanceingress/InstanceIngressEventConsumer.java index 21231dc6c..6142cb908 100644 --- a/src/main/java/org/folio/inventory/instanceingress/InstanceIngressEventConsumer.java +++ b/src/main/java/org/folio/inventory/instanceingress/InstanceIngressEventConsumer.java @@ -3,6 +3,7 @@ import static org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.constructContext; import static org.folio.rest.jaxrs.model.InstanceIngressEvent.EventType.CREATE_INSTANCE; import static org.folio.rest.jaxrs.model.InstanceIngressEvent.EventType.UPDATE_INSTANCE; +import static org.folio.rest.util.OkapiConnectionParams.OKAPI_TENANT_HEADER; import static org.folio.rest.util.OkapiConnectionParams.OKAPI_TOKEN_HEADER; import static org.folio.rest.util.OkapiConnectionParams.OKAPI_URL_HEADER; @@ -13,6 +14,8 @@ import io.vertx.core.json.Json; import io.vertx.ext.web.client.WebClient; import io.vertx.kafka.client.consumer.KafkaConsumerRecord; +import java.util.Map; +import java.util.Optional; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.inventory.common.Context; @@ -28,6 +31,7 @@ import org.folio.kafka.AsyncRecordHandler; import org.folio.kafka.KafkaHeaderUtils; import org.folio.processing.exceptions.EventProcessingException; +import org.folio.rest.jaxrs.model.EventMetadata; import org.folio.rest.jaxrs.model.InstanceIngressEvent; public class InstanceIngressEventConsumer implements AsyncRecordHandler { @@ -52,7 +56,7 @@ public InstanceIngressEventConsumer(Vertx vertx, public Future handle(KafkaConsumerRecord consumerRecord) { var kafkaHeaders = KafkaHeaderUtils.kafkaHeadersToMap(consumerRecord.headers()); var event = Json.decodeValue(consumerRecord.value(), InstanceIngressEvent.class); - var context = constructContext(event.getEventMetadata().getTenantId(), + var context = constructContext(getTenantId(event, kafkaHeaders), kafkaHeaders.get(OKAPI_TOKEN_HEADER), kafkaHeaders.get(OKAPI_URL_HEADER)); LOGGER.info("Instance ingress event has been received with event type: {}", event.getEventType()); return Future.succeededFuture(event.getEventPayload()) @@ -63,6 +67,13 @@ public Future handle(KafkaConsumerRecord consumerRecord) }); } + private static String getTenantId(InstanceIngressEvent event, + Map kafkaHeaders) { + return Optional.ofNullable(event.getEventMetadata()) + .map(EventMetadata::getTenantId) + .orElseGet(() -> kafkaHeaders.get(OKAPI_TENANT_HEADER)); + } + private Future processEvent(InstanceIngressEvent event, Context context) { try { Promise promise = Promise.promise(); diff --git a/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java b/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java index 4ee681014..787a46924 100644 --- a/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java +++ b/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java @@ -14,7 +14,7 @@ import static org.folio.inventory.dataimport.util.MappingConstants.MARC_BIB_RECORD_TYPE; import static org.folio.rest.jaxrs.model.EntityType.MARC_BIBLIOGRAPHIC; import static org.folio.rest.jaxrs.model.Record.RecordType.MARC_BIB; -import static org.folio.rest.jaxrs.model.Snapshot.Status.PROCESSING_IN_PROGRESS; +import static org.folio.rest.jaxrs.model.Snapshot.Status.PROCESSING_FINISHED; import io.vertx.core.Future; import io.vertx.core.Promise; @@ -117,7 +117,7 @@ private Future prepareAndExecuteMapping(MappingMetadataDto m Record targetRecord, InstanceIngressEvent event, String instanceId) { - return postSnapshotInSrsAndHandleResponse(event.getId()) + return postSnapshotInSrsAndHandleResponse(targetRecord.getId()) .compose(snapshot -> { try { LOGGER.info("Manipulating fields of a Record from InstanceIngressEvent with id '{}'", event.getId()); @@ -125,9 +125,9 @@ private Future prepareAndExecuteMapping(MappingMetadataDto m AdditionalFieldsUtil.updateLatestTransactionDate(targetRecord, mappingParameters); AdditionalFieldsUtil.move001To035(targetRecord); AdditionalFieldsUtil.normalize035(targetRecord); - if (event.getEventPayload().getAdditionalProperties().containsKey( - LINKED_DATA_ID)) {AdditionalFieldsUtil.addFieldToMarcRecord(targetRecord, TAG_035, TAG_035_SUB, - LD + event.getEventPayload().getAdditionalProperties().get(LINKED_DATA_ID)); + if (event.getEventPayload().getAdditionalProperties().containsKey(LINKED_DATA_ID)) { + AdditionalFieldsUtil.addFieldToMarcRecord(targetRecord, TAG_035, TAG_035_SUB, + LD + event.getEventPayload().getAdditionalProperties().get(LINKED_DATA_ID)); } LOGGER.info("Mapping a Record from InstanceIngressEvent with id '{}' into an Instance", event.getId()); @@ -207,7 +207,7 @@ private Future saveInstance(Instance instance, InstanceIngressEvent ev private Future executeFieldsManipulation(Instance instance, Record srcRecord, Map eventProperties) { if (eventProperties.containsKey(LINKED_DATA_ID)) { - AdditionalFieldsUtil.addFieldToMarcRecord(srcRecord, TAG_999, SUBFIELD_L, (String) eventProperties.get(LINKED_DATA_ID)); + AdditionalFieldsUtil.addFieldToMarcRecord(srcRecord, TAG_999, SUBFIELD_L, String.valueOf(eventProperties.get(LINKED_DATA_ID))); } return super.executeFieldsManipulation(instance, srcRecord); } @@ -238,7 +238,7 @@ private Future postSnapshotInSrsAndHandleResponse(String id) { var snapshot = new Snapshot() .withJobExecutionId(id) .withProcessingStartedDate(new Date()) - .withStatus(PROCESSING_IN_PROGRESS); + .withStatus(PROCESSING_FINISHED); return super.postSnapshotInSrsAndHandleResponse(context.getOkapiLocation(), context.getToken(), snapshot, context.getTenantId()); }