Add multiple languages

This commit is contained in:
2025-02-09 18:31:51 +01:00
parent 47e1b47028
commit 66617923b7
11 changed files with 141 additions and 20 deletions

View File

@@ -9,10 +9,12 @@ class LanguageDao(id: EntityID<Int>) : IntEntity(id) {
var name by Languages.name
var shortName by Languages.shortName
var alphaCode by Languages.alphaCode
fun toModel() : Language = Language(
id = id.value,
name = name,
shortName = shortName
shortName = shortName,
alphaCode = alphaCode
)
}

View File

@@ -9,7 +9,8 @@ import org.jetbrains.exposed.sql.Table
data class Language(
val id: Int,
val name: String,
val shortName: String
val shortName: String,
val alphaCode: String?
)

View File

@@ -13,6 +13,8 @@ import javax.sql.DataSource
import tables.*
import dao.*
import com.typesafe.config.ConfigFactory
import models.Dictionary
import models.Language
import models.Translation
object DatabaseFactory {
@@ -21,7 +23,9 @@ object DatabaseFactory {
private val dbs : MutableMap<Int,Database> = mutableMapOf()
private val hdb : MutableMap<String,Database> = mutableMapOf()
private val htrans: MutableMap<String,Translation> = mutableMapOf()
private val halpha: MutableMap<String,Language> = mutableMapOf()
private val dictById: MutableMap<Int,Dictionary> = mutableMapOf()
private val hLang1: MutableMap<Int,Language> = mutableMapOf()
fun connectAndMigrate() {
log.info("Initialising database")
val pool = hikari()
@@ -43,10 +47,17 @@ object DatabaseFactory {
return HikariDataSource(config)
}
fun getTranslationForLanguages(l1: String, l2: String): Translation? = htrans["${l1}${l2}"]?: null
fun getTranslationForLanguages(l1: String, l2: String): Translation? = htrans["${l1.lowercase()}${l2.lowercase()}"]?: null
fun getLanguageForCode(l: String): Language? = halpha[l.uppercase()]?: null
fun getLanguageForId(id: Int): Language? = hLang1[id]?: null
fun getDictionaies(): Map<Int,Dictionary> = dictById
fun getAllLanguages(): List<Language> {
return halpha.values.toList()
}
fun connectAll() {
if (getDictionaies().any()) return
fun connectAll(): Map<Int,Database> {
val hMap : MutableMap<Int, Database> = mutableMapOf()
transaction {
for (trans in TranslationDao.all()) {
val l1 = trans.lang1.shortName.lowercase()
@@ -55,6 +66,11 @@ object DatabaseFactory {
htrans["${l1}${l2}"] = trans.toModel()
}
for (lang in LanguageDao.all()) {
halpha[lang.shortName] = lang.toModel()
hLang1[lang.id.value] = lang.toModel()
}
for (dict in DictionaryDao.all()) {
val cfg = ConfigFactory.load().getConfig("h2")
val config = HikariConfig().apply {
@@ -68,10 +84,14 @@ object DatabaseFactory {
val db = HikariDataSource(config)
val dbc = Database.connect(db)
dbs[dict.id.value] = dbc
dictById[dict.id.value] = dict.toModel()
hdb["${dict.lang1.shortName.lowercase()}${dict.lang2.shortName.lowercase()}"] = dbc
}
}
return hMap
println("DBS PRINT")
println(dbs)
}
private fun runFlyway(datasource: DataSource) {

View File

@@ -6,4 +6,7 @@ import models.Language
interface LanguageService {
fun getLanguage(id: Int): Language?
fun getAllLanguages(): List<Language>
fun getAlphaCode4String(code: String): Language?
fun getAlphaCode4Lang(lang: Language): Language?
}

View File

@@ -3,6 +3,7 @@ package service
import dao.LanguageDao
import org.jetbrains.exposed.sql.transactions.transaction
import service.DatabaseFactory.dbExecId
import service.DatabaseFactory.getLanguageForCode
import models.Language
class LanguageServiceImpl : LanguageService {
@@ -14,4 +15,12 @@ class LanguageServiceImpl : LanguageService {
LanguageDao.all().map { it.toModel() }
}
override fun getAlphaCode4String(code: String): Language? {
return getLanguageForCode(code)
}
override fun getAlphaCode4Lang(lang: Language): Language? {
return getLanguageForCode(lang.shortName)
}
}

View File

@@ -8,5 +8,5 @@ object Languages : IntIdTable() {
val name = varchar("name", 255)
val shortName = varchar("short_name", 255)
val alphaCode = char("alpha_code",2).nullable()
}

View File

@@ -6,8 +6,8 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import tables.Suffixes
object Terms : IntIdTable() {
val string1 = varchar("string1", 255)
val string2 = varchar("string2", 255)
val string1 = varchar("string1", 255).index()
val string2 = varchar("string2", 255).index()
val suffix1 = reference("suffix1_id",Suffixes).nullable()
val suffix2 = reference("suffix2_id",Suffixes).nullable()
val type = reference("type_id",DictTypes).nullable()