From 37db0fd7025345600ce5d9c59b283bd85dcf1968 Mon Sep 17 00:00:00 2001 From: MagzhanArtykov Date: Mon, 29 Jan 2024 14:54:16 +0600 Subject: [PATCH] MODTLR-13 Handle request update event from data tenant to update ECS TLR --- .../kafka}/KafkaEventListener.java | 6 +-- .../folio/repository/EcsTlrRepository.java | 3 ++ .../java/org/folio/service/EcsTlrService.java | 1 + .../org/folio/service/KafkaEventHandler.java | 2 +- .../folio/service/impl/EcsTlrServiceImpl.java | 12 +++++ .../service/impl/KafkaEventHandlerImpl.java | 16 ++++++- .../java/org/folio/support/KafkaEvent.java | 44 +++++++++++++++++++ 7 files changed, 78 insertions(+), 6 deletions(-) rename src/main/java/org/folio/{controller => listener/kafka}/KafkaEventListener.java (90%) create mode 100644 src/main/java/org/folio/support/KafkaEvent.java diff --git a/src/main/java/org/folio/controller/KafkaEventListener.java b/src/main/java/org/folio/listener/kafka/KafkaEventListener.java similarity index 90% rename from src/main/java/org/folio/controller/KafkaEventListener.java rename to src/main/java/org/folio/listener/kafka/KafkaEventListener.java index bbae044d..019c9522 100644 --- a/src/main/java/org/folio/controller/KafkaEventListener.java +++ b/src/main/java/org/folio/listener/kafka/KafkaEventListener.java @@ -1,4 +1,4 @@ -package org.folio.controller; +package org.folio.listener.kafka; import org.folio.service.KafkaEventHandler; import org.springframework.beans.factory.annotation.Autowired; @@ -22,7 +22,7 @@ public KafkaEventListener(@Autowired KafkaEventHandler eventHandler) { ) public void handleRequestEvent(String event) { log.info("handleRequestEvent:: message received: {}", event); - eventHandler.handle(event); + eventHandler.handleRequestEvent(event); } -} \ No newline at end of file +} diff --git a/src/main/java/org/folio/repository/EcsTlrRepository.java b/src/main/java/org/folio/repository/EcsTlrRepository.java index 3d77afe7..f4e6d8d8 100644 --- a/src/main/java/org/folio/repository/EcsTlrRepository.java +++ b/src/main/java/org/folio/repository/EcsTlrRepository.java @@ -3,8 +3,11 @@ import org.folio.domain.entity.EcsTlrEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; + +import java.util.Optional; import java.util.UUID; @Repository public interface EcsTlrRepository extends JpaRepository { + //Optional findByTlrId(UUID tlrRequestId); } diff --git a/src/main/java/org/folio/service/EcsTlrService.java b/src/main/java/org/folio/service/EcsTlrService.java index 4d7da52a..125f9930 100644 --- a/src/main/java/org/folio/service/EcsTlrService.java +++ b/src/main/java/org/folio/service/EcsTlrService.java @@ -8,4 +8,5 @@ public interface EcsTlrService { Optional get(UUID requestId); EcsTlr post(EcsTlr ecsTlr); + void updateRequestItem(UUID tlrRequestId, UUID itemId); } diff --git a/src/main/java/org/folio/service/KafkaEventHandler.java b/src/main/java/org/folio/service/KafkaEventHandler.java index 52b281cf..327bd386 100644 --- a/src/main/java/org/folio/service/KafkaEventHandler.java +++ b/src/main/java/org/folio/service/KafkaEventHandler.java @@ -1,5 +1,5 @@ package org.folio.service; public interface KafkaEventHandler { - void handle(String event); + void handleRequestEvent(String event); } diff --git a/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java b/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java index 43d20595..d66f3434 100644 --- a/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java +++ b/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java @@ -4,6 +4,7 @@ import java.util.UUID; import org.folio.domain.dto.EcsTlr; +import org.folio.domain.entity.EcsTlrEntity; import org.folio.domain.mapper.EcsTlrMapper; import org.folio.repository.EcsTlrRepository; import org.folio.service.EcsTlrService; @@ -35,4 +36,15 @@ public EcsTlr post(EcsTlr ecsTlr) { return requestsMapper.mapEntityToDto(ecsTlrRepository.save( requestsMapper.mapDtoToEntity(ecsTlr))); } + + @Override + public void updateRequestItem(UUID tlrRequestId, UUID itemId) { + log.debug("updateRequestItem:: parameters tlrRequestId: {}, itemId: {}", tlrRequestId, itemId); + ecsTlrRepository.findById(tlrRequestId).ifPresentOrElse( + ecsTlrEntity -> { + ecsTlrEntity.setItemId(itemId); + ecsTlrRepository.save(ecsTlrEntity); + }, + () -> log.error("updateRequestItem:: EcsTlr with tlrId: {} not found", tlrRequestId)); + } } diff --git a/src/main/java/org/folio/service/impl/KafkaEventHandlerImpl.java b/src/main/java/org/folio/service/impl/KafkaEventHandlerImpl.java index 7311ff6a..053cb978 100644 --- a/src/main/java/org/folio/service/impl/KafkaEventHandlerImpl.java +++ b/src/main/java/org/folio/service/impl/KafkaEventHandlerImpl.java @@ -1,16 +1,28 @@ package org.folio.service.impl; +import lombok.AllArgsConstructor; +import org.folio.service.EcsTlrService; import org.folio.service.KafkaEventHandler; +import org.folio.support.KafkaEvent; import org.springframework.stereotype.Service; import lombok.extern.log4j.Log4j2; +import java.util.UUID; + +@AllArgsConstructor @Service @Log4j2 public class KafkaEventHandlerImpl implements KafkaEventHandler { + private final EcsTlrService ecsTlrService; @Override - public void handle(String event) { - log.info("handle:: event consumed: {}", event); + public void handleRequestEvent(String event) { + log.info("handle:: request event consumed: {}", event); + KafkaEvent kafkaEvent = new KafkaEvent(event); + if(kafkaEvent.getEventType() == KafkaEvent.EventType.UPDATED) { + ecsTlrService.updateRequestItem(UUID.fromString(kafkaEvent.getNewNode().get("id").asText()), + UUID.fromString(kafkaEvent.getNewNode().get("itemId").asText())); + } } } diff --git a/src/main/java/org/folio/support/KafkaEvent.java b/src/main/java/org/folio/support/KafkaEvent.java new file mode 100644 index 00000000..a5fc4e5c --- /dev/null +++ b/src/main/java/org/folio/support/KafkaEvent.java @@ -0,0 +1,44 @@ +package org.folio.support; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Getter; +import lombok.extern.log4j.Log4j2; + +@Log4j2 +@Getter +public class KafkaEvent { + private static final ObjectMapper objectMapper = new ObjectMapper(); + public static final String STATUS = "status"; + private EventType eventType; + private JsonNode newNode; + private JsonNode oldNode; + + public KafkaEvent(String eventPayload) { + try { + JsonNode jsonNode = objectMapper.readTree(eventPayload); + setEventType(jsonNode.get("type").asText()); + setNewNode(jsonNode.get("data")); + setOldNode(jsonNode.get("data")); + } catch (Exception e) { + log.error("Could not parse input payload for processing event", e); + } + } + + private void setEventType(String eventType) { + this.eventType = EventType.valueOf(eventType); + } + private void setNewNode(JsonNode dataNode) { + if(dataNode != null) { + this.newNode = dataNode.get("new"); + } + } + private void setOldNode(JsonNode dataNode) { + if(dataNode != null) { + this.oldNode = dataNode.get("old"); + } + } + public enum EventType { + UPDATED, CREATED + } +}