From 9d66e9460fcebe8749614ae5a8117397fc84c212 Mon Sep 17 00:00:00 2001 From: azizbekxm Date: Wed, 15 Nov 2023 16:09:02 +0500 Subject: [PATCH] [MODAUD-174] - Improved code by changing field injection to constructor injection and changed method name --- .../folio/dao/acquisition/PieceEventsDao.java | 2 +- .../acquisition/impl/OrderEventsDaoImpl.java | 3 --- .../impl/OrderLineEventsDaoImpl.java | 4 +--- .../acquisition/impl/PieceEventsDaoImpl.java | 24 +++++++++---------- .../rest/impl/AuditDataAcquisitionImpl.java | 21 ++++++++-------- .../acquisition/PieceAuditEventsService.java | 2 +- .../impl/PieceAuditEventsServiceImpl.java | 4 ++-- .../org/folio/util/PostgresClientFactory.java | 5 ++-- .../PieceEventConsumersVerticle.java | 11 ++++----- .../consumers/PieceEventsHandler.java | 9 ++++--- 10 files changed, 37 insertions(+), 48 deletions(-) diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/PieceEventsDao.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/PieceEventsDao.java index d9d03b77..27b5ab54 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/PieceEventsDao.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/PieceEventsDao.java @@ -31,6 +31,6 @@ public interface PieceEventsDao { Future getAuditEventsByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId); - Future getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, + Future getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId); } diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java index f866e24b..3b868a51 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java @@ -12,7 +12,6 @@ import org.folio.rest.jaxrs.model.OrderAuditEvent; import org.folio.rest.jaxrs.model.OrderAuditEventCollection; import org.folio.util.PostgresClientFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.time.LocalDateTime; @@ -37,10 +36,8 @@ public class OrderEventsDaoImpl implements OrderEventsDao { public static final String INSERT_SQL = "INSERT INTO %s (id, action, order_id, user_id, event_date, action_date, modified_content_snapshot)" + " VALUES ($1, $2, $3, $4, $5, $6, $7)"; - @Autowired private final PostgresClientFactory pgClientFactory; - @Autowired public OrderEventsDaoImpl(PostgresClientFactory pgClientFactory) { this.pgClientFactory = pgClientFactory; } diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java index edae488c..4add4a26 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java @@ -12,7 +12,6 @@ import org.folio.rest.jaxrs.model.OrderLineAuditEvent; import org.folio.rest.jaxrs.model.OrderLineAuditEventCollection; import org.folio.util.PostgresClientFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.time.LocalDateTime; @@ -38,10 +37,9 @@ public class OrderLineEventsDaoImpl implements OrderLineEventsDao { private static final String INSERT_SQL = "INSERT INTO %s (id, action, order_id, order_line_id, user_id, event_date, action_date, modified_content_snapshot) " + "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"; - @Autowired private final PostgresClientFactory pgClientFactory; - @Autowired + public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) { this.pgClientFactory = pgClientFactory; } 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 ecb777a6..6ec6a430 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 @@ -29,7 +29,6 @@ import org.folio.rest.jaxrs.model.PieceAuditEvent; import org.folio.rest.jaxrs.model.PieceAuditEventCollection; import org.folio.util.PostgresClientFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository @@ -38,23 +37,22 @@ public class PieceEventsDaoImpl implements PieceEventsDao { private static final String TABLE_NAME = "acquisition_piece_log"; private static final String GET_BY_PIECE_ID_SQL = "SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, " + " (SELECT count(*) AS total_records FROM %s WHERE piece_id = $1) FROM %s WHERE piece_id = $1 %s LIMIT $2 OFFSET $3"; - private static final String GET_STATUS_CHANGE_HISTORY_BY_PIECE_ID_SQL = - "WITH StatusChanges AS (SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, " + - " LAG(modified_content_snapshot ->> 'receivingStatus') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status FROM %s " + - ")" + - "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, '') " + - "%s LIMIT $2 OFFSET $3"; - + private static final String GET_STATUS_CHANGE_HISTORY_BY_PIECE_ID_SQL = """ + WITH StatusChanges AS (SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, + LAG(modified_content_snapshot ->> 'status') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status FROM %s + ) + 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 ->> 'status' <> COALESCE(previous_status, '')) + FROM StatusChanges WHERE piece_id = $1 and modified_content_snapshot ->> 'status' <> COALESCE(previous_status, '') + %s LIMIT $2 OFFSET $3 + """; private static final String INSERT_SQL = "INSERT INTO %s (id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot)" + " VALUES ($1, $2, $3, $4, $5, $6, $7)"; private final PostgresClientFactory pgClientFactory; - @Autowired public PieceEventsDaoImpl(PostgresClientFactory pgClientFactory) { this.pgClientFactory = pgClientFactory; } @@ -92,7 +90,7 @@ public Future getAuditEventsByPieceId(String pieceId, } @Override - public Future getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { + public Future getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with piece id : {}", pieceId); Promise> promise = Promise.promise(); try { diff --git a/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java b/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java index a895edb7..993f7bd9 100644 --- a/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java +++ b/mod-audit-server/src/main/java/org/folio/rest/impl/AuditDataAcquisitionImpl.java @@ -18,7 +18,6 @@ import org.folio.services.acquisition.PieceAuditEventsService; import org.folio.spring.SpringContextUtil; import org.folio.util.ErrorUtils; -import org.springframework.beans.factory.annotation.Autowired; import javax.ws.rs.core.Response; import java.util.Map; @@ -29,16 +28,16 @@ public class AuditDataAcquisitionImpl implements AuditDataAcquisition { private static final Logger LOGGER = LogManager.getLogger(); - @Autowired - private OrderAuditEventsService orderAuditEventsService; + private final OrderAuditEventsService orderAuditEventsService; + private final OrderLineAuditEventsService orderLineAuditEventsService; + private final PieceAuditEventsService pieceAuditEventsService; - @Autowired - private OrderLineAuditEventsService orderLineAuditEventsService; - - @Autowired - private PieceAuditEventsService pieceAuditEventsService; - - public AuditDataAcquisitionImpl() { + public AuditDataAcquisitionImpl(OrderAuditEventsService orderAuditEventsService, + OrderLineAuditEventsService orderLineAuditEventsService, + PieceAuditEventsService pieceAuditEventsService) { + this.orderAuditEventsService = orderAuditEventsService; + this.orderLineAuditEventsService = orderLineAuditEventsService; + this.pieceAuditEventsService = pieceAuditEventsService; SpringContextUtil.autowireDependencies(this, Vertx.currentContext()); } @@ -108,7 +107,7 @@ public void getAuditDataAcquisitionPieceStatusChangeHistoryById(String pieceId, try { LOGGER.warn("Trying to get piece audit events with unique status by piece id: {}", pieceId); - pieceAuditEventsService.getAuditEventsWithUniqueStatusByPieceId(pieceId, sortBy, sortOrder.name(), limit, offset, tenantId) + pieceAuditEventsService.getAuditEventsWithStatusChangesByPieceId(pieceId, sortBy, sortOrder.name(), limit, offset, tenantId) .map(GetAuditDataAcquisitionPieceByIdResponse::respond200WithApplicationJson) .map(Response.class::cast) .otherwise(this::mapExceptionToResponse) diff --git a/mod-audit-server/src/main/java/org/folio/services/acquisition/PieceAuditEventsService.java b/mod-audit-server/src/main/java/org/folio/services/acquisition/PieceAuditEventsService.java index 51e82027..9b11db32 100644 --- a/mod-audit-server/src/main/java/org/folio/services/acquisition/PieceAuditEventsService.java +++ b/mod-audit-server/src/main/java/org/folio/services/acquisition/PieceAuditEventsService.java @@ -39,6 +39,6 @@ Future getAuditEventsByPieceId(String pieceId, String * @param offset offset * @return future with PieceAuditEventCollection */ - Future getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, + Future getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId); } 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 9f73a41b..fadcd204 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 @@ -41,9 +41,9 @@ public Future getAuditEventsByPieceId(String pieceId, } @Override - public Future getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { + public Future getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) { LOGGER.debug("getAuditEventsByOrderId:: Retrieving audit events with unique status changes for pieceId={} and tenantId={}", pieceId, tenantId); - return pieceEventsDao.getAuditEventsWithUniqueStatusByPieceId(pieceId, sortBy, sortOrder, limit, offset, tenantId); + return pieceEventsDao.getAuditEventsWithStatusChangesByPieceId(pieceId, sortBy, sortOrder, limit, offset, tenantId); } private Future handleFailures(Throwable throwable, String id) { diff --git a/mod-audit-server/src/main/java/org/folio/util/PostgresClientFactory.java b/mod-audit-server/src/main/java/org/folio/util/PostgresClientFactory.java index 40aab653..304b981c 100644 --- a/mod-audit-server/src/main/java/org/folio/util/PostgresClientFactory.java +++ b/mod-audit-server/src/main/java/org/folio/util/PostgresClientFactory.java @@ -3,15 +3,14 @@ import io.vertx.core.Vertx; import org.folio.rest.persist.PostgresClient; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class PostgresClientFactory { - private Vertx vertx; + private final Vertx vertx; - public PostgresClientFactory(@Autowired Vertx vertx) { + public PostgresClientFactory(Vertx vertx) { this.vertx = vertx; } diff --git a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/PieceEventConsumersVerticle.java b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/PieceEventConsumersVerticle.java index 56200b2f..7e13ef16 100644 --- a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/PieceEventConsumersVerticle.java +++ b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/PieceEventConsumersVerticle.java @@ -3,19 +3,18 @@ import java.util.List; import org.folio.kafka.AsyncRecordHandler; -import org.folio.kafka.KafkaConfig; import org.folio.util.AcquisitionEventType; import org.folio.verticle.AbstractConsumersVerticle; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class PieceEventConsumersVerticle extends AbstractConsumersVerticle { - @Autowired - private KafkaConfig kafkaConfig; - @Autowired - private AsyncRecordHandler pieceEventsHandler; + private final AsyncRecordHandler pieceEventsHandler; + + public PieceEventConsumersVerticle(AsyncRecordHandler pieceEventsHandler) { + this.pieceEventsHandler = pieceEventsHandler; + } @Override public List getEvents() { diff --git a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/PieceEventsHandler.java b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/PieceEventsHandler.java index e1c947d5..90a4e098 100644 --- a/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/PieceEventsHandler.java +++ b/mod-audit-server/src/main/java/org/folio/verticle/acquisition/consumers/PieceEventsHandler.java @@ -16,7 +16,6 @@ import org.folio.rest.jaxrs.model.PieceAuditEvent; import org.folio.rest.util.OkapiConnectionParams; import org.folio.services.acquisition.PieceAuditEventsService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @@ -24,11 +23,11 @@ public class PieceEventsHandler implements AsyncRecordHandler { private static final Logger LOGGER = LogManager.getLogger(); - private PieceAuditEventsService pieceAuditEventsService; - private Vertx vertx; + private final PieceAuditEventsService pieceAuditEventsService; + private final Vertx vertx; - public PieceEventsHandler(@Autowired Vertx vertx, - @Autowired PieceAuditEventsService pieceAuditEventsService) { + public PieceEventsHandler(Vertx vertx, + PieceAuditEventsService pieceAuditEventsService) { this.pieceAuditEventsService = pieceAuditEventsService; this.vertx = vertx; }