Added csv file verification

main
Cemal Odabasioglu 2023-12-04 15:28:25 +01:00
parent 8955734175
commit eb764277dd
1 changed files with 36 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package main package main
import androidx.compose.desktop.ui.tooling.preview.Preview import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -23,13 +22,11 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.window.* import androidx.compose.ui.window.*
import database.DatabaseManager import database.DatabaseManager
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put import kotlinx.serialization.json.put
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.io.IOException import java.io.IOException
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
val connection = DatabaseManager.getConnection() val connection = DatabaseManager.getConnection()
@ -117,7 +114,7 @@ fun App() {
val filePath = selectedDirectory + selectedFile val filePath = selectedDirectory + selectedFile
println("Opening: $filePath") println("Opening: $filePath")
lireFichierCSV(filePath) importCSV(filePath)
} else { } else {
println("Open command cancelled by user.") println("Open command cancelled by user.")
@ -191,8 +188,8 @@ fun App() {
} }
} }
fun lireFichierCSV(cheminFichier: String) { fun importCSV(cheminFichier: String) {
val file = File(cheminFichier).readText() val file = File(cheminFichier).readText(Charsets.UTF_8)
val lines = file.split("\n") val lines = file.split("\n")
val header = lines.first().split(";") val header = lines.first().split(";")
@ -200,25 +197,53 @@ fun lireFichierCSV(cheminFichier: String) {
dataLines.forEach { line -> dataLines.forEach { line ->
val values = line.split(";") val values = line.split(";")
val mot = header.zip(values).toMap() values.forEach { value ->
println(mot) println(value)
}
val mot = header.zip(values).toMap().toMutableMap()
if(mot["Antonyme"] == "\r") {
mot["Antonyme"] = ""
}
if (mot["Mot"] == null || mot["Mot"] == "") { if (mot["Mot"] == null || mot["Mot"] == "") {
return return
} }
if (!verifierChampsRequis(mot)) {
return
}
ajouterMotAuGlossaire(mot) ajouterMotAuGlossaire(mot)
} }
} }
fun verifierChampsRequis(mot: Map<String, String>): Boolean { fun verifierChampsRequis(mot: Map<String, String>): Boolean {
val champsRequis = listOf("Mot", "Description", "Contexte principal", "Contexte 2", "Lié à", "Synonyme", "Antonyme") //Vérifier que les headers sont égales aux champs requis
val champsRequis = listOf(
"Mot",
"Description",
"Contexte principal",
"Contexte 2",
"Lié à",
"Synonyme",
"Antonyme"
)
val motKeys = mot.keys.map { it.trim() }
motKeys.forEach { key ->
if (!champsRequis.contains(key)) {
println("Le champ $key n'est pas reconnu")
return false
}
}
return true
return champsRequis.all { mot.containsKey(it) }
} }
fun ajouterMotAuGlossaire(mot: Map<String, String>) { fun ajouterMotAuGlossaire(mot: Map<String, String>) {
println("Ajout du mot :" + mot["Mot"]) println("Ajout du mot :" + mot["Mot"])
val motJson = buildJsonObject { val motJson = buildJsonObject {
put("nom", mot["Mot"]) put("nom", mot["Mot"])
put("description", mot["Description"]) put("description", mot["Description"])
@ -229,6 +254,7 @@ fun ajouterMotAuGlossaire(mot: Map<String, String>) {
put("antonyme", mot["Antonyme"]) put("antonyme", mot["Antonyme"])
} }
try { try {
FileWriter("glossaire.json", true).use { fileWriter -> FileWriter("glossaire.json", true).use { fileWriter ->
fileWriter.appendLine(motJson.toString()) fileWriter.appendLine(motJson.toString())
@ -237,8 +263,6 @@ fun ajouterMotAuGlossaire(mot: Map<String, String>) {
e.printStackTrace() e.printStackTrace()
} }
println("Mot ajouté avec succès : $mot")
} }
@Composable @Composable