Added csv import
parent
9a911e9352
commit
8955734175
|
@ -23,6 +23,7 @@ dependencies {
|
|||
implementation(kotlin("stdlib"))
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
|
||||
implementation("mysql:mysql-connector-java:8.0.23")
|
||||
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.2")
|
||||
implementation("org.jetbrains.compose.material:material:1.0.0-beta-02")
|
||||
implementation("org.jetbrains.compose.ui:ui:1.0.0-beta-02")
|
||||
implementation("org.jetbrains.compose.foundation:foundation:1.0.0-beta-02")
|
||||
|
|
|
@ -29,6 +29,7 @@ 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()
|
||||
|
@ -115,6 +116,9 @@ fun App() {
|
|||
if (selectedFile != null) {
|
||||
val filePath = selectedDirectory + selectedFile
|
||||
println("Opening: $filePath")
|
||||
|
||||
lireFichierCSV(filePath)
|
||||
|
||||
} else {
|
||||
println("Open command cancelled by user.")
|
||||
}
|
||||
|
@ -187,6 +191,55 @@ fun App() {
|
|||
}
|
||||
}
|
||||
|
||||
fun lireFichierCSV(cheminFichier: String) {
|
||||
val file = File(cheminFichier).readText()
|
||||
|
||||
val lines = file.split("\n")
|
||||
val header = lines.first().split(";")
|
||||
val dataLines = lines.drop(1)
|
||||
|
||||
dataLines.forEach { line ->
|
||||
val values = line.split(";")
|
||||
val mot = header.zip(values).toMap()
|
||||
println(mot)
|
||||
if (mot["Mot"] == null || mot["Mot"] == "") {
|
||||
return
|
||||
}
|
||||
ajouterMotAuGlossaire(mot)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun verifierChampsRequis(mot: Map<String, String>): Boolean {
|
||||
val champsRequis = listOf("Mot", "Description", "Contexte principal", "Contexte 2", "Lié à", "Synonyme", "Antonyme")
|
||||
|
||||
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"])
|
||||
put("contextePrincipal", mot["Contexte principal"])
|
||||
put("contexte2", mot["Contexte 2"])
|
||||
put("lieA", mot["Lié à"])
|
||||
put("synonyme", mot["Synonyme"])
|
||||
put("antonyme", mot["Antonyme"])
|
||||
}
|
||||
|
||||
try {
|
||||
FileWriter("glossaire.json", true).use { fileWriter ->
|
||||
fileWriter.appendLine(motJson.toString())
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
|
||||
println("Mot ajouté avec succès : $mot")
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ChoixLangagePage(
|
||||
|
@ -459,7 +512,6 @@ fun FormulairePage(onAnnulerClick: () -> Unit) {
|
|||
e.printStackTrace()
|
||||
}
|
||||
|
||||
// Reste de la logique de validation ou de navigation
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = customRedColor,
|
||||
|
|
Loading…
Reference in New Issue