Skip to content

Commit

Permalink
MODTLR-13 Handle request update event from data tenant to update ECS TLR
Browse files Browse the repository at this point in the history
  • Loading branch information
MagzhanArtykov committed Jan 29, 2024
1 parent d0cb1c0 commit 37db0fd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

}
}
3 changes: 3 additions & 0 deletions src/main/java/org/folio/repository/EcsTlrRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<EcsTlrEntity, UUID> {
//Optional<EcsTlrEntity> findByTlrId(UUID tlrRequestId);
}
1 change: 1 addition & 0 deletions src/main/java/org/folio/service/EcsTlrService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface EcsTlrService {
Optional<EcsTlr> get(UUID requestId);
EcsTlr post(EcsTlr ecsTlr);
void updateRequestItem(UUID tlrRequestId, UUID itemId);
}
2 changes: 1 addition & 1 deletion src/main/java/org/folio/service/KafkaEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.folio.service;

public interface KafkaEventHandler {
void handle(String event);
void handleRequestEvent(String event);
}
12 changes: 12 additions & 0 deletions src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
16 changes: 14 additions & 2 deletions src/main/java/org/folio/service/impl/KafkaEventHandlerImpl.java
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/org/folio/support/KafkaEvent.java
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 37db0fd

Please sign in to comment.