diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 14ffe31..32f196e 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -22,7 +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.Json +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.put import java.io.File +import java.io.FileWriter import java.io.IOException import kotlinx.serialization.json.Json import kotlinx.serialization.encodeToString @@ -122,7 +126,21 @@ fun App() { println("Open command cancelled by user.") } }, - onExporterClick = { /* Action d'export */ }, + onExporterClick = { + val fileDialog = FileDialog(Frame(), "Save as CSV", FileDialog.SAVE) + fileDialog.file = "glossaire_exporte.csv" // Initial file name + fileDialog.isVisible = true + val selectedFile = fileDialog.file + val selectedDirectory = fileDialog.directory + if (selectedFile != null) { + val csvFilePath = "$selectedDirectory$selectedFile" + val jsonFilePath = "glossaire.json" // Replace with the actual path to your JSON file + println("Exporting to: $csvFilePath") + exportToCSV(jsonFilePath, csvFilePath) + } else { + println("Export command cancelled by user.") + } + }, onRetourClick = { currentPage.value = "accueil" } ) } @@ -190,6 +208,35 @@ fun App() { } } +fun exportToCSV(jsonFilePath: String, csvFilePath: String) { + val glossary = readJsonFile(jsonFilePath) + val csvContent = buildString { + appendLine("nom,description,contextePrincipal,contexte2,lieA,synonyme,antonyme") + glossary.forEach { entry -> + appendLine( + "${entry["nom"]},${entry["description"]},${entry["contextePrincipal"]}," + + "${entry["contexte2"]},${entry["lieA"]},${entry["synonyme"]},${entry["antonyme"]}" + ) + } + } + try { + FileWriter(csvFilePath).use { fileWriter -> + fileWriter.write(csvContent) + } + } catch (e: IOException) { + e.printStackTrace() + } +} +fun readJsonFile(filePath: String): List> { + return try { + val content = File(filePath).readText(Charsets.UTF_8) + Json.decodeFromString(content) + } catch (e: Exception) { + e.printStackTrace() + emptyList() + } +} + fun importCSV(cheminFichier: String) { val file = File(cheminFichier).readText(Charsets.UTF_8)