Added csv file verification
parent
8955734175
commit
eb764277dd
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import androidx.compose.desktop.ui.tooling.preview.Preview
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
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.window.*
|
||||
import database.DatabaseManager
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.IOException
|
||||
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
|
||||
|
||||
|
||||
val connection = DatabaseManager.getConnection()
|
||||
|
@ -117,7 +114,7 @@ fun App() {
|
|||
val filePath = selectedDirectory + selectedFile
|
||||
println("Opening: $filePath")
|
||||
|
||||
lireFichierCSV(filePath)
|
||||
importCSV(filePath)
|
||||
|
||||
} else {
|
||||
println("Open command cancelled by user.")
|
||||
|
@ -191,8 +188,8 @@ fun App() {
|
|||
}
|
||||
}
|
||||
|
||||
fun lireFichierCSV(cheminFichier: String) {
|
||||
val file = File(cheminFichier).readText()
|
||||
fun importCSV(cheminFichier: String) {
|
||||
val file = File(cheminFichier).readText(Charsets.UTF_8)
|
||||
|
||||
val lines = file.split("\n")
|
||||
val header = lines.first().split(";")
|
||||
|
@ -200,25 +197,53 @@ fun lireFichierCSV(cheminFichier: String) {
|
|||
|
||||
dataLines.forEach { line ->
|
||||
val values = line.split(";")
|
||||
val mot = header.zip(values).toMap()
|
||||
println(mot)
|
||||
values.forEach { value ->
|
||||
println(value)
|
||||
}
|
||||
val mot = header.zip(values).toMap().toMutableMap()
|
||||
if(mot["Antonyme"] == "\r") {
|
||||
mot["Antonyme"] = ""
|
||||
}
|
||||
if (mot["Mot"] == null || mot["Mot"] == "") {
|
||||
return
|
||||
}
|
||||
if (!verifierChampsRequis(mot)) {
|
||||
return
|
||||
}
|
||||
ajouterMotAuGlossaire(mot)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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>) {
|
||||
println("Ajout du mot :" + mot["Mot"])
|
||||
|
||||
|
||||
val motJson = buildJsonObject {
|
||||
put("nom", mot["Mot"])
|
||||
put("description", mot["Description"])
|
||||
|
@ -229,6 +254,7 @@ fun ajouterMotAuGlossaire(mot: Map<String, String>) {
|
|||
put("antonyme", mot["Antonyme"])
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
FileWriter("glossaire.json", true).use { fileWriter ->
|
||||
fileWriter.appendLine(motJson.toString())
|
||||
|
@ -237,8 +263,6 @@ fun ajouterMotAuGlossaire(mot: Map<String, String>) {
|
|||
e.printStackTrace()
|
||||
}
|
||||
|
||||
|
||||
println("Mot ajouté avec succès : $mot")
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
|
Loading…
Reference in New Issue