Skip to content

Commit

Permalink
[MODAUD-174] - Extracted duplicate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Nov 15, 2023
1 parent c72b7b8 commit 6438c25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package org.folio.dao.acquisition.impl;

import static java.lang.String.format;
import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD;
import static org.folio.util.AuditEventDBConstants.ACTION_FIELD;
import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD;
import static org.folio.util.AuditEventDBConstants.ID_FIELD;
import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN;
import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD;
import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD;
import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD;
import static org.folio.util.DbUtils.formatDBTableName;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
Expand All @@ -14,15 +31,6 @@
import org.folio.util.PostgresClientFactory;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import static java.lang.String.format;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.util.AuditEventDBConstants.*;

@Repository
public class OrderEventsDaoImpl implements OrderEventsDao {

Expand All @@ -46,10 +54,9 @@ public OrderEventsDaoImpl(PostgresClientFactory pgClientFactory) {
public Future<RowSet<Row>> save(OrderAuditEvent orderAuditEvent, String tenantId) {
LOGGER.debug("save:: Saving Order AuditEvent with tenant id : {}", tenantId);
Promise<RowSet<Row>> promise = Promise.promise();
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);

String query = format(INSERT_SQL, logTable);

makeSaveCall(promise, query, orderAuditEvent, tenantId);
LOGGER.info("save:: Saved Order AuditEvent with tenant id : {}", tenantId);
return promise.future();
Expand All @@ -60,7 +67,7 @@ public Future<OrderAuditEventCollection> getAuditEventsByOrderId(String orderId,
LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with order id : {}", orderId);
Promise<RowSet<Row>> promise = Promise.promise();
try {
LOGGER.info("getAuditEventsByOrderId:: Trying to Retrieve AuditEvent with order id : {}", orderId);
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_ORDER_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder));
Tuple queryParams = Tuple.of(UUID.fromString(orderId), limit, offset);
Expand Down Expand Up @@ -116,8 +123,4 @@ private OrderAuditEvent mapRowToOrderEvent(Row row) {
.withOrderSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD)));
}

private String formatDBTableName(String tenantId, String table) {
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
return format("%s.%s", convertToPsqlStandard(tenantId), table);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package org.folio.dao.acquisition.impl;

import static java.lang.String.format;
import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD;
import static org.folio.util.AuditEventDBConstants.ACTION_FIELD;
import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD;
import static org.folio.util.AuditEventDBConstants.ID_FIELD;
import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN;
import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_LINE_ID_FIELD;
import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD;
import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD;
import static org.folio.util.DbUtils.formatDBTableName;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
Expand All @@ -14,15 +32,6 @@
import org.folio.util.PostgresClientFactory;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import static java.lang.String.format;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.util.AuditEventDBConstants.*;

@Repository
public class OrderLineEventsDaoImpl implements OrderLineEventsDao {

Expand All @@ -39,7 +48,6 @@ public class OrderLineEventsDaoImpl implements OrderLineEventsDao {

private final PostgresClientFactory pgClientFactory;


public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) {
this.pgClientFactory = pgClientFactory;
}
Expand All @@ -48,12 +56,10 @@ public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) {
public Future<RowSet<Row>> save(OrderLineAuditEvent orderLineAuditEvent, String tenantId) {
LOGGER.debug("save:: Saving OrderLine AuditEvent with tenant id : {}", tenantId);
Promise<RowSet<Row>> promise = Promise.promise();
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);

String query = format(INSERT_SQL, logTable);

makeSaveCall(promise, query, orderLineAuditEvent, tenantId);

LOGGER.info("save:: Saved OrderLine AuditEvent with tenant id : {}", tenantId);
return promise.future();
}
Expand All @@ -63,7 +69,7 @@ public Future<OrderLineAuditEventCollection> getAuditEventsByOrderLineId(String
LOGGER.debug("getAuditEventsByOrderLineId:: Retrieving AuditEvent with order line id : {}", orderLineId);
Promise<RowSet<Row>> promise = Promise.promise();
try {
LOGGER.info("getAuditEventsByOrderLineId:: Trying to Retrieve AuditEvent with order line id : {}", orderLineId);
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_ORDER_LINE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder));
Tuple queryParams = Tuple.of(UUID.fromString(orderLineId), limit, offset);
Expand Down Expand Up @@ -120,9 +126,4 @@ private OrderLineAuditEvent mapRowToOrderLineEvent(Row row) {
.withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC)))
.withOrderLineSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD)));
}

private String formatDBTableName(String tenantId, String table) {
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
return format("%s.%s", convertToPsqlStandard(tenantId), table);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.folio.dao.acquisition.impl;

import static java.lang.String.format;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD;
import static org.folio.util.AuditEventDBConstants.ACTION_FIELD;
import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD;
Expand Down Expand Up @@ -41,7 +40,8 @@ public class PieceEventsDaoImpl implements PieceEventsDao {
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
LAG(modified_content_snapshot ->> 'receivingStatus') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status
FROM %s WHERE piece_id=$1
)
SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot,
(SELECT COUNT(*) AS total_records FROM StatusChanges
Expand Down

0 comments on commit 6438c25

Please sign in to comment.