@@ -2,20 +2,30 @@ package co.statu.rule.database
22
33import co.statu.parsek.api.ParsekPlugin
44import co.statu.parsek.api.config.PluginConfigManager
5+ import co.statu.rule.database.api.DatabaseHelper
56import co.statu.rule.database.impl.SchemeVersionDaoImpl
67import co.statu.rule.database.model.SchemeVersion
78import io.vertx.core.Vertx
89import io.vertx.core.json.JsonObject
910import io.vertx.jdbcclient.JDBCPool
1011import org.slf4j.Logger
12+ import org.springframework.beans.factory.config.ConfigurableBeanFactory
13+ import org.springframework.context.annotation.Scope
14+ import org.springframework.stereotype.Component
1115import java.sql.BatchUpdateException
1216import kotlin.system.exitProcess
1317
18+ @Component
19+ @Scope(value = ConfigurableBeanFactory .SCOPE_SINGLETON )
1420class DatabaseManager (
1521 private val vertx : Vertx ,
16- private val pluginConfigManager : PluginConfigManager <DatabaseConfig >,
1722 private val logger : Logger ,
23+ private val databasePlugin : DatabasePlugin
1824) {
25+ private val pluginConfigManager by lazy {
26+ databasePlugin.pluginBeanContext.getBean(PluginConfigManager ::class .java) as PluginConfigManager <DatabaseConfig >
27+ }
28+
1929 private lateinit var pool: JDBCPool
2030
2131 private val tables = mutableMapOf<ParsekPlugin , MutableList <Dao <* >>>()
@@ -69,7 +79,7 @@ class DatabaseManager(
6979 }
7080
7181 private suspend fun initSchemeVersion (plugin : ParsekPlugin , jdbcPool : JDBCPool ) {
72- val lastSchemeVersion = schemeVersionDaoImpl.getLastSchemeVersion(plugin.context. pluginId, jdbcPool)
82+ val lastSchemeVersion = schemeVersionDaoImpl.getLastSchemeVersion(plugin.pluginId, jdbcPool)
7383
7484 val latestMigration = getLatestMigration(plugin)
7585
@@ -80,9 +90,9 @@ class DatabaseManager(
8090 if (latestMigration == null ) {
8191 schemeVersionDaoImpl.add(
8292 SchemeVersion (
83- pluginId = plugin.context. pluginId,
93+ pluginId = plugin.pluginId,
8494 version = 1 ,
85- extra = " Init ${plugin.context. pluginId} "
95+ extra = " Init ${plugin.pluginId} "
8696 ),
8797 jdbcPool
8898 )
@@ -92,7 +102,7 @@ class DatabaseManager(
92102
93103 schemeVersionDaoImpl.add(
94104 SchemeVersion (
95- pluginId = plugin.context. pluginId,
105+ pluginId = plugin.pluginId,
96106 version = latestMigration.SCHEME_VERSION ,
97107 extra = latestMigration.SCHEME_VERSION_INFO
98108 ),
@@ -105,13 +115,12 @@ class DatabaseManager(
105115
106116 initTables(plugin)
107117
108- logger.info(" \" ${plugin.context. pluginId} \" 's database has been initialized" )
118+ logger.info(" \" ${plugin.pluginId} \" 's database has been initialized" )
109119 }
110120
111121 suspend fun initialize (
112122 plugin : ParsekPlugin ,
113- tables : List <Dao <* >>,
114- migrations : List <DatabaseMigration >
123+ databaseHelper : DatabaseHelper ? = null
115124 ) {
116125 if (this .tables[plugin] == null ) {
117126 this .tables[plugin] = mutableListOf ()
@@ -121,14 +130,15 @@ class DatabaseManager(
121130 this .migrations[plugin] = mutableListOf ()
122131 }
123132
124- this .tables[plugin]!! .addAll(tables)
125- this .migrations[plugin]!! .addAll(migrations)
133+ databaseHelper?.tables?. let { this .tables[plugin]!! .addAll(it) }
134+ databaseHelper?.migrations?. let { this .migrations[plugin]!! .addAll(it) }
126135
127136 val jdbcPool: JDBCPool
128137
129138 try {
130139 jdbcPool = getConnectionPool()
131140 } catch (e: Exception ) {
141+ e.printStackTrace()
132142 logger.info(" Connection to database failed! Shutting down..." )
133143
134144 exitProcess(1 )
@@ -137,7 +147,7 @@ class DatabaseManager(
137147 val lastSchemeVersion: SchemeVersion ?
138148
139149 try {
140- lastSchemeVersion = schemeVersionDaoImpl.getLastSchemeVersion(plugin.context. pluginId, jdbcPool)
150+ lastSchemeVersion = schemeVersionDaoImpl.getLastSchemeVersion(plugin.pluginId, jdbcPool)
141151 } catch (e: BatchUpdateException ) {
142152 try {
143153 if (plugin is DatabasePlugin ) {
@@ -151,7 +161,7 @@ class DatabaseManager(
151161 return
152162 } catch (e: Exception ) {
153163 logger.error(e.message)
154- logger.error(" Database Error: Could not install plugin \" ${plugin.context. pluginId} \" . Shutting down..." )
164+ logger.error(" Database Error: Could not install plugin \" ${plugin.pluginId} \" . Shutting down..." )
155165
156166 exitProcess(1 )
157167 }
@@ -180,7 +190,7 @@ class DatabaseManager(
180190 internal fun getLatestMigration (plugin : ParsekPlugin ) = migrations[plugin]?.maxByOrNull { it.SCHEME_VERSION }
181191
182192 suspend fun checkMigration (plugin : ParsekPlugin , jdbcPool : JDBCPool , lastSchemeVersion : SchemeVersion ? ) {
183- logger.info(" Checking available database migrations for \" ${plugin.context. pluginId} \" " )
193+ logger.info(" Checking available database migrations for \" ${plugin.pluginId} \" " )
184194
185195 val databaseVersion = lastSchemeVersion?.version ? : 0
186196
@@ -196,7 +206,7 @@ class DatabaseManager(
196206 private suspend fun updateSchemeVersion (version : Int , info : String , plugin : ParsekPlugin , jdbcPool : JDBCPool ) {
197207 schemeVersionDaoImpl.add(
198208 SchemeVersion (
199- pluginId = plugin.context. pluginId,
209+ pluginId = plugin.pluginId,
200210 version = version,
201211 extra = info
202212 ),
0 commit comments