Skip to content

Commit

Permalink
[MODINVOSTO-187] Register new beans and update existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Nov 5, 2024
1 parent 29fa7bf commit 29503e1
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 27 deletions.
4 changes: 3 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@
{ "name": "DB_DATABASE", "value": "okapi_modules" },
{ "name": "DB_QUERYTIMEOUT", "value": "60000" },
{ "name": "DB_CHARSET", "value": "UTF-8" },
{ "name": "DB_MAXPOOLSIZE", "value": "5" }
{ "name": "DB_MAXPOOLSIZE", "value": "5" },
{ "name": "KAFKA_HOST", "value": "10.0.2.15" },
{ "name": "KAFKA_PORT", "value": "9092" }
]
}
}
3 changes: 1 addition & 2 deletions ramls/audit-outbox.raml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ documentation:
content: This API is intended for internal use only by the Timer interface

types:
event-action: !include acq-models/mod-invoice-storage/schemas/event_action.json
event-topic: !include acq-models/mod-invoice-storage/schemas/event_topic.json
outbox-event-log: !include acq-models/mod-invoice-storage/schemas/outbox_event_log.json
invoice-audit-event: !include acq-models/mod-invoice-storage/schemas/invoice_audit_event.json
invoice-line-audit-event: !include acq-models/mod-invoice-storage/schemas/invoice_line_audit_event.json
event-topic: !include acq-models/mod-invoice-storage/schemas/event_topic.json

/invoice-storage/audit-outbox:
/process:
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/org/folio/config/ApplicationConfig.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package org.folio.config;

import org.folio.dao.audit.AuditOutboxEventLogDAO;
import org.folio.dao.audit.AuditOutboxEventLogPostgresDAO;
import org.folio.dao.invoice.InvoiceDAO;
import org.folio.dao.invoice.InvoicePostgresDAO;
import org.folio.dao.lines.InvoiceLinesDAO;
import org.folio.dao.lines.InvoiceLinesPostgresDAO;
import org.folio.dao.lock.InternalLockDAO;
import org.folio.dao.lock.InternalLockPostgresDAO;
import org.folio.kafka.KafkaConfig;
import org.folio.rest.core.RestClient;
import org.folio.service.InvoiceLineNumberService;
import org.folio.service.InvoiceLineStorageService;
import org.folio.service.InvoiceStorageService;
import org.folio.service.audit.AuditEventProducer;
import org.folio.service.audit.AuditOutboxService;
import org.folio.service.order.OrderStorageService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(KafkaConfiguration.class)
public class ApplicationConfig {

@Bean
Expand All @@ -34,12 +44,38 @@ public InvoiceLinesDAO invoiceLinesDAO() {
}

@Bean
public InvoiceStorageService invoiceStorageService(InvoiceDAO invoiceDAO) {
return new InvoiceStorageService(invoiceDAO);
public AuditOutboxEventLogDAO auditOutboxEventLogDAO() {
return new AuditOutboxEventLogPostgresDAO();
}

@Bean
public InternalLockDAO internalLockDAO() {
return new InternalLockPostgresDAO();
}

@Bean
public InvoiceStorageService invoiceStorageService(InvoiceDAO invoiceDAO, AuditOutboxService auditOutboxService) {
return new InvoiceStorageService(invoiceDAO, auditOutboxService);
}

@Bean
public InvoiceLineStorageService invoiceLineStorageService(InvoiceLinesDAO invoiceLinesDAO, AuditOutboxService auditOutboxService) {
return new InvoiceLineStorageService(invoiceLinesDAO, auditOutboxService);
}

@Bean
public InvoiceLineNumberService invoiceLineNumberService(InvoiceDAO invoiceDAO, InvoiceLinesDAO invoiceLinesDAO) {
return new InvoiceLineNumberService(invoiceDAO, invoiceLinesDAO);
}

@Bean
public AuditEventProducer auditEventProducer(KafkaConfig kafkaConfig) {
return new AuditEventProducer(kafkaConfig);
}

@Bean
public AuditOutboxService auditOutboxService(AuditOutboxEventLogDAO auditOutboxEventLogDAO, InternalLockDAO internalLockDAO, AuditEventProducer producer) {
return new AuditOutboxService(auditOutboxEventLogDAO, internalLockDAO, producer);
}

}
36 changes: 36 additions & 0 deletions src/main/java/org/folio/config/KafkaConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.folio.config;

import org.folio.kafka.KafkaConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class KafkaConfiguration {

@Value("${KAFKA_HOST:kafka}")
private String kafkaHost;
@Value("${KAFKA_PORT:9092}")
private String kafkaPort;
@Value("${OKAPI_URL:http://okapi:9130}")
private String okapiUrl;
@Value("${REPLICATION_FACTOR:1}")
private int replicationFactor;
@Value("${MAX_REQUEST_SIZE:1048576}")
private int maxRequestSize;
@Value("${ENV:folio}")
private String envId;

@Bean
public KafkaConfig kafkaConfig() {
return KafkaConfig.builder()
.envId(envId)
.kafkaHost(kafkaHost)
.kafkaPort(kafkaPort)
.okapiUrl(okapiUrl)
.replicationFactor(replicationFactor)
.maxRequestSize(maxRequestSize)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.Map;

import org.folio.dao.lines.InvoiceLinesDAO;
import org.folio.rest.jaxrs.model.EventAction;
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent;
import org.folio.rest.persist.DBClient;
import org.folio.service.audit.AuditOutboxService;

Expand All @@ -26,8 +26,8 @@
@RequiredArgsConstructor
public class InvoiceLineStorageService {

private final AuditOutboxService auditOutboxService;
private final InvoiceLinesDAO invoiceLinesDAO;
private final AuditOutboxService auditOutboxService;

public void createInvoiceLine(InvoiceLine invoiceLine, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext, Map<String, String> headers) {
Expand All @@ -36,7 +36,7 @@ public void createInvoiceLine(InvoiceLine invoiceLine, Handler<AsyncResult<Respo
log.info("createInvoiceLine:: Creating a new invoiceLine by id: {}", invoiceLine.getId());
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceLinesDAO.createInvoiceLine(invoiceLine, conn)
.compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, EventAction.CREATE, headers)))
.compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, InvoiceLineAuditEvent.Action.CREATE, headers)))
.onSuccess(s -> {
log.info("createInvoiceLine:: Successfully created a new invoiceLine by id: {}", invoiceLine.getId());
asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_LINES_PREFIX + invoiceLine.getId(), invoiceLine));
Expand All @@ -62,7 +62,7 @@ public void updateInvoiceLine(String id, InvoiceLine invoiceLine, Map<String, St
log.info("updateInvoiceLine:: Updating invoice line with id: {}", id);
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceLinesDAO.updateInvoiceLine(id, invoiceLine, conn)
.compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, EventAction.EDIT, headers)))
.compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, InvoiceLineAuditEvent.Action.EDIT, headers)))
.onSuccess(s -> {
log.info("updateInvoiceLine:: Successfully updated invoice line with id: {}", id);
asyncResultHandler.handle(buildNoContentResponse());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/folio/service/InvoiceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.folio.dao.invoice.InvoiceDAO;
import org.folio.rest.jaxrs.model.Document;
import org.folio.rest.jaxrs.model.DocumentCollection;
import org.folio.rest.jaxrs.model.EventAction;
import org.folio.rest.jaxrs.model.Invoice;
import org.folio.rest.jaxrs.model.InvoiceAuditEvent;
import org.folio.rest.jaxrs.model.InvoiceDocument;
import org.folio.rest.jaxrs.resource.InvoiceStorage.GetInvoiceStorageInvoicesDocumentsByIdResponse;
import org.folio.rest.persist.DBClient;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void postInvoiceStorageInvoices(Invoice invoice, Handler<AsyncResult<Resp
log.info("postInvoiceStorageInvoices:: Creating a new invoice by id: {}", invoice.getId());
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceDAO.createInvoice(invoice, conn)
.compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, EventAction.CREATE, headers)))
.compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, InvoiceAuditEvent.Action.CREATE, headers)))
.onSuccess(s -> {
log.info("postInvoiceStorageInvoices:: Successfully created a new invoice by id: {}", invoice.getId());
asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_PREFIX + invoice.getId(), invoice));
Expand All @@ -76,7 +76,7 @@ public void putInvoiceStorageInvoicesById(String id, Invoice invoice, Map<String
log.info("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id);
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceDAO.updateInvoice(invoice, conn)
.compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, EventAction.EDIT, headers)))
.compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, InvoiceAuditEvent.Action.EDIT, headers)))
.onSuccess(s -> {
log.info("putInvoiceStorageInvoicesById:: Successfully updated invoice with id: {}", id);
asyncResultHandler.handle(buildNoContentResponse());
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/folio/service/audit/AuditEventProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.folio.kafka.KafkaTopicNameHelper;
import org.folio.kafka.SimpleKafkaProducerManager;
import org.folio.kafka.services.KafkaProducerRecordBuilder;
import org.folio.rest.jaxrs.model.EventAction;
import org.folio.rest.jaxrs.model.EventTopic;
import org.folio.rest.jaxrs.model.Invoice;
import org.folio.rest.jaxrs.model.InvoiceAuditEvent;
Expand Down Expand Up @@ -38,21 +37,21 @@ public class AuditEventProducer {
* @param okapiHeaders the okapi headers
* @return future with true if sending was success or failed future in another case
*/
public Future<Void> sendInvoiceEvent(Invoice invoice, EventAction eventAction, Map<String, String> okapiHeaders) {
public Future<Void> sendInvoiceEvent(Invoice invoice, InvoiceAuditEvent.Action eventAction, Map<String, String> okapiHeaders) {
var event = getAuditEvent(invoice, eventAction);
log.info("sendInvoiceEvent:: Sending event with id: {} and invoiceId: {} to Kafka", event.getId(), invoice.getId());
return sendToKafka(EventTopic.ACQ_INVOICE_CHANGED, event.getInvoiceId(), event, okapiHeaders)
.onFailure(t -> log.warn("sendInvoiceEvent:: Failed to send event with id: {} and invoiceId: {} to Kafka", event.getId(), invoice.getId(), t));
}

public Future<Void> sendInvoiceLineEvent(InvoiceLine invoiceLine, EventAction action, Map<String, String> okapiHeaders) {
public Future<Void> sendInvoiceLineEvent(InvoiceLine invoiceLine, InvoiceLineAuditEvent.Action action, Map<String, String> okapiHeaders) {
var event = getAuditEvent(invoiceLine, action);
log.info("sendInvoiceLineEvent:: Sending event with id: {} and invoiceLineId: {} to Kafka", event.getId(), invoiceLine.getId());
return sendToKafka(EventTopic.ACQ_INVOICE_LINE_CHANGED, event.getInvoiceId(), event, okapiHeaders)
.onFailure(t -> log.warn("sendInvoiceLineEvent:: Failed to send event with id: {} and invoiceLineId: {} to Kafka", event.getId(), invoiceLine.getId(), t));
}

private InvoiceAuditEvent getAuditEvent(Invoice invoice, EventAction eventAction) {
private InvoiceAuditEvent getAuditEvent(Invoice invoice, InvoiceAuditEvent.Action eventAction) {
return new InvoiceAuditEvent()
.withId(UUID.randomUUID().toString())
.withAction(eventAction)
Expand All @@ -63,15 +62,15 @@ private InvoiceAuditEvent getAuditEvent(Invoice invoice, EventAction eventAction
.withInvoiceSnapshot(invoice.withMetadata(null));
}

private InvoiceAuditEvent getAuditEvent(InvoiceLine invoiceLine, EventAction eventAction) {
private InvoiceAuditEvent getAuditEvent(InvoiceLine invoiceLine, InvoiceLineAuditEvent.Action eventAction) {
return new InvoiceLineAuditEvent()
.withId(UUID.randomUUID().toString())
.withAction(eventAction)
.withInvoiceId(invoiceLine.getId())
.withInvoiceLineId(invoiceLine.getId())
.withEventDate(new Date())
.withActionDate(invoiceLine.getMetadata().getUpdatedDate())
.withUserId(invoiceLine.getMetadata().getUpdatedByUserId())
.withInvoiceSnapshot(invoiceLine.withMetadata(null));
.withInvoiceLineSnapshot(invoiceLine.withMetadata(null));
}

private Future<Void> sendToKafka(EventTopic eventTopic, String key, Object eventPayload, Map<String, String> okapiHeaders) {
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/folio/service/audit/AuditOutboxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import org.folio.dao.audit.AuditOutboxEventLogDAO;
import org.folio.dao.lock.InternalLockDAO;
import org.folio.okapi.common.GenericCompositeFuture;
import org.folio.rest.jaxrs.model.EventAction;
import org.folio.rest.jaxrs.model.Invoice;
import org.folio.rest.jaxrs.model.InvoiceAuditEvent;
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.jaxrs.model.InvoiceLineAuditEvent;
import org.folio.rest.jaxrs.model.OutboxEventLog;
import org.folio.rest.jaxrs.model.OutboxEventLog.EntityType;
import org.folio.rest.persist.Conn;
Expand Down Expand Up @@ -63,8 +64,16 @@ public Future<Integer> processOutboxEventLogs(Map<String, String> okapiHeaders,
private List<Future<Void>> sendEventLogsToKafka(List<OutboxEventLog> eventLogs, Map<String, String> okapiHeaders) {
return eventLogs.stream().map(eventLog ->
switch (eventLog.getEntityType()) {
case INVOICE -> producer.sendInvoiceEvent(Json.decodeValue(eventLog.getPayload(), Invoice.class), eventLog.getAction(), okapiHeaders);
case INVOICE_LINE -> producer.sendInvoiceLineEvent(Json.decodeValue(eventLog.getPayload(), InvoiceLine.class), eventLog.getAction(), okapiHeaders);
case INVOICE -> {
var invoice = Json.decodeValue(eventLog.getPayload(), Invoice.class);
var action = InvoiceAuditEvent.Action.fromValue(eventLog.getAction());
yield producer.sendInvoiceEvent(invoice, action, okapiHeaders);
}
case INVOICE_LINE -> {
var invoiceLine = Json.decodeValue(eventLog.getPayload(), InvoiceLine.class);
var action = InvoiceLineAuditEvent.Action.fromValue(eventLog.getAction());
yield producer.sendInvoiceLineEvent(invoiceLine, action, okapiHeaders);
}
}).toList();
}

Expand All @@ -77,8 +86,8 @@ private List<Future<Void>> sendEventLogsToKafka(List<OutboxEventLog> eventLogs,
* @param okapiHeaders okapi headers
* @return future with saved outbox log id in the same transaction
*/
public Future<String> saveInvoiceOutboxLog(Conn conn, Invoice entity, EventAction action, Map<String, String> okapiHeaders) {
return saveOutboxLog(conn, okapiHeaders, action, EntityType.INVOICE, entity.getId(), entity);
public Future<String> saveInvoiceOutboxLog(Conn conn, Invoice entity, InvoiceAuditEvent.Action action, Map<String, String> okapiHeaders) {
return saveOutboxLog(conn, okapiHeaders, action.value(), EntityType.INVOICE, entity.getId(), entity);
}

/**
Expand All @@ -90,11 +99,11 @@ public Future<String> saveInvoiceOutboxLog(Conn conn, Invoice entity, EventActio
* @param okapiHeaders okapi headers
* @return future with saved outbox log id in the same transaction
*/
public Future<String> saveInvoiceLineOutboxLog(Conn conn, InvoiceLine entity, EventAction action, Map<String, String> okapiHeaders) {
return saveOutboxLog(conn, okapiHeaders, action, EntityType.INVOICE_LINE, entity.getId(), entity);
public Future<String> saveInvoiceLineOutboxLog(Conn conn, InvoiceLine entity, InvoiceLineAuditEvent.Action action, Map<String, String> okapiHeaders) {
return saveOutboxLog(conn, okapiHeaders, action.value(), EntityType.INVOICE_LINE, entity.getId(), entity);
}

private Future<String> saveOutboxLog(Conn conn, Map<String, String> okapiHeaders, EventAction action, EntityType entityType, String entityId, Object entity) {
private Future<String> saveOutboxLog(Conn conn, Map<String, String> okapiHeaders, String action, EntityType entityType, String entityId, Object entity) {
log.debug("saveOutboxLog:: Saving outbox log for {} with id: {}", entityType, entityId);
var eventLog = new OutboxEventLog()
.withEventId(UUID.randomUUID().toString())
Expand Down

0 comments on commit 29503e1

Please sign in to comment.