Skip to content

Commit

Permalink
[MODAUD-174] - Extracted handleFauilures and optimized codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Nov 15, 2023
1 parent d04fa1f commit 3132186
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
""";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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
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;
}

@Override
public Future<RowSet<Row>> 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
Expand All @@ -40,11 +40,4 @@ public Future<OrderAuditEventCollection> getAuditEventsByOrderId(String orderId,
return orderEventsDao.getAuditEventsByOrderId(orderId, sortBy, sortOrder, limit, offset, tenantId);
}

private <T> Future<T> 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);
}

}
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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
public class OrderLineAuditEventsServiceImpl implements OrderLineAuditEventsService {

private static final Logger LOGGER = LogManager.getLogger();

private OrderLineEventsDao orderLineEventsDao;
private final OrderLineEventsDao orderLineEventsDao;

@Autowired
public OrderLineAuditEventsServiceImpl(OrderLineEventsDao orderLineEventsDao) {
this.orderLineEventsDao = orderLineEventsDao;
}

@Override
public Future<RowSet<Row>> 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
Expand All @@ -40,11 +40,4 @@ public Future<OrderLineAuditEventCollection> getAuditEventsByOrderLineId(String
return orderLineEventsDao.getAuditEventsByOrderLineId(orderLineId, sortBy, sortOrder, limit, offset, tenantId);
}

private <T> Future<T> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +19,6 @@ public class PieceAuditEventsServiceImpl implements PieceAuditEventsService {

private final PieceEventsDao pieceEventsDao;

@Autowired
public PieceAuditEventsServiceImpl(PieceEventsDao pieceEventsDao) {
this.pieceEventsDao = pieceEventsDao;
}
Expand All @@ -31,7 +27,10 @@ public PieceAuditEventsServiceImpl(PieceEventsDao pieceEventsDao) {
public Future<RowSet<Row>> 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
Expand All @@ -46,11 +45,4 @@ public Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceI
return pieceEventsDao.getAuditEventsWithStatusChangesByPieceId(pieceId, sortBy, sortOrder, limit, offset, tenantId);
}

private <T> Future<T> 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);
}

}
10 changes: 10 additions & 0 deletions mod-audit-server/src/main/java/org/folio/util/ErrorUtils.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,4 +25,9 @@ public static Errors buildErrors(String statusCode, Throwable throwable) {
.withTotalRecords(1);
}

public static <T> Future<T> 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);
}
}

0 comments on commit 3132186

Please sign in to comment.