Skip to content

Commit

Permalink
[MODAUD-174] - Improved code by changing field injection to construct…
Browse files Browse the repository at this point in the history
…or injection and changed method name
  • Loading branch information
azizbekxm committed Nov 15, 2023
1 parent 2376e3c commit 9d66e94
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public interface PieceEventsDao {
Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId, String sortBy, String sortOrder,
int limit, int offset, String tenantId);

Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder,
Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder,
int limit, int offset, String tenantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -92,7 +90,7 @@ public Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId,
}

@Override
public Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) {
public Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) {
LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with piece id : {}", pieceId);
Promise<RowSet<Row>> promise = Promise.promise();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId, String
* @param offset offset
* @return future with PieceAuditEventCollection
*/
Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder,
Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder,
int limit, int offset, String tenantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId,
}

@Override
public Future<PieceAuditEventCollection> getAuditEventsWithUniqueStatusByPieceId(String pieceId, String sortBy, String sortOrder, int limit, int offset, String tenantId) {
public Future<PieceAuditEventCollection> 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 <T> Future<T> handleFailures(Throwable throwable, String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> pieceEventsHandler;
private final AsyncRecordHandler<String, String> pieceEventsHandler;

public PieceEventConsumersVerticle(AsyncRecordHandler<String, String> pieceEventsHandler) {
this.pieceEventsHandler = pieceEventsHandler;
}

@Override
public List<String> getEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
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
public class PieceEventsHandler implements AsyncRecordHandler<String, String> {

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;
}
Expand Down

0 comments on commit 9d66e94

Please sign in to comment.