From 3132186790ea5e8ba13a0c9d0c3a8fdd3f7047d4 Mon Sep 17 00:00:00 2001 From: azizbekxm Date: Wed, 15 Nov 2023 17:14:26 +0500 Subject: [PATCH] [MODAUD-174] - Extracted handleFauilures and optimized codebase --- .../acquisition/impl/PieceEventsDaoImpl.java | 4 ++-- .../impl/OrderAuditEventsServiceImpl.java | 21 +++++++------------ .../impl/OrderLineAuditEventsServiceImpl.java | 21 +++++++------------ .../impl/PieceAuditEventsServiceImpl.java | 18 +++++----------- .../main/java/org/folio/util/ErrorUtils.java | 10 +++++++++ 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java index 6595eef3..3c669d31 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java @@ -45,8 +45,8 @@ WITH StatusChanges AS (SELECT id, action, piece_id, user_id, event_date, action_ ) SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, (SELECT COUNT(*) AS total_records FROM StatusChanges - WHERE piece_id = $1 and modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '')) - FROM StatusChanges WHERE piece_id = $1 and modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '') + WHERE and modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '')) + FROM StatusChanges WHERE and modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '') %s LIMIT $2 OFFSET $3 """; diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderAuditEventsServiceImpl.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderAuditEventsServiceImpl.java index 3741c36e..511e3391 100644 --- a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderAuditEventsServiceImpl.java +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderAuditEventsServiceImpl.java @@ -1,19 +1,16 @@ package org.folio.services.acquisition.impl; -import static org.folio.util.AuditEventDBConstants.UNIQUE_CONSTRAINT_VIOLATION_CODE; +import static org.folio.util.ErrorUtils.handleFailures; import io.vertx.core.Future; -import io.vertx.pgclient.PgException; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.dao.acquisition.OrderEventsDao; -import org.folio.kafka.exception.DuplicateEventException; import org.folio.rest.jaxrs.model.OrderAuditEvent; import org.folio.rest.jaxrs.model.OrderAuditEventCollection; import org.folio.services.acquisition.OrderAuditEventsService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -21,9 +18,8 @@ public class OrderAuditEventsServiceImpl implements OrderAuditEventsService { private static final Logger LOGGER = LogManager.getLogger(); - private OrderEventsDao orderEventsDao; + private final OrderEventsDao orderEventsDao; - @Autowired public OrderAuditEventsServiceImpl(OrderEventsDao orderEvenDao) { this.orderEventsDao = orderEvenDao; } @@ -31,7 +27,11 @@ public OrderAuditEventsServiceImpl(OrderEventsDao orderEvenDao) { @Override public Future> saveOrderAuditEvent(OrderAuditEvent orderAuditEvent, String tenantId) { LOGGER.debug("saveOrderAuditEvent:: Saving order audit event with Id={} for tenantId={}", orderAuditEvent.getId(), tenantId); - return orderEventsDao.save(orderAuditEvent, tenantId).recover(throwable -> handleFailures(throwable, orderAuditEvent.getId())); + return orderEventsDao.save(orderAuditEvent, tenantId) + .recover(throwable -> { + LOGGER.error("handleFailures:: Handling Failures with Id : {}", orderAuditEvent.getOrderId()); + return handleFailures(throwable, orderAuditEvent.getId()); + }); } @Override @@ -40,11 +40,4 @@ public Future getAuditEventsByOrderId(String orderId, return orderEventsDao.getAuditEventsByOrderId(orderId, sortBy, sortOrder, limit, offset, tenantId); } - private Future handleFailures(Throwable throwable, String id) { - LOGGER.debug("handleFailures:: Handling Failures with Id : {}", id); - return (throwable instanceof PgException && ((PgException) throwable).getCode().equals(UNIQUE_CONSTRAINT_VIOLATION_CODE)) ? - Future.failedFuture(new DuplicateEventException(String.format("Event with Id=%s is already processed.", id))) : - Future.failedFuture(throwable); - } - } diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderLineAuditEventsServiceImpl.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderLineAuditEventsServiceImpl.java index e08c409f..0c4c0cb5 100644 --- a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderLineAuditEventsServiceImpl.java +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/OrderLineAuditEventsServiceImpl.java @@ -1,19 +1,16 @@ package org.folio.services.acquisition.impl; -import static org.folio.util.AuditEventDBConstants.UNIQUE_CONSTRAINT_VIOLATION_CODE; +import static org.folio.util.ErrorUtils.handleFailures; import io.vertx.core.Future; -import io.vertx.pgclient.PgException; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.dao.acquisition.OrderLineEventsDao; -import org.folio.kafka.exception.DuplicateEventException; import org.folio.rest.jaxrs.model.OrderLineAuditEvent; import org.folio.rest.jaxrs.model.OrderLineAuditEventCollection; import org.folio.services.acquisition.OrderLineAuditEventsService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -21,9 +18,8 @@ public class OrderLineAuditEventsServiceImpl implements OrderLineAuditEventsServ private static final Logger LOGGER = LogManager.getLogger(); - private OrderLineEventsDao orderLineEventsDao; + private final OrderLineEventsDao orderLineEventsDao; - @Autowired public OrderLineAuditEventsServiceImpl(OrderLineEventsDao orderLineEventsDao) { this.orderLineEventsDao = orderLineEventsDao; } @@ -31,7 +27,11 @@ public OrderLineAuditEventsServiceImpl(OrderLineEventsDao orderLineEventsDao) { @Override public Future> saveOrderLineAuditEvent(OrderLineAuditEvent orderLineAuditEvent, String tenantId) { LOGGER.debug("saveOrderLineAuditEvent:: Saving order line audit event with tenant Id : {}", tenantId); - return orderLineEventsDao.save(orderLineAuditEvent, tenantId).recover(throwable -> handleFailures(throwable, orderLineAuditEvent.getId())); + return orderLineEventsDao.save(orderLineAuditEvent, tenantId) + .recover(throwable -> { + LOGGER.error("handleFailures:: Handling Failures with Id : {}", orderLineAuditEvent.getOrderLineId()); + return handleFailures(throwable, orderLineAuditEvent.getId()); + }); } @Override @@ -40,11 +40,4 @@ public Future getAuditEventsByOrderLineId(String return orderLineEventsDao.getAuditEventsByOrderLineId(orderLineId, sortBy, sortOrder, limit, offset, tenantId); } - private Future handleFailures(Throwable throwable, String id) { - LOGGER.debug("handleFailures:: Handling Failures with Id : {}", id); - return (throwable instanceof PgException && ((PgException) throwable).getCode().equals(UNIQUE_CONSTRAINT_VIOLATION_CODE)) ? - Future.failedFuture(new DuplicateEventException(String.format("Event with Id=%s is already processed.", id))) : - Future.failedFuture(throwable); - } - } diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/PieceAuditEventsServiceImpl.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/PieceAuditEventsServiceImpl.java index fadcd204..6d902884 100644 --- a/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/PieceAuditEventsServiceImpl.java +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/impl/PieceAuditEventsServiceImpl.java @@ -1,19 +1,16 @@ package org.folio.services.acquisition.impl; -import static org.folio.util.AuditEventDBConstants.UNIQUE_CONSTRAINT_VIOLATION_CODE; +import static org.folio.util.ErrorUtils.handleFailures; import io.vertx.core.Future; -import io.vertx.pgclient.PgException; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.dao.acquisition.PieceEventsDao; -import org.folio.kafka.exception.DuplicateEventException; import org.folio.rest.jaxrs.model.PieceAuditEvent; import org.folio.rest.jaxrs.model.PieceAuditEventCollection; import org.folio.services.acquisition.PieceAuditEventsService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -22,7 +19,6 @@ public class PieceAuditEventsServiceImpl implements PieceAuditEventsService { private final PieceEventsDao pieceEventsDao; - @Autowired public PieceAuditEventsServiceImpl(PieceEventsDao pieceEventsDao) { this.pieceEventsDao = pieceEventsDao; } @@ -31,7 +27,10 @@ public PieceAuditEventsServiceImpl(PieceEventsDao pieceEventsDao) { public Future> savePieceAuditEvent(PieceAuditEvent pieceAuditEvent, String tenantId) { LOGGER.debug("savePieceAuditEvent:: Trying to save piece audit event with id={} for tenantId={}", pieceAuditEvent.getPieceId(), tenantId); return pieceEventsDao.save(pieceAuditEvent, tenantId) - .recover(throwable -> handleFailures(throwable, pieceAuditEvent.getId())); + .recover(throwable -> { + LOGGER.error("handleFailures:: Handling Failures with Id : {}", pieceAuditEvent.getPieceId()); + return handleFailures(throwable, pieceAuditEvent.getId()); + }); } @Override @@ -46,11 +45,4 @@ public Future getAuditEventsWithStatusChangesByPieceI return pieceEventsDao.getAuditEventsWithStatusChangesByPieceId(pieceId, sortBy, sortOrder, limit, offset, tenantId); } - private Future handleFailures(Throwable throwable, String id) { - LOGGER.debug("handleFailures:: Handling Failures with id={}", id); - return (throwable instanceof PgException pgException && pgException.getCode().equals(UNIQUE_CONSTRAINT_VIOLATION_CODE)) ? - Future.failedFuture(new DuplicateEventException(String.format("Event with id=%s is already processed.", id))) : - Future.failedFuture(throwable); - } - } diff --git a/mod-audit-server/src/main/java/org/folio/util/ErrorUtils.java b/mod-audit-server/src/main/java/org/folio/util/ErrorUtils.java index 12ebd9ae..4d7b8e74 100644 --- a/mod-audit-server/src/main/java/org/folio/util/ErrorUtils.java +++ b/mod-audit-server/src/main/java/org/folio/util/ErrorUtils.java @@ -1,6 +1,11 @@ package org.folio.util; +import static org.folio.util.AuditEventDBConstants.UNIQUE_CONSTRAINT_VIOLATION_CODE; + +import io.vertx.core.Future; +import io.vertx.pgclient.PgException; import org.folio.HttpStatus; +import org.folio.kafka.exception.DuplicateEventException; import org.folio.rest.jaxrs.model.Error; import org.folio.rest.jaxrs.model.Errors; @@ -20,4 +25,9 @@ public static Errors buildErrors(String statusCode, Throwable throwable) { .withTotalRecords(1); } + public static Future handleFailures(Throwable throwable, String id) { + return (throwable instanceof PgException pgException && pgException.getCode().equals(UNIQUE_CONSTRAINT_VIOLATION_CODE)) ? + Future.failedFuture(new DuplicateEventException(String.format("Event with id=%s is already processed.", id))) : + Future.failedFuture(throwable); + } }