From a09b74913380f91c478fdd2bf572ab08eb2eda04 Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Tue, 5 Dec 2023 14:07:53 +0100 Subject: [PATCH 1/3] Add csv export --- src/main/kotlin/main/Main.kt | 66 +++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 2ae0630..f6f84c6 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -27,6 +27,9 @@ 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.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive val connection = DatabaseManager.getConnection() @@ -120,7 +123,24 @@ fun App() { println("Open command cancelled by user.") } }, - onExporterClick = { /* Action d'export */ }, + onExporterClick = { + val fileDialog = FileDialog(Frame(), "Save as", FileDialog.SAVE) + fileDialog.file = "GlossaireExporte.csv" + fileDialog.isVisible = true + + val selectedFile = fileDialog.file + val selectedDirectory = fileDialog.directory + + if (selectedFile != null) { + val csvFilePath = selectedDirectory + selectedFile + println("Exporting to: $csvFilePath") + + exportJsonToCSV("glossaire.json", csvFilePath) + } else { + println("Export command cancelled by user.") + } + } + , onRetourClick = { currentPage.value = "accueil" } ) } @@ -188,6 +208,50 @@ fun App() { } } +fun exportJsonToCSV(jsonFilePath: String, csvFilePath: String) { + try { + // Read JSON file + val jsonContent = File(jsonFilePath).readText(Charsets.UTF_8) + val glossaryData = Json.parseToJsonElement(jsonContent) + + if (glossaryData is JsonObject) { + val header = glossaryData.keys.toList() + val csvContent = buildString { + // Append the header + append(header.joinToString(separator = ";")) + append("\n") + + // Append the data + appendJsonToCSV(glossaryData, this) + } + + // Write to CSV file + File(csvFilePath).writeText(csvContent, Charsets.UTF_8) + println("Export successful. File saved at: $csvFilePath") + } else { + println("Invalid JSON file format.") + } + } catch (e: IOException) { + println("Error exporting data: ${e.message}") + } +} + +fun appendJsonToCSV(json: JsonObject, stringBuilder: StringBuilder) { + val values = json.values.map { + when (it) { + is JsonPrimitive -> it.content + is JsonObject -> { + val nestedValues = buildString { appendJsonToCSV(it, this) } + "\"$nestedValues\"" + } + else -> "" + } + } + + stringBuilder.append(values.joinToString(separator = ";")) + stringBuilder.append("\n") +} + fun importCSV(cheminFichier: String) { val file = File(cheminFichier).readText(Charsets.UTF_8) From dfa960e968b312399c6a9d051009e1b334360288 Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Tue, 5 Dec 2023 14:42:06 +0100 Subject: [PATCH 2/3] Update csv export --- src/main/kotlin/main/Main.kt | 77 +++++++++++++++--------------------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index f6f84c6..d4fcb39 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -21,18 +21,14 @@ import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.window.* -import database.DatabaseManager +import kotlinx.serialization.decodeFromString 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.json.JsonObject -import kotlinx.serialization.json.JsonPrimitive - -val connection = DatabaseManager.getConnection() val customRedColor = Color(0xFFB70D1B) @OptIn(ExperimentalComposeUiApi::class) @@ -124,23 +120,24 @@ fun App() { } }, onExporterClick = { - val fileDialog = FileDialog(Frame(), "Save as", FileDialog.SAVE) - fileDialog.file = "GlossaireExporte.csv" + 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 csvFilePath = "$selectedDirectory$selectedFile" + val jsonFilePath = "glossaire.json" // Replace with the actual path to your JSON file + println("Exporting to: $csvFilePath") - exportJsonToCSV("glossaire.json", csvFilePath) + exportToCSV(jsonFilePath, csvFilePath) } else { println("Export command cancelled by user.") } - } - , + }, onRetourClick = { currentPage.value = "accueil" } ) } @@ -208,48 +205,36 @@ fun App() { } } -fun exportJsonToCSV(jsonFilePath: String, csvFilePath: String) { +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 { - // Read JSON file - val jsonContent = File(jsonFilePath).readText(Charsets.UTF_8) - val glossaryData = Json.parseToJsonElement(jsonContent) - - if (glossaryData is JsonObject) { - val header = glossaryData.keys.toList() - val csvContent = buildString { - // Append the header - append(header.joinToString(separator = ";")) - append("\n") - - // Append the data - appendJsonToCSV(glossaryData, this) - } - - // Write to CSV file - File(csvFilePath).writeText(csvContent, Charsets.UTF_8) - println("Export successful. File saved at: $csvFilePath") - } else { - println("Invalid JSON file format.") + FileWriter(csvFilePath).use { fileWriter -> + fileWriter.write(csvContent) } } catch (e: IOException) { - println("Error exporting data: ${e.message}") + e.printStackTrace() } } -fun appendJsonToCSV(json: JsonObject, stringBuilder: StringBuilder) { - val values = json.values.map { - when (it) { - is JsonPrimitive -> it.content - is JsonObject -> { - val nestedValues = buildString { appendJsonToCSV(it, this) } - "\"$nestedValues\"" - } - else -> "" - } +fun readJsonFile(filePath: String): List> { + return try { + val content = File(filePath).readText(Charsets.UTF_8) + Json.decodeFromString(content) + } catch (e: Exception) { + e.printStackTrace() + emptyList() } - - stringBuilder.append(values.joinToString(separator = ";")) - stringBuilder.append("\n") } fun importCSV(cheminFichier: String) { From 65f6610c0b382cf6d0eca3d18d6c9c9c92f9fda3 Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Tue, 5 Dec 2023 15:12:38 +0100 Subject: [PATCH 3/3] Remove unused variable --- src/main/kotlin/main/Main.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index d4fcb39..b6fc204 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -37,7 +37,6 @@ fun HomePage( onGlossaireClick: () -> Unit, onCodeAVerifierClick: () -> Unit ) { - var isHover by remember { mutableStateOf(false) } Column( modifier = Modifier.fillMaxSize(),