All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.mysql.handler.MySQLFindDomainEventsHandler.kt Maven / Gradle / Ivy

package org.enodeframework.mysql.handler

import com.google.common.collect.Maps
import io.vertx.core.AsyncResult
import io.vertx.core.Handler
import io.vertx.core.json.JsonObject
import io.vertx.mysqlclient.MySQLException
import io.vertx.sqlclient.Row
import io.vertx.sqlclient.RowSet
import org.enodeframework.common.exception.EventStoreException
import org.enodeframework.common.exception.IORuntimeException
import org.enodeframework.common.serializing.ISerializeService
import org.enodeframework.common.utils.DateUtil
import org.enodeframework.eventing.DomainEventStream
import org.enodeframework.eventing.IEventSerializer
import org.slf4j.LoggerFactory
import java.util.concurrent.CompletableFuture

class MySQLFindDomainEventsHandler(
    private val eventSerializer: IEventSerializer,
    private val serializeService: ISerializeService,
    private val msg: String
) : Handler>> {

    companion object {
        private val logger = LoggerFactory.getLogger(MySQLFindDomainEventsHandler::class.java)
    }

    var future = CompletableFuture>()

    override fun handle(ar: AsyncResult>) {
        if (ar.succeeded()) {
            future.complete(ar.result().map { row: Row ->
                this.convertFrom(row.toJson())
            }.toList())
            return
        }
        val throwable = ar.cause()
        if (throwable is MySQLException) {
            logger.error(
                "Find event has sql exception, msg: {}", msg, throwable
            )
            future.completeExceptionally(IORuntimeException(msg, throwable))
            return
        }
        logger.error(
            "Find event by has unknown exception, msg: {}", msg, throwable
        )
        future.completeExceptionally(EventStoreException(msg, throwable))
        return
    }

    private fun convertFrom(record: JsonObject): DomainEventStream {
        val gmtCreate = record.getValue("gmt_create")
        val date = DateUtil.parseDate(gmtCreate)
        return DomainEventStream(
            record.getString("command_id"),
            record.getString("aggregate_root_id"),
            record.getString("aggregate_root_type_name"),
            date,
            eventSerializer.deserialize(
                serializeService.deserialize(
                    record.getString("events"), MutableMap::class.java
                ) as MutableMap
            ),
            Maps.newHashMap()
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy